Javascript 如何从经纬度点获取城市名称?
有没有一种方法可以使用google maps api for javascript从纬度和经度点获取城市名称 如果是这样的话,我可以看一个例子吗?这叫做反向地理编码 来自谷歌的文档: 对谷歌地理编码Web服务的示例调用: 以下是完整的示例:Javascript 如何从经纬度点获取城市名称?,javascript,node.js,google-maps,geocoding,latitude-longitude,Javascript,Node.js,Google Maps,Geocoding,Latitude Longitude,有没有一种方法可以使用google maps api for javascript从纬度和经度点获取城市名称 如果是这样的话,我可以看一个例子吗?这叫做反向地理编码 来自谷歌的文档: 对谷歌地理编码Web服务的示例调用: 以下是完整的示例: 这是一个现代的解决方案,使用承诺: function getAddress (latitude, longitude) { return new Promise(function (resolve, reject) { var re
这是一个现代的解决方案,使用承诺:
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”中返回错误状态代码。以下代码可以使用Google Map Geo API获取城市名称: HTML
与@Sanchit Gupta相同 在这部分
if (results[0]) {
var add= results[0].formatted_address ;
var value=add.split(",");
count=value.length;
country=value[count-1];
state=value[count-2];
city=value[count-3];
x.innerHTML = "city name is: " + city;
}
只需控制台结果数组
if (results[0]) {
console.log(results[0]);
// choose from console whatever you need.
var city = results[0].address_components[3].short_name;
x.innerHTML = "city name is: " + city;
}
在node.js中,我们可以使用node geocoder从lat、lng获取地址
geo.js
输出:
node geo.js
这是谷歌地理编码网络服务的最新示例 只需将您的API密钥更改为您从
p/S:地理编码API位于位置而非地图之下 您可以使用纯php和google geocode api来实现 /* * *@param latlong字符串是纬度和经度,带有分隔符,例如21.3724002,39.8016229 **/ 函数getCityNameBystitudeLong$latlong { $APIKEY=aizaxxxxxxxxxxxxxxxxxxxxxxxxxxxx;//将其替换为您的google地图api密钥 $googleMapsUrl=https://maps.googleapis.com/maps/api/geocode/json?latlng= .$latlong&语言=ar&key=.$APIKEY; $response=file\u get\u contents$googleMapsUrl; $response=json_decode$response,true; $results=$response[results]; $addressComponents=$results[0][address_components]; $cityName=; foreach$addressComponents作为$component{ //echo$组件; $types=$component[types]; 如果在数组中,$types&&在数组中,$types{ $cityName=$component[long_name]; } } 如果$cityName=={ echo无法获取CityName; }否则{ echo$cityName; } }
有许多可用的工具 谷歌地图API就像所有人写的一样 使用这些数据 下载免费版本并使用一些在线转换器将excel转换为JSON,如 使用IP地址 这是不好的,因为使用某人的IP地址可能不好 用户认为你可以破解它们。 其他免费和付费工具也可用也有一个很好的API用于此,也适用于nodejs用户 他们有。而且,根据配额使用API_密钥是免费的 代码如下所示:
const client = require('@bigdatacloudapi/client')(API_KEY);
async foo() {
...
const location: string = await client.getReverseGeocode({
latitude:'32.101786566878445',
longitude: '34.858965073072056'
});
}
如果你不想使用谷歌地理编码API,你可以参考一些其他的免费API进行开发。 例如,我使用[mapquest]API来获取位置名称 通过实现以下函数,可以轻松获取位置名称 const fetchLocationName=async lat,lng=>{ 待命 'https://www.mapquestapi.com/geocoding/v1/reverse?key=API-Key&location='+lat+'%2C'+lng+'&outFormat=json&thumbMaps=false', .thenresponse=>response.json .thenresponseJson=>{ console.log “地址地理编码回来了!!=>”+JSON.stringifyresponseJson, ; };
};令人惊叹的我读了这篇文章,现在有太多的疑问;谢谢。如果你使用javascript API,每个IP地址和每个用户都是一样的,但是如果你使用PHP,你认为你会达到这些限制,你需要将请求限制在每秒1个,或者使用代理服务器,但是要小心代理,谷歌并不愚蠢,你不能攻击它们。此处的更多信息:您需要找到一个提供此功能的web服务。是否有任何方法可以在未经用户批准的情况下从纬度和经度查找用户位置?@VikasVerma如果允许您在没有用户许可的情况下查找用户位置,这将严重侵犯隐私consent@omerio谢谢,但我在那个里做了代码,我强迫用户点击允许他/她想继续。这实际上给了我确切的家庭地址。正是我想要的。如何像城市和州或邮政编码一样提取?感谢您提供的清晰有效的反馈,选择“节点地理编码器”和“@google/maps”会有什么区别吗?,虽然两个输出相同,但它们似乎做了相同的事情,但node geocoder是用于获取地址的简化模块,@google/maps是用于获取地址的api,我们需要对其进行配置。如果没有访问密钥,这将无法工作。传感器参数已过时,而不是javascript解决方案OP要求使用Google Maps API解决方案,我认为您没有回答这个问题。抱歉,我只是建议了一种替代方法。如果他有谷歌地理编码api密钥,它确实可以正常工作。
if (results[0]) {
var add= results[0].formatted_address ;
var value=add.split(",");
count=value.length;
country=value[count-1];
state=value[count-2];
city=value[count-3];
x.innerHTML = "city name is: " + city;
}
if (results[0]) {
console.log(results[0]);
// choose from console whatever you need.
var city = results[0].address_components[3].short_name;
x.innerHTML = "city name is: " + city;
}
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);
});
[ { 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' } ]
const client = require('@bigdatacloudapi/client')(API_KEY);
async foo() {
...
const location: string = await client.getReverseGeocode({
latitude:'32.101786566878445',
longitude: '34.858965073072056'
});
}