下面範例使用了Google Map GeoLocation Api對raspberry pi進行地理定位,
其中key可在https://console.developers.google.com申請,每日免費額度是2500(每100秒限制10000次)
/*
npm i pi-wifi google-geolocation
node main.js
*/
function geolocation(callback){
var geolocation = require ('google-geolocation') ({
key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
});
var piWifi = require('pi-wifi');
piWifi.scan(function(err, networks) {
if (err) {
return console.error(err.message);
}
//console.log(networks);
var items=[];
for(i in networks){
items.push({
'macAddress': networks[i].bssid,
'signalStrength': networks[i].signalLevel,
});
}
var params={
wifiAccessPoints: items,
};
geolocation (params, (err, data) => {
if (err) {
return;
}
var result={
'lat': data.location.lat,
'lng': data.location.lng,
'accuracy': data.accuracy,
'type': 'wifi',
};
if(callback && typeof(callback) == "function")
callback(result);
});
});
}
geolocation(function(location){
console.log(location);
var url ="https://www.google.com.tw/maps/search/"+location.lat+","+location.lng;
console.log(url);
});
文章短網址: https://slanla.com/__st1p5d