WeChat Open Platform

Mini Programs Documentation

Location

wx.getLocation(OBJECT)

Gets current geographical location and speed. This interface cannot be called after the user leaves the Mini Program. It can continue to be called when the user clicks on "Display at top of chat list".

OBJECT parameter descriptions:

Parameter Type Required Description
type String No Default is GPS coordinates returned by WGS84, GCJ-02 returns coordinates that can be used for wx.openLocation
success Function Yes Callback function for successful interface call, refer to return parameter descriptions for return content
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

success return parameter descriptions:

Parameter Description
latitude Latitude, floating-point number, range is -90 to 90, negative numbers indicate southern latitudes
longitude Longitude, floating-point number, range is -180 to 180, negative numbers indicate western latitudes
speed Speed, floating-point number, unit m/s
accuracy Location accuracy

Sample code:

wx.getLocation({
  type: 'wgs84',
  success: function(res) {
    var latitude = res.latitude
    var longitude = res.longitude
    var speed = res.speed
    var accuracy = res.accuracy
  }
})

wx.chooseLocation(OBJECT)

Open map and select location.

OBJECT parameter descriptions:

Parameter Type Required Description
success Function Yes Callback function for successful interface call, refer to return parameter descriptions for return content
cancel Function No Called when user cancels
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

success return parameter descriptions:

Parameter Description
name Location name
address Detailed address
latitude Latitude, floating-point number, range is -90 to 90, negative numbers indicate southern latitudes
longitude Longitude, floating-point number, range is -180 to 180, negative numbers indicate western latitudes

wx.openLocation(OBJECT)

‚Äč Use WeChat built-in map to view location.

OBJECT parameter descriptions:

Parameter Type Required Description
latitude Float Yes Latitude, range is -90 to 90, negative numbers indicate southern latitudes
longitude Float Yes Longitude, range is -180 to 180, negative numbers indicate western latitudes
scale INT No Scale, range is 5 to 18, default is 18
name String No Location name
address String No Detailed description of address
success Function No Callback function for successful interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

Sample code:

wx.getLocation({
  type: 'gcj02', //Returns latitudes and longitudes that can be used for wx.openLocation
  success: function(res) {
    var latitude = res.latitude
    var longitude = res.longitude
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 28
    })
  }
})

Bugs & Tips

  1. bug: Type parameters are invalid in iOS 6.3.30, only WGS84 type coordinate information will be returned.
  2. tip: The wx.getLocation and wx.chooseLocation interfaces require user authorization, they are scenes where compatible users are requested to decline authorization.