如何获取经纬度示例代码

获取经纬度示例代码。

HarmonyOS
2024-06-03 23:28:47
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
江湖骗子嗷
import geoLocationManager from '@ohos.geoLocationManager'; 
import abilityAccessCtrl from '@ohos.abilityAccessCtrl'; 
 
/** 
 
 系统定位服务实现 
 */ 
@Entry 
@Component 
struct Index { 
  //纬度 
  @State lat: Number = -1; 
  //经度 
  @State lon: Number = -1; 
  @State message: string = '还没开始哦'; 
 
  getLocation() { 
    let requestInfo: geoLocationManager.CurrentLocationRequest = { 
      'priority': 0x203, 
      'scenario': geoLocationManager.LocationRequestScenario.DAILY_LIFE_SERVICE, 
      'maxAccuracy': 1000, 
      'timeoutMs': 5000 
    }; 
 
    if (geoLocationManager.isLocationEnabled()) { 
      console.log("AAAAAAAAAAQAAAAAAAAAAAAAAA:定位已使能"); 
    } 
    else { 
      console.log("AAAAAAAAAAQAAAAAAAAAAAAAAA:定位未使能"); 
    } 
 
    try { 
      geoLocationManager.getCurrentLocation(requestInfo, (err, location) => { 
        if (err) { 
          console.error("定位失败,失败原因为:"); 
          console.error(err.message); 
          return; 
        } 
        console.info("位置获取成功"); 
 
        this.lat = location.latitude; 
        this.lon = location.longitude; 
        console.info("this.lat = " + this.lat); 
        console.info("this.lon = " + this.lon); 
        setTimeout(() => { 
          this.message = '看见了'; 
        }, 2000); 
      }) 
    } catch (err) { 
      console.error("报错原因为:"); 
      console.error(err); 
    } 
  } 
 
  how() { 
    abilityAccessCtrl.createAtManager().requestPermissionsFromUser(getContext(), [ 
      'ohos.permission.LOCATION', 'ohos.permission.APPROXIMATELY_LOCATION']).then(() => { 
    }); 
  } 
 
  build() { 
    Column({ space: 50 }) { 
      Text(this.message) 
      Button() { 
        Text('获取位置') 
          .fontSize(50) 
      } 
      .backgroundColor(Color.Orange) 
      .width(200) 
      .height(100) 
      .onClick(() => { 
        console.log("获取位置已点击"); 
        this.getLocation(); 
        console.info('授权已成功'); 
      }) 
 
      Text("经度:" + this.lon); 
      Text("纬度:" + this.lat); 
    } 
    .width('100%').height('100%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
分享
微博
QQ
微信
回复
2024-06-04 22:30:10
相关问题
HarmonyOS 坐标系经纬度转换
1271浏览 • 1回复 待解决
HarmonyOS 有关经纬度问题
803浏览 • 1回复 待解决
HarmonyOS map kit 获取地图中心经纬度
1117浏览 • 1回复 待解决
HarmonyOS 获取经纬的案例
802浏览 • 1回复 待解决