react-native-amap-zmt插件的使用(iOS)

功能:

通過(guò)高德地圖SDK實(shí)現(xiàn)定位、返回地理位置經(jīng)緯度坐標(biāo)、關(guān)鍵字檢索及周邊檢索功能。

使用步驟:

一、鏈接AMap庫(kù)

參考:https://reactnative.cn/docs/0.50/linking-libraries-ios.html#content

手動(dòng)添加:

1、添加react-native-amap-zmt插件到你工程的node_modules文件夾下
2、添加AMap庫(kù)中的.xcodeproj文件在你的工程中
3、點(diǎn)擊你的主工程文件,選擇 Build Phases,然后把剛才所添加進(jìn)去的.xcodeproj下的Products文件夾中的靜態(tài)庫(kù)文件(.a文件),拖到Link Binary With Libraries組內(nèi)。

自動(dòng)添加:
$ npm install react-native-amap-zmt --save 
或
$ yarn add react-native-amap-zmt

$ react-native link

二、開發(fā)環(huán)境配置

參考:http://lbs.amap.com/api/ios-location-sdk/guide/create-project/manual-configuration

1、引入地圖庫(kù)

TARGETS->Build Phases-> Link Binary With Libaries中點(diǎn)擊“+”按鈕,在彈出的窗口中點(diǎn)擊“Add Other”按鈕,選擇AMap插件目錄下的 MAMapKit.frameworkAMapFoundationKit.frameworkAMapSearchKit.framework 文件添加到工程中。

選擇Build Settings,搜索Search Paths,然后添加庫(kù)所在的目錄$(SRCROOT)/../node_modules/react-native-amap-zmt/ios/AMap

2、引入資源文件

需要引入的資源文件包括:AMap.bundle,其中:AMap.bundleMAMapKit.frameworkResources文件夾下,AMap.bundle資源文件中存儲(chǔ)了定位、默認(rèn)大頭針標(biāo)注視圖等圖片,可利用這些資源圖片進(jìn)行開發(fā)。

左側(cè)目錄中選中工程名,在右鍵菜單中選擇Add Files to“工程名”…,從MAMapKit.framework->Resources文件中選擇AMap.bundle文件,并勾選“Copy items if needed”復(fù)選框,單擊“Add”按鈕,將資源文件添加到工程中。

注意:2D地圖和3D地圖的資源文件是不同的,在進(jìn)行SDK切換時(shí),需要同時(shí)更換對(duì)應(yīng)的資源文件。

3、引入系統(tǒng)庫(kù)

左側(cè)目錄中選中工程名,在TARGETS->Build Phases-> Link Binary With Libaries中點(diǎn)擊“+”按鈕,在彈出的窗口中查找并選擇所需的庫(kù)(見下表),單擊“Add”按鈕,將庫(kù)文件添加到工程中。

  • JavaScriptcore.framework
  • SystemConfiguration.framework
  • CoreTelephony.framework
  • CoreLocation.framework
  • libz.tbd
  • libc++.tbd
  • libstdc++.6.0.9.tbd
  • Security.framework

三、配置plist文件

1、修改數(shù)據(jù)訪問安全

iOS9為了增強(qiáng)數(shù)據(jù)訪問安全,將所有的http請(qǐng)求都改為了https,為了能夠在iOS9中正常使用地圖SDK,請(qǐng)?jiān)?code>"Info.plist"中進(jìn)行如下配置,否則影響SDK的使用。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

2、開啟定位

iOS 8 - iOS 10 版本:
NSLocationWhenInUseUsageDescription表示應(yīng)用在前臺(tái)的時(shí)候可以搜到更新的位置信息。
NSLocationAlwaysUsageDescription申請(qǐng)Always權(quán)限,以便應(yīng)用在前臺(tái)和后臺(tái)(suspendterminated)都可以獲取到更新的位置數(shù)據(jù)。

iOS 11 版本:
NSLocationAlwaysAndWhenInUseUsageDescription申請(qǐng)Always權(quán)限,以便應(yīng)用在前臺(tái)和后臺(tái)(suspendterminated)都可以獲取到更新的位置數(shù)據(jù)(NSLocationWhenInUseUsageDescription也必須有)。

如果需要同時(shí)支持在iOS8-iOS10iOS11系統(tǒng)上后臺(tái)定位,建議在plist文件中同時(shí)添加NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescriptionNSLocationAlwaysAndWhenInUseUsageDescription權(quán)限申請(qǐng)。

四、簡(jiǎn)單使用

屬性
Prop Type Default Note
AMapKey string null apiKey
showTraffic bool false 是否顯示實(shí)時(shí)路況
showsCompass bool true 是否顯示指南針
zoomEnabled bool true 縮放手勢(shì)的開啟和關(guān)閉
scrollEnabled bool true 拖動(dòng)的開啟和關(guān)閉
GeoName string null 地理編碼查詢名稱
KeywordsCity string null 關(guān)鍵字檢索城市,和關(guān)鍵字檢索名稱配合使用
KeywordsName string null 關(guān)鍵字檢索名稱,和關(guān)鍵字檢索城市配合使用
AroundName string null 周邊檢索名稱
方法
Event Name Returns Notes
onGetLocation event 獲取當(dāng)前位置信息
onGeocodeSearch event 地理編碼查詢結(jié)果回調(diào)
onKeywordsSearch event 關(guān)鍵字檢索結(jié)果回調(diào)
onAroundSearch event 周邊檢索結(jié)果回調(diào)
//index.ios.js

import React, { Component } from 'react';
import Map from 'react-native-amap-zmt';

import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Dimensions,
    AlertIOS,
    TextInput,
} from 'react-native';

export default class MapView extends Component {

    constructor(){
        super();
        this.state={
            latitude: 0,
            longitude: 0,
            title:'',
            subtitle:'',
            message:'',
            GeoName:'',
            KeywordsCity:'',
            KeywordsName:'',
            AroundName:'',
        }
    }

    //獲取當(dāng)前位置信息
    _onGetLocation(event){
        this.setState({
            latitude: event['latitude'],
            longitude: event['longitude'],
            title:event['title'],
            subtitle:event['subtitle'],
            message:event['message'],
        });
    }

    _onGeocodeSearch(event){
        AlertIOS.alert(event['message'])
    }

    _onSubmitEditing(event) {
        this.setState({
        GeoName:'北京久其軟件',
        // AroundName:'肯德基',
        // KeywordsName:event.nativeEvent.text,
        // KeywordsCity:'北京',

        });
    }

    _onChangeText(text) {

    }


    render() {
        return (
            <View style={styles.container}>

            <TextInput style={styles.search} placeholder="搜索"
                onSubmitEditing={this._onSubmitEditing.bind(this)}
                returnKeyType="search"
                placeholderTextColor="#494949" autoFocus={false}
                onChangeText={this._onChangeText}/>

            <Text style={{marginTop:10}}>{'緯度='+this.state.latitude}</Text>
            <Text style={{marginTop:5}}>{'經(jīng)度='+this.state.longitude}</Text>
            <Text style={{marginTop:5}}>{'title='+this.state.title}</Text>
            <Text style={{marginTop:5}}>{'subtitle='+this.state.subtitle}</Text>
            <Text style={{marginTop:5}}>{'結(jié)果='+this.state.message}</Text>

            <Map style={styles.map}
                AMapKey="04a46bd238047b8bf33fba5723ecad72"
                onGetLocation={(event)=>{this._onGetLocation(event)}}
                showTraffic={true}
                scrollEnabled={false}

                GeoName={this.state.GeoName}
                onGeocodeSearch={(event)=>{this._onGeocodeSearch(event)}}

                KeywordsCity={this.state.KeywordsCity}
                KeywordsName={this.state.KeywordsName}
                onKeywordsSearch={(event)=>{}}

                AroundName={this.state.AroundName}
                onAroundSearch={(event)=>{}}
            >
            </Map>

            </View>
        );
    }

}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    map: {
        marginTop:10,
        height:400,
        width:Dimensions.get('window').width - 20,
    },
    search: {
        width:Dimensions.get('window').width - 20,
        height: 35,
        borderWidth: 1,
        borderColor: '#ccc',
        borderRadius:3,
        fontSize:15
    }

});

效果展示


github鏈接:
https://github.com/zhoumeitong/react-native-amap-zmt

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 對(duì)自己好一點(diǎn),你說(shuō)南方有你的夢(mèng)想可是你卻背上行囊去了北方。今天是挑戰(zhàn)第十一天,我們能做的太少,總是實(shí)力撐不起情懷。...
    小烈吶閱讀 484評(píng)論 0 0
  • 我夢(mèng)見,她坐在我的床頭,纖纖素手輕柔的撫弄我的頭發(fā),那愛撫像是在彈撥美妙的樂器。我望著她的臉龐,雙眸淚光閃閃,難言...
    蔚藍(lán)中搖曳的風(fēng)箏閱讀 212評(píng)論 0 0