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

js实现手机页面定位

时间:2017-09-13 17:53:23      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:int   prototype   break   elf   浏览器   new   字符   dma   api   

<script type="text/javascript">
    function Location() {};
 
    Location.prototype.getLocation = function (callback) {
        var options = {
            enableHighAccuracy: true,
            maximumAge: 1000
        };
        this.callback = Object.prototype.toString.call(callback) == "[object Function]" ?
            callback :
        function (address) {
            alert(address.province + address.city);
            console.log("getocation(callbackFunction) 可获得定位信息对象");
        };
        var self = this;
        if (navigator.geolocation) {
            //浏览器支持geolocation
            navigator.geolocation.getCurrentPosition(function (position) {
                //经度
                var longitude = position.coords.longitude;
                //纬度
                var latitude = position.coords.latitude;
                self.loadMapApi(longitude, latitude);
            }, self.onError, options);
        } else {
            //浏览器不支持geolocation
        }
    };
 
    Location.prototype.loadMapApi = function (longitude, latitude) {
        var self = this;
        var oHead = document.getElementsByTagName(‘HEAD‘).item(0);
        var oScript = document.createElement("script");
        oScript.type = "text/javascript";
        oScript.src = "http://api.map.baidu.com/getscript?v=2.0&ak=A396783ee700cfdb9ba1df281ce36862&services=&t=20140930184510";
        oHead.appendChild(oScript);
        oScript.onload = function (date) {
            var point = new BMap.Point(longitude, latitude);
            var gc = new BMap.Geocoder();
            gc.getLocation(point, function (rs) {
                var addComp = rs.addressComponents;
                self.callback(addComp);
            });
        }
    };
 
    Location.prototype.onError = function (error) {
        switch (error.code) {
        case 1:
            alert("位置服务被拒绝");
            break;
        case 2:
            alert("暂时获取不到位置信息");
            break;
        case 3:
            alert("获取信息超时");
            break;
        case 4:
            alert("未知错误");
            break;
        }
    };
 
    //调用
    var local = new Location();
    local.getLocation(function (res) {
        //此处就是返回的地理位置信息
        console.log(res);
        //JSON.stringify(res),把返回的对象转为字符串了,自己根据需求截取下就好
        var resstr = JSON.stringify(res);
        alert(resstr);
    });
  </script>

放入手机端页面即可

js实现手机页面定位

标签:int   prototype   break   elf   浏览器   new   字符   dma   api   

原文地址:http://www.cnblogs.com/henuyuxiang/p/7516174.html

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