码迷,mamicode.com
首页 > Web开发 > 详细

js定位当前位置的坐标经纬度和地点名称和天气

时间:2018-10-09 18:07:03      阅读:436      评论:0      收藏:0      [点我收藏+]

标签:sort   current   span   name   get   天气   http   point   location   

<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定位当前位置的坐标经纬度和地点名称和天气

标签:sort   current   span   name   get   天气   http   point   location   

原文地址:https://www.cnblogs.com/kccdzz/p/9761542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!