js實現手機頁面定位

來源:互聯網
上載者:User

標籤: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實現手機頁面定位

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.