有没有办法使用javascript的谷歌地图api从纬度和经度点获取城市名称?
如果是这样,我可以看看一个例子吗?
有没有办法使用javascript的谷歌地图api从纬度和经度点获取城市名称?
如果是这样,我可以看看一个例子吗?
这称为反向地理编码
这是一个完整的示例:
<!DOCTYPE html>
<html>
<head>
<title>Geolocation API with Google Maps API</title>
<meta charset="UTF-8" />
</head>
<body>
<script>
function displayLocation(latitude,longitude){
var request = new XMLHttpRequest();
var method = 'GET';
var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+latitude+','+longitude+'&sensor=true';
var async = true;
request.open(method, url, async);
request.onreadystatechange = function(){
if(request.readyState == 4 && request.status == 200){
var data = JSON.parse(request.responseText);
var address = data.results[0];
document.write(address.formatted_address);
}
};
request.send();
};
var successCallback = function(position){
var x = position.coords.latitude;
var y = position.coords.longitude;
displayLocation(x,y);
};
var errorCallback = function(error){
var errorMessage = 'Unknown error';
switch(error.code) {
case 1:
errorMessage = 'Permission denied';
break;
case 2:
errorMessage = 'Position unavailable';
break;
case 3:
errorMessage = 'Timeout';
break;
}
document.write(errorMessage);
};
var options = {
enableHighAccuracy: true,
timeout: 1000,
maximumAge: 0
};
navigator.geolocation.getCurrentPosition(successCallback,errorCallback,options);
</script>
</body>
</html>
在 node.js 中,我们可以使用node-geocoder npm module从 lat、lng.、
地理.js
var NodeGeocoder = require('node-geocoder');
var options = {
provider: 'google',
httpAdapter: 'https', // Default
apiKey: ' ', // for Mapquest, OpenCage, Google Premier
formatter: 'json' // 'gpx', 'string', ...
};
var geocoder = NodeGeocoder(options);
geocoder.reverse({lat:28.5967439, lon:77.3285038}, function(err, res) {
console.log(res);
});
输出:
节点geo.js
[ { formattedAddress: 'C-85B, C Block, Sector 8, Noida, Uttar Pradesh 201301, India',
latitude: 28.5967439,
longitude: 77.3285038,
extra:
{ googlePlaceId: 'ChIJkTdx9vzkDDkRx6LVvtz1Rhk',
confidence: 1,
premise: 'C-85B',
subpremise: null,
neighborhood: 'C Block',
establishment: null },
administrativeLevels:
{ level2long: 'Gautam Buddh Nagar',
level2short: 'Gautam Buddh Nagar',
level1long: 'Uttar Pradesh',
level1short: 'UP' },
city: 'Noida',
country: 'India',
countryCode: 'IN',
zipcode: '201301',
provider: 'google' } ]
这是谷歌地理编码网络服务的最新示例
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY
只需将 更改为YOUR_API_KEY
您从Google Geocoding API获得的 API 密钥
P/S:地理编码 API位于Places NOT Maps 下 ;)
这是使用Promise的现代解决方案:
function getAddress (latitude, longitude) {
return new Promise(function (resolve, reject) {
var request = new XMLHttpRequest();
var method = 'GET';
var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&sensor=true';
var async = true;
request.open(method, url, async);
request.onreadystatechange = function () {
if (request.readyState == 4) {
if (request.status == 200) {
var data = JSON.parse(request.responseText);
var address = data.results[0];
resolve(address);
}
else {
reject(request.status);
}
}
};
request.send();
});
};
并这样称呼它:
getAddress(lat, lon).then(console.log).catch(console.error);
Promise在“then”中返回地址对象或在“catch”中返回错误状态代码