根据经纬度定位用户所在城市

Posted 小橙子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据经纬度定位用户所在城市相关的知识,希望对你有一定的参考价值。

//根据经纬度定位用户所在城市

     if (navigator.geolocation){ navigator.geolocation.getCurrentPosition(showPosition, showErr); } function showPosition(position){ //alert(position.coords.latitude+‘,‘+position.coords.longitude); // ak = appkey 访问次数流量有限制 var xy = window.location.protocol.split(‘:‘)[0] === ‘https‘ ? ‘https‘ : ‘http‘; $.getJSON(xy+‘://api.map.baidu.com/geocoder/v2/?location=‘+position.coords.latitude+‘,‘+position.coords.longitude+‘&output=json&pois=1&ak=您的ak‘, function(res){ //addressComponent => {city: "广州市", district: "天河区", province: "广东省", street: "广州大道", street_number: "中922号-之101-128"} console.log(res.result.addressComponent.city); }) } function showErr(error){ var result; switch(error.code) { case error.PERMISSION_DENIED: result="User denied the request for Geolocation."; break; case error.POSITION_UNAVAILABLE: result="Location information is unavailable."; break; case error.TIMEOUT: result="The request to get user location timed out."; break; case error.UNKNOWN_ERROR: result="An unknown error occurred."; break; } alert(result); }

//百度API : http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding

以上是关于根据经纬度定位用户所在城市的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序自动定位,通过百度地图根据经纬度获取该地点所在城市信息

微信,根据经纬度获取当前城市

怎样根据GPS获得的经纬度来获取城市名?

微信小程序实现城市定位:获取当前所在的国家城市信息

android定位城市

北纬36.47东经116.64定位?