js定位当前位置的坐标经纬度和地点名称和天气
Posted 浮尘
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js定位当前位置的坐标经纬度和地点名称和天气相关的知识,希望对你有一定的参考价值。
<script src="http://api.map.baidu.com/api?v=2.0&ak=s6vFvPKgaEnI2ImqBpKGDj0m"></script> <script src="http://api.map.baidu.com/getscript?v=2.0&ak=s6vFvPKgaEnI2ImqBpKGDj0m&services=&t=20160401164342"></script> <div id="allmap"></div> <script> var cid = 35; var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ var position = { lng: r.point.lng, lat: r.point.lat } if(cid == ‘sort‘){ sort(position); } else { positions(position, cid); } //alert(‘您的位置:‘+r.point.lng+‘,‘+r.point.lat); $("#poi").val(r.point.lng+‘,‘+r.point.lat); getAddress(r.point.lng,r.point.lat); } else { alert(‘获取当前位置失败,请确定您开启了定位服务‘); } },{enableHighAccuracy: true}); //该js函数是必须要的不然要报错 function positions(json, cid) {} //根据经纬度获取具体地址信息 function getAddress(lng,lat){ var map = new BMap.Map("allmap"); var point = new BMap.Point(lng,lat); map.centerAndZoom(point,12); var geoc = new BMap.Geocoder(); geoc.getLocation(point,function(rs){ var addComp = rs.addressComponents; var names=""; for(var name in addComp){ names+=name+": "+addComp[name]+", "; } // alert(names); //alert(addComp.province + "," + addComp.city + "," + addComp.district + "," + addComp.street + "," + addComp.streetNumber); $("#area").val(addComp.province + "," + addComp.city + "," + addComp.district); $("#addr").val(addComp.street + "," + addComp.streetNumber); getWeather(addComp.city); }); } function getWeather(citytq) { $.ajax({ async: false, type: "GET", url: "http://wthrcdn.etouch.cn/weather_mini?city="+citytq, success: function (res) { alert(res); } }); } </script>
以上是关于js定位当前位置的坐标经纬度和地点名称和天气的主要内容,如果未能解决你的问题,请参考以下文章