# react-native-amap-geolocation **Repository Path**: kunyuan-hongke/react-native-amap-geolocation ## Basic Information - **Project Name**: react-native-amap-geolocation - **Description**: react-native-amap-geolocation - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-02-25 - **Last Updated**: 2025-06-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README > 模板版本:v0.2.2

@ohmi/react-native-amap-geolocation

Supported platforms License

> [!TIP] [Gitee 地址](https://e.gitee.com/kunyuan-hongke/projects/723653/repos/kunyuan-hongke/react-native-amap-geolocation/sources) ## 介绍 该项目基于[react-native-amap-geolocation](https://github.com/qiuxiang/react-native-amap-geolocation)开发。 ## 安装与使用 请到三方库的 Releases 发布地址查看配套的版本信息:[@ohmi/react-native-amap-geolocation Releases](https://e.gitee.com/kunyuan-hongke/projects/723653/repos/kunyuan-hongke/react-native-amap-geolocation/releases) 。对于未发布到npm的旧版本,请参考[安装指南](https://gitee.com/react-native-oh-library/usage-docs/blob/master/zh-cn/tgz-usage.md)安装tgz包。 进入到工程目录并输入以下命令: #### **npm** ```bash npm install @ohmi/react-native-amap-geolocation ``` #### **yarn** ```bash yarn add @ohmi/react-native-amap-geolocation ``` 下面的代码展示了这个库的基本使用场景: > [!WARNING] 使用时 import 的库名不变。 ```js import * as React from "react"; import { Button, Platform, ScrollView, StyleSheet, Text, View, } from "react-native"; import { Geolocation, setInterval, addLocationListener, setAllowsBackgroundLocationUpdates, setGeoLanguage, setDistanceFilter, setDesiredAccuracy, init, setNeedAddress, start, stop, setLocationTimeout, setOnceLocation, GeoLanguage } from "react-native-amap-geolocation"; const style = StyleSheet.create({ body: { padding: 16, paddingTop: Platform.OS === "ios" ? 48 : 16, }, button: { flexDirection: "column", marginRight: 8, marginBottom: 5, marginTop: 5 }, result: { fontFamily: Platform.OS === "ios" ? "menlo" : "monospace", }, }); class AmapGeoLocationDemo extends React.Component { state = { location: null, needAddressText: "setNeedAddress(false)", language: "setLanguage(chinese)", interval: "setInterval(默认2000)", onceText: "setOnceLocation(false) 设置单次定位", startText: "start 持续定位", timeoutText: "setLocationTimeout 设置请求定位超时时间",distanceText:"setDistanceFilter(0) 设置定位的最小更新距离" }; watchId = 0; needAddress = true; currLanguage = GeoLanguage.ZH; currInterval = false; onceLocationJudge = false; timeout5000 = false; distanseJudge = true; init = () => { console.log("rn AMapLocationManagerImpl init"); init("5f7389b845cbd89b1c32e24f526728f4").then(() => { console.log("rn AMapLocationManagerImpl init success"); }); }; addLocationListener = () => { console.log("rn AMapLocationManagerImpl addLocationListener"); addLocationListener((locationData) => { console.log("rn AMapLocationManagerImpl callback success:" + JSON.stringify(locationData, null, 2)); let location = locationData; this.setState({ location: location }); }); }; setDistanceFilter = () => { console.log("rn AMapLocationManagerImpl setDistanceFilter"); this.distanseJudge = !this.distanseJudge; if (this.distanseJudge) { setDistanceFilter(0); this.setState({ distanceText: "setDistanceFilter(0) 设置定位的最小更新距离" }); } else { setDistanceFilter(1); this.setState({ distanceText: "setDistanceFilter(1) 设置定位的最小更新距离" }); } }; setLocationTimeout = () => { console.log("rn AMapLocationManagerImpl setLocationTimeout"); this.timeout5000 = !this.timeout5000; if (this.timeout5000) { setLocationTimeout(5000); this.setState({ timeoutText: "setLocationTimeout(5000) 设置请求定位超时时间" }); } else { setLocationTimeout(10000); this.setState({ timeoutText: "setLocationTimeout(10000) 设置请求定位超时时间" }); } }; start = () => { console.log("rn AMapLocationManagerImpl start"); start(); }; stop = () => { console.log("rn AMapLocationManagerImpl stop"); stop(); } onceLocation = () => { this.onceLocationJudge = !this.onceLocationJudge; if (this.onceLocationJudge) { setOnceLocation(true); this.setState({ onceText: "setOnceLocation(true) 设置单次定位", startText: "start 单次定位" }); } else { setOnceLocation(false); this.setState({ onceText: "setOnceLocation(false) 设置单次定位", startText: "start 持续定位" }); } console.log("rn AMapLocationManagerImpl current onceLocation:" + this.onceLocationJudge); }; setInterval = () => { if (this.currInterval) { setInterval(2000); this.setState({ interval: "setInterval:2000" }); } else { setInterval(10000); this.setState({ interval: "setInterval:10000" }); } this.currInterval = !this.currInterval; } setNeedAddress = () => { this.needAddress = !this.needAddress; console.log("rn AMapLocationManagerImpl setNeedAddress:" + this.needAddress); if (this.needAddress) { setNeedAddress(true); this.setState({ needAddressText: "setNeedAddress(true)" }); } else { setNeedAddress(false); this.setState({ needAddressText: "setNeedAddress(false)" }); } } setLanguage = () => { // default = 0,chinese = 1, engilish=2; console.log("rn AMapLocationManagerImpl setLanguage"); if (this.currLanguage == GeoLanguage.ZH) { this.currLanguage = GeoLanguage.EN; setGeoLanguage(GeoLanguage.EN); this.setState({ language: "setLanguage(engilish)" }); } else { this.currLanguage = GeoLanguage.ZH; setGeoLanguage(GeoLanguage.ZH); this.setState({ language: "setLanguage(chinese)" }); } } updateLocationState(location: any) { console.log("rn AMapLocationManagerImpl updateLocationState"); if (location) { this.setState({ location: location }); console.log(location); } } getCurrentPosition = () => { Geolocation.getCurrentPosition( (position) => this.updateLocationState(position), (error) => this.updateLocationState(error) ); }; watchPosition = () => { if (!this.watchId) { this.watchId = Geolocation.watchPosition( (position) => this.updateLocationState(position), (error) => this.updateLocationState(error) ); console.log("rn AMapLocationManagerImpl watchPosition watchId:" + this.watchId); } }; clearWatch = () => { if (this.watchId) { console.log("rn AMapLocationManagerImpl clearWatch watchId:" + this.watchId); Geolocation.clearWatch(this.watchId); this.watchId = 0; } stop(); this.setState({ location: null }); }; render() { const location = this.state.location; const needAddressText = this.state.needAddressText; const language = this.state.language; const currInterval = this.state.interval; const onceText = this.state.onceText; const startText = this.state.startText; const timeoutText = this.state.timeoutText; const distanceText = this.state.distanceText; return (