Skip to main content

my.getLocation

getLocation

my.getLocation là API dùng để lấy thông tin vị trí địa lí hiện tại của device.

Khả dụng: Hỗ trợ từ runtime version 1.73.1 trở lên.

Quét mã để trải nghiệm

Xem code mẫu trên Tini Studio

Demo

Trải nghiệm thử với trình giả lập bên dưới

API Params

Thuộc tínhKiểu dữ liệuMô tả
cacheTimeoutnumberThời gian mà cache toạ độ địa lý của thiết bị có hiệu lực. Đơn vị tính là giây, mặc định 30 giây. Sử dụng cache toạ độ giúp tăng tốc độ lấy location, đồng thời tiết kiệm pin.
typenumberChọn độ chính xác:
0 - low accuracy. Sử dụng wifi để lấy location
1 - high accuracy. Sử dụng GPS để lấy location
successFunctionCallback function khi lấy thông tin location được thực hiện thành công
failFunctionCallback function khi lấy thông tin location thất bại
completeFunctionCallback function khi việc lấy thông tin location kết thúc cho dù thành công hay thất bại

Callback success function payload

Thuộc tínhKiểu dữ liệuMô tả
latitudenumberKinh độ của thiết bị
longitudemumberVĩ độ của thiết bị
altitudenumberĐộ cao của thiết bị so với mực nước biển
accuracynumberĐộ chính xác của vị trí; tính bằng mét
altitudeAccuracynumberĐộ chính xác của độ cao; tính bằng mét
headingnumberHướng di chuyển
speednumberTốc độ di chuyển; tính bằng m/s

Sample Code

<view>
<view class="block-content">
<button class="button-full" onTap="onGetLocation">Get User Location</button>
</view>
<view class="block-content" hidden={{!location}}>
<view class="title mb-8">Current location:</view>
<text class="text-full">Latitude:{{location.latitude}}</text>
<text class="text-full">Longitude:{{location.longitude}}</text>
<text class="text-full">Altitude:{{location.altitude}}</text>
<text class="text-full">Accuracy:{{location.accuracy}}</text>
<text class="text-full">Altitude Accuracy:{{location.altitudeAccuracy}}</text>
<text class="text-full">Heading:{{location.heading}}</text>
<text class="text-full">Speed:{{location.speed}}</text>
</view>
</view>
Page({
data: {
location: undefined
},
onGetLocation() {
my.getLocation({
cacheTimeout: 30,
success: (res) => {
this.setData({ location: res });
},
fail: (e) => {
console.log(e);
}
});
}
});