react-native 獲取經(jīng)緯度工具類 通過高德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 {


    // 構(gòu)造
    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}, 經(jīng)度 = ${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;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末噪径,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子梗顺,更是在濱河造成了極大的恐慌车摄,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件变屁,死亡現(xiàn)場離奇詭異意狠,居然都是意外死亡,警方通過查閱死者的電腦和手機环戈,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門院塞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人拦止,你說我怎么就攤上這事∫樟幔” “怎么了鞠抑?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長秒梳。 經(jīng)常有香客問我,道長酪碘,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任徙赢,我火速辦了婚禮探越,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘枕屉。我一直安慰自己鲤氢,他們只是感情好,可當我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布哨颂。 她就那樣靜靜地躺著相种,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蚂子。 梳的紋絲不亂的頭發(fā)上缭黔,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天馏谨,我揣著相機與錄音,去河邊找鬼惧互。 笑死,一個胖子當著我的面吹牛拨与,可吹牛的內(nèi)容都是我干的艾猜。 我是一名探鬼主播买喧,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼淤毛,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了低淡?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤何荚,失蹤者是張志新(化名)和其女友劉穎纸颜,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體唠倦,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡涮较,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年狂票,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片闺属。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡掂器,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出国瓮,到底是詐尸還是另有隱情,我是刑警寧澤禁漓,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布孵睬,位于F島的核電站,受9級特大地震影響掰读,放射性物質(zhì)發(fā)生泄漏撩穿。R本人自食惡果不足惜谒撼,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望抵皱。 院中可真熱鬧辩蛋,春花似錦、人聲如沸悼院。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽颖医。三九已至,卻和暖如春熔萧,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背贮缕。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工俺榆, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人肋演。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像烂琴,于是被迫代替她去往敵國和親爹殊。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,322評論 25 707
  • 持續(xù)更新中...... 一套企業(yè)級的 UI 設計語言和 React 實現(xiàn)奸绷。 https://mobile.ant....
    日不落000閱讀 5,728評論 0 35
  • afinalAfinal是一個android的ioc梗夸,orm框架 https://github.com/yangf...
    passiontim閱讀 15,440評論 2 45
  • 在iOS8.X上一個視圖調(diào)用 [self layoutIfNeeded] 方法后,第一個子視圖y值發(fā)生改變 ps:...
    學習無底閱讀 437評論 0 0
  • 人号醉,就要用時間擠著勞作反症,有壓力才有動力辛块。月初月末的事情2天完結(jié),我都有些佩服自己…… 說起來有點奇怪铅碍,...
    指揮官閱讀 194評論 0 5