SoFunction
Updated on 2025-04-12

JavaScript WeChat positioning function implementation method

Share how WeChat is positioned:

This article mainly explains how to use WeChat positioning, how to convert the positioned latitude and longitude to the corresponding latitude and longitude of Baidu map, and the default practices when handling positioning failures, cancellations and errors.

//Get geographic location information start//Embroidered into a function function getPosition() {
 //Request with ajax  $.ajax({
   url: "/wechat/jssdk",//Request address   type: 'post',//Post request   dataType: 'json',
   contentType: "application/x-www-form-urlencoded; charset=utf-8",
   data: {
    'url': ('#')[0]//Pass the address before the first # number   },
//The function that requested successfully   success: function (data) {
    ({
     // debug: true,
     appId: ,
     timestamp: ,
     nonceStr: ,
     signature: ,
     jsApiList: ['checkJsApi', 'getLocation']
    });
    (function () {
     ({
     //Getting successful      success: function (res) {
      //This is the real latitude and longitude returned by WeChat       var oldLat = ; // Latitude, floating point number, range 90 ~ -90       var oldLng = ; // Longitude, floating point number, range from 180 to -180.       /*The following is to convert the obtained real latitude and longitude into the corresponding Baidu latitude and longitude, because the data is queried using the latitude and longitude of Baidu map, and the database is also stored in the latitude and longitude of Baidu*/
       //Create a point on Baidu map       var customerPoint = new (oldLng, oldLat);
       //
       var convertor = new ();

       var pointArr = [];//Create an array       (customerPoint);//Put the point just now       (pointArr, 1, 5, initMap); //Convert coordinates       function initMap(data) {
        if ( === 0) {//Conversion is successful         var point = [0];//The point after getting         var lng = ;//Get the converted longitude         var lat = ;//Get the converted latitude         toDoFunction(lng, lat);//Pass the latitude and longitude into the function to be used        } else {
        //The following two lines are the latitude and longitude of West Lake by default         lng = 120.141375;
         lat = 30.257806;
         toDoFunction(lng, lat);//Pass the latitude and longitude into the function to be used        }
       }

      },
      //Cancel Positioning      cancel: function () {
      //The following two lines are the latitude and longitude of West Lake by default       var lng = 120.141375;
       var lat = 30.257806;
       toDoFunction(lng, lat);//Pass the latitude and longitude into the function to be used      },
      //Location failed      fail: function () {
      //The following two lines are the latitude and longitude of West Lake by default       var lng = 120.141375;
       var lat = 30.257806;
       toDoFunction(lng, lat);//Pass the latitude and longitude into the function to be used      }

     });
     // An error occurred in positioning     (function () {
     //The following two lines are the latitude and longitude of West Lake by default      var lng = 120.141375;
      var lat = 30.257806;
      toDoFunction(lng, lat);//Pass the latitude and longitude into the function to be used     });

    });
   }
  });
 }

For the above code, if you can locate it, use the real longitude and longitude you locate it, and then convert it into the corresponding longitude and longitude of Baidu map. If the positioning fails, click cancel or an error occurs, it will be located to the latitude and longitude of West Lake by default.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.