Location

更新时间:
复制 MD 格式

my.chooseLocation

This API is used to open the built-in map to choose a location.

  • When using this API on the Android client, you need to apply for an AutoNavi key and add it to AndroidManifest. For details, see Applying for AutoNavi Key.

  • When using this API on iOS, you need to set the AutoNavi key in the beforeDidFinishLaunchingWithOptions method. The required code is shown below. Please refer to the Obtaining Key document to obtain the AutoNavi key.

    [APMapKeySetting getInstance].apiKey = @"AutoNavi key"

Restrictions

  • There is no overseas map data temporarily, and this API may not be called normally in regions other than Mainland China (excluding Hong Kong, Macao and Taiwan).

  • Only supports Autonavi Map Style and National Confidentiality Plugin (AKA Mars coordinate system).

Example

chooselocation

Input parameter

Object type, the attributes are as follows:

Attribute

Type

Required

Description

success

Function

No

Callback function for successful call.

fail

Function

No

Callback function for failed call.

complete

Function

No

Callback function for ended call (executed regardless of whether the call is successful or failed).

success callback function

Attribute

Type

Description

name

String

Location name.

address

String

Detailed location.

latitude

Number

Latitude, floating point number, the range is -90 ~ 90, negative number means south latitude.

longitude

Number

Longitude, floating point number, the range is -180 ~ 180, negative number means west longitude.

provinceName

String

Province name

cityName

String

City name.

Code sample

.json code sample:

// API-DEMO page/API/choose-location/choose-location.json
{
    "defaultTitle": "Choose location"
}

.axml code sample:

<!-- API-DEMO page/API/choose-location/choose-location.axml-->
<view class="page">
  <view class="page-section">
    <view class="page-section-demo">
      <text>Longitude:</text>
      <input value="{{longitude}}"></input>
    </view>
    <view class="page-section-demo">
      <text>Latitude:</text>
      <input value="{{latitude}}"></input>
    </view>
    <view class="page-section-demo">
      <text>Location name:</text>
      <input value="{{name}}"></input>
    </view>
    <view class="page-section-demo">
      <text>Detailed location:</text>
      <input value="{{address}}"></input>
    </view>    
    <view class="page-section-btns">
      <view onTap="chooseLocation">Choose location</view>
    </view>
  </view>
</view>

.js code sample:

// API-DEMO page/API/choose-location/choose-location.js
Page({
  data: {
    longitude: '120.126293',
    latitude: '30.274653',
    name: 'Huanglong Center',
    address: 'No. 77, Xueyuan Road',
  },
  chooseLocation() {
    var that = this
    my.chooseLocation({
         success:(res)=>{
          console.log(JSON.stringify(res))
          that.setData({
            longitude:res.longitude,
            latitude:res.latitude,
            name:res.name,
            address:res.address
          })
        },
        fail:(error)=>{
          my.alert({content: 'Call failed:'+JSON.stringify(error), });
        },
    });
    },
})

.acss code sample:

/* API-DEMO page/API/choose-location/choose-location.acss */
.page-body-info {
  height: 250rpx;
}
.page-body-text-location {
  display: flex;
  font-size: 50rpx;
}
.page-body-text-location text {
  margin: 10rpx;
}
.page-section-location-text{
    color: #49a9ee;
}

my.getLocation(OBJECT)

This API is used to get the user’s current geographic location information.

  • When using this API on the Android client, you need to apply for an AutoNavi key and add it to AndroidManifest. For details, see Applying for AutoNavi Key.

  • When using this API on iOS, you need to set the AutoNavi key in the beforeDidFinishLaunchingWithOptions method. The required code is shown below. Please refer to the Obtaining Key document to obtain the AutoNavi key.

    [LBSmPaaSAdaptor sharedInstance].shouldAMapRegeoWhenLBSFailed = YES;
    [AMapServices sharedServices].apiKey = @"AutoNave Key"

Restrictions

  • This interface is supported since basic library version 1.1.0. Compatibility processing is required for earlier versions. See Mini Program base library to learn more.

  • There is no overseas map data temporarily, and this API may not be called normally in regions other than Mainland China (excluding Hong Kong, Macao and Taiwan).

  • Only supports Autonavi Map Style and National Confidentiality Plugin (AKA Mars coordinate system).

Example

getLocation

Input parameter

Name

Type

Required

Description

cacheTimeout

Number

No

Expiration time of mPaaS client’s latitude and longitude location cache, in seconds. It defaults to 30 seconds. Using cache will speed up the positioning, and the reposition will be performed in case the cache is expired.

type

Number

No

Get the type of latitude and longitude data. The default value is 0. The earliest version is 1.1.1.

success

Function

No

Callback function for call success

fail

Function

No

Callback function for call failure

complete

Function

No

Callback function for call end (executed regardless of whether the call is successful or failed)

success return value

Name

Type

Description

Earliest version

longitude

String

Longitude

-

latitude

String

Latitude

-

accuracy

String

Accuracy, in meters

-

horizontalAccuracy

String

Horizontal accuracy, in meters

-

country

String

Country (valid in case type>0)

1.1.1

countryCode

String

Country code (valid in case type>0)

1.1.1

province

String

Province (valid in case type>0)

1.1.1

city

String

City (valid in case type>0)

1.1.1

cityAdcode

String

Region code at city level (valid in case type>0)

1.1.1

district

String

District & county (valid in case type>0)

1.1.1

districtAdcode

String

Region code at district & county level (valid in case type>0)

1.1.1

streetNumber

Object

This field is returned only when street level reverse geocoding data is required. Street number information in the format of {street, number} (valid in case type>1)

1.1.1

pois

array

This field is returned only when POI level reverse geocoding data is required. POI near the locating position in the format of {name, address} (valid in case type>2)

1.1.1

fail callback function

Object type, the attributes are as follows:

Attribute

Type

Description

error

String

Error code

errorMessage

String

Error information

Code sample

.json code sample:

// API-DEMO page/API/get-location/get-location.json
{
    "defaultTitle": "Get location"
}

.axml code sample:

<!-- API-DEMO page/API/get-location/get-location.axml-->
<view class="page">
  <view class="page-section">
    <view class="page-section-demo">
      <view>Longitude and latitude of the current location</view>
      <block a:if="{{hasLocation === false}}">
        <text>Not obtained</text>
      </block>
      <block a:if="{{hasLocation === true}}">
        <view class="page-body-text-location">
          <text>E{{location.longitude[0]}}°{{location.longitude[1]}}′</text>
          <text>N{{location.latitude[0]}}°{{location.latitude[1]}}′</text>
        </view>
      </block>
    </view>

    <view class="page-section-btns">
      <view onTap="getLocation">Get location</view>
      <view onTap="clear">Clear</view>
    </view>
  </view>
</view>

.js code sample:

// API-DEMO page/API/get-location/format-location.js
function formatLocation(longitude, latitude) {
  longitude = Number(longitude).toFixed(2),
  latitude = Number(latitude).toFixed(2)

  return {
    longitude: longitude.toString().split('.'),
    latitude: latitude.toString().split('.')
  }
}

export default formatLocation

.js code sample:

// API-DEMO page/API/get-location/get-location.js
import formatLocation from './format-location.js';

Page({
  data: {
    hasLocation: false,
  },
  getLocation() {
    var that = this;
    my.showLoading();
    my.getLocation({
      success(res) {
        my.hideLoading();
        console.log(res)
        that.setData({
          hasLocation: true,
          location: formatLocation(res.longitude, res.latitude)
        })
      },
      fail() {
        my.hideLoading();
        my.alert({ title: 'Positioning failed' });
      },
    })
  },
  clear() {
    this.setData({
      hasLocation: false
    })
  }
})

.acss code sample:

/* API-DEMO page/API/get-location/get-location.acss */
.page-body-info {
  height: 250rpx;
}
.page-body-text-small {
  font-size: 24rpx;
  color: #000;
  margin-bottom: 100rpx;
}
.page-body-text-location {
  display: flex;
  font-size: 50rpx;
}
.page-body-text-location text {
  margin: 10rpx;
}

Error code

Error code

Description

Solution

11

Please confirm that the location-related permissions have been turned on.

Prompt user to turn on the location permissions.

12

Network error, please try again later

Prompt the user to check the current network.

13

Positioning failed, please try again later

Prompt the user to try again.

14

Business positioning timeout

Prompt the user to try again.

2001

The user refused to authorize the mini program.

Prompt the user to accept the mini program authorization.

FAQ

  • Q: If the mini program is deleted after the my.getLocation authorization is allowed for the first time, will it need to re-authorize after reopening?

    A: Re-authorization is required. After deleting the Mini Program, the authorization relationship for obtaining the positioning will be deleted together.

my.openLocation

This API is used to use the built-in map of mPaaS MINI program to view the location.

Restrictions

  • There is no overseas map data temporarily, and this API may not be called normally in regions other than Mainland China (excluding Hong Kong, Macao and Taiwan).

  • Only supports Autonavi Map Style and National Confidentiality Plugin (AKA Mars coordinate system).

Example

my.openLocation

Input parameter

Name

Type

Required

Description

longitude

String

Yes

Longitude

latitude

String

Yes

Latitude

name

String

Yes

Location name

address

String

Yes

Detailed description of the address

scale

Number

No

Scaling, ranging from 3 ~ 19, it defaults to 15

success

Function

No

Callback function for call success

fail

Function

No

Callback function for call failure

complete

Function

No

Callback function for call end (executed regardless of whether the call is successful or failed)

Code sample

// API-DEMO page/API/open-location/open-location.json
{
    "defaultTitle": "View location"
}
<!-- API-DEMO page/API/open-location/open-location.axml-->
<view class="page">
  <view class="page-section">
    <view class="page-section-demo">
      <text>Longitude</text>
      <input type="text" disabled="{{true}}" value="{{longitude}}" name="longitude"></input>
    </view>
    <view class="page-section-demo">
      <text>Latitude</text>
      <input type="text" disabled="{{true}}"  value="{{latitude}}" name="latitude"></input>
    </view>
    <view class="page-section-demo">
      <text>Location name</text>
      <input type="text" disabled="{{true}}"  value="{{name}}" name="name"></input>
    </view>
    <view class="page-section-demo">
      <text>Detailed location</text>
      <input type="text" disabled="{{true}}"  value="{{address}}" name="address"></input>
    </view>
    <view class="page-section-btns">
      <view type="primary" formType="submit" onTap="openLocation">View location</view>
    </view>
  </view>
</view>
// API-DEMO page/API/open-location/open-location.js
Page({
  data: {
    longitude: '120.126293',
    latitude: '30.274653',
    name: 'Huanglong Center',
    address: 'No. 77, Xueyuan Road',
  },

  openLocation() {
    my.openLocation({
      longitude: this.data.longitude,
      latitude: this.data.latitude,
      name: this.data.name,
      address: this.data.address,
    })
  }
})