react-native 获取经纬度工具类 通过高德SDK

安装依赖:

package.json:

  "dependencies": {
    "react-native-smart-amap-location": "git+https://github.com/ribuluo000/react-native-smart-amap-location.git"
  },

re.

https://github.com/ribuluo000/react-native-smart-amap-location


工具类:

AMapLocationUtil.js

/**
 * Created by nick on 2017/6/27.
 */
import React from "react";
import { Alert, NativeAppEventEmitter } from "react-native";

import AMapLocation from "react-native-smart-amap-location";

/**
 *  how to use:
 * this.amapLocationUtil = new AMapLocationUtil({_onRequestLocationOk:()=>{alert(22)}});
 this.amapLocationUtil._showLocation();
 */
export default class AMapLocationUtil {


    // 构造
    constructor(props) {
        this.props = props;
    }

    static propTypes = {
        _onRequestLocationOk: React.PropTypes.func,
    };

    static defaultProps = {
        _onRequestLocationOk: () => {
        },
    };

    componentDidMount() {
        let amapOptions = null;
        amapOptions = {
            onceLocation: true,
        };

        AMapLocation.init(amapOptions); //使用默认定位配置
        this.listenerAMap = NativeAppEventEmitter.addListener('amap.location.onLocationResult',
            (result) => this._onLocationResult(result)
        );
    }

    componentWillUnmount() {
        //停止并销毁定位服务
        AMapLocation.cleanUp();
        this.listenerAMap && this.listenerAMap.remove();
    }

    _onLocationResult(result) {
        this.componentWillUnmount();
        console.log(result);
        if (result.error) {
            Alert.alert(`错误代码: ${result.error.code}, 错误信息: ${result.error.localizedDescription}`);
            TmpDataUtil.curLatitude = 0;
            TmpDataUtil.curLongitude = 0;
        }
        else {
            if (result.formattedAddress) {
                // Alert.alert(`格式化地址 = ${result.formattedAddress}`);
            }
            else {
                // Alert.alert(`纬度 = ${result.coordinate.latitude}, 经度 = ${result.coordinate.longitude}`);
            }

            TmpDataUtil.curLatitude = result.coordinate.latitude;
            TmpDataUtil.curLongitude = result.coordinate.longitude;


        }

        if (TmpDataUtil.isRequestLocation) {
            TmpDataUtil.isRequestLocation = false;
            this.props._onRequestLocationOk && this.props._onRequestLocationOk();
        }


    }

    //单次定位并返回逆地理编码信息
    _showReGeocode() {
        AMapLocation.getReGeocode();
    }

    //单次定位并返回地理编码信息
    _showLocation() {
        this.componentWillUnmount();
        this.componentDidMount();

        if (false == TmpDataUtil.isRequestLocation) {
            TmpDataUtil.isRequestLocation = true;
            setTimeout(() => {
                this.componentWillUnmount();
                TmpDataUtil.isRequestLocation = false;
            }, 15000);
            AMapLocation.getLocation();
        }

    }

}

TmpDataUtil.js


var TmpDataUtil = {

    isRequestLocation: false,
    curLatitude: '',
    curLongitude: '',

};

module.exports = TmpDataUtil;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,967评论 25 708
  • 持续更新中...... 一套企业级的 UI 设计语言和 React 实现。 https://mobile.ant....
    日不落000阅读 6,117评论 0 35
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,933评论 2 45
  • 在iOS8.X上一个视图调用 [self layoutIfNeeded] 方法后,第一个子视图y值发生改变 ps:...
    学习无底阅读 480评论 0 0
  • 人,就要用时间挤着劳作,有压力才有动力。月初月末的事情2天完结,我都有些佩服自己…… 说起来有点奇怪,...
    指挥官阅读 242评论 0 5

友情链接更多精彩内容