使用百度地图进行定位

  • 用html5api获取当前经纬度
if (navigator.geolocation) {
    console.log("h5 定位中");
    navigator.geolocation.getCurrentPosition(function(position) {
        latitude  = position.coords.latitude;
        longitude = position.coords.longitude;
        console.log('h5定位成功;');
        console.log(latitude);
        console.log(longitude);
    }, function(error) {
        console.log('h5定失败;');
    })
}
  • 使用百度api把经纬度转为地址
    • 获得应用密钥ak
    • 引入
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=VvSbG64Z4oNtynCAf0XHYLHppKYGaPFZ"></script>
    
    • 转换
    var point = new BMap.Point(longitude, latitude);
    var geoc = new BMap.Geocoder();
    geoc.getLocation(point, function(rs){
        var addComp = rs.addressComponents;
        console.log(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
    });
    

总结

  • 在pc端使用ip定位,只能获得一个地址
  • 获得多个地址需要继续研究
  • 在移动端未尝试
posted @ 2017-05-21 22:11  风烟  阅读(205)  评论(0编辑  收藏  举报