JavaScript micro-Position function implementation method _javascript skills

Source: Internet
Author: User

Share how the micro-letters are positioned:

This article mainly explains how to use the micro-letter positioning, how to convert the latitude and longitude of the location to the corresponding latitude and longitude of Baidu map, as well as deal with positioning failure, cancellation and error when the default approach.

Get geographic information start//encapsulation into a function functions getPosition () {//$.ajax with Ajax request ({URL: "/WECHAT/JSSDK",//Request Address Type: ' Post ', Post request DataType: ' JSON ', ContentType: ' application/x-www-form-urlencoded; Charset=utf-8 ", data: {' URL ': Location.href.split (' # ') [0]//the address before the first # number},///Request successful function success:function (data ) {Wx.config ({//Debug:true, AppId:data.data.appId, Timestamp:data.data.timestamp, Noncestr:
    DATA.DATA.NONCESTR, Signature:data.data.signature, jsapilist: [' Checkjsapi ', ' getLocation ']};  Wx.ready (function () {wx.getlocation ({//Get location succeeded Success:function (res) {//This is the true latitude and longitude of a micro-letter return Var Oldlat = Res.latitude; Latitude, floating-point number, range of -90 var oldlng = res.longitude;
       Longitude, floating-point number, range 180 ~-180. * Below is to get the true latitude and longitude to convert to the corresponding Baidu latitude and longitude, because the use of the latitude and longitude of Baidu map to query data, the database is also Baidu's latitude and longitude * *///Create a Baidu map point var customerpoint = new BMAP.P
       Oint (OLDLNG, Oldlat);

  var convertor = new Bmap.convertor ();     var Pointarr = [];//Create an array pointarr.push (customerpoint);/Put the point in Convertor.translate (Pointarr, 1, 5, INITMAP);
         Convert coordinate function initmap (data) {if (Data.status = 0) {//convert success var point = data.points[0];// var LNG = point.lng;//Gets the converted longitude var lat = point.lat;//Gets the converted latitude todofunction (LNG, LAT);//Pass the longitude and latitude to the
         The use of the function} else {//The following two lines is the default positioning to the West Lake latitude and longitude of LNG = 120.141375;
         lat = 30.257806; Todofunction (LNG, LAT);//Pass the latitude and longitude to the function to be used}},//Cancel:function () {//the following two lines
       is the default location to the latitude and longitude of the West lake var lng = 120.141375;
       var lat = 30.257806; Todofunction (LNG, LAT);//Pass the latitude and longitude to the function to be used},///Locate failed Fail:function () {//The following two lines are the default positioning to the West Lake latitude and longitude va
       R LNG = 120.141375;
       var lat = 30.257806;
     Todofunction (LNG, LAT);//Pass the latitude and longitude to the function to be used}}); Location Error Wx.error (function () {//The following two lines are the default positioning to the West lake latitude and longitude var lng = 120.141375;
      var lat = 30.257806;

    Todofunction (LNG, LAT);//Pass the latitude and longitude to the function to be used});
   });
 }
  });
 }

The above code, if you can locate to the true latitude and longitude, and then converted to Baidu map corresponding latitude and longitude, if the location failed or click to cancel or error, the default positioning to the West lake latitude and longitude.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.