React Native 高德地圖組件的使用(react-native-amap3d)

這篇文章主要介紹 RN 高德地圖組件 react-native-amap3d役耕,安裝過程請參考 README瓜贾。

基本使用

import {MapView} from 'react-native-amap3d'

render() {
  return <MapView style={StyleSheet.absoluteFill}/>
}

設(shè)置地圖狀態(tài)

所謂的地圖狀態(tài)包括:中心坐標(coordinate)蠕嫁、縮放級別(zoomLevel)冰寻、傾斜度(titl)觉阅、旋轉(zhuǎn)角度(rotation)稻爬、顯示區(qū)域(region)嗜闻。

通過 coordinate、zoomLevel 設(shè)置顯示區(qū)域

<MapView
  style={StyleSheet.absoluteFill}
  coordinate={{
    latitude: 39.90980,
    longitude: 116.37296,
  }}
  zoomLevel={18}
  tilt={45}
  showsIndoorMap
/>

通過 region 設(shè)置顯示區(qū)域

region 由中心坐標和經(jīng)緯跨度組成桅锄,相對于 zoomLevel琉雳,region 可以精確控制顯示邊界样眠。

<MapView
  style={StyleSheet.absoluteFill}
  region={{
    latitude: 39.90980,
    longitude: 116.37296,
    latitudeDelta: 0.1,
    longitudeDelta: 0.1,
  }}
/>

動畫過渡

通過屬性控制的地圖狀態(tài)切換會顯得比較生硬,如果希望地圖狀態(tài)切換是動畫過渡的翠肘,可以使用 animateTo 方法檐束。

<MapView ref={ref => this._mapView} style={StyleSheet.absoluteFill}/>

this._mapView.animateTo({
  tilt: 45,
  rotation: 90,
  zoomLevel: 18,
  coordinate: {
    latitude: 39.97837,
    longitude: 116.31363,
  }
})

5種地圖模式

目前高德地圖支持5種地圖模式:

  • 標準(standard)
  • 衛(wèi)星(satellite)
  • 導(dǎo)航(navigation)
  • 公交(bus)
  • 夜間(night)
<MapView
  style={StyleSheet.absoluteFill}
  zoomLevel={14}
  mapType='satellite'
/>
衛(wèi)星地圖
導(dǎo)航地圖
公交地圖
夜間地圖

地圖事件

目前支持的地圖事件有:

  • onPress 點按事件
  • onLongPress 長按事件
  • onLocation 定位事件
  • onStatusChange 地圖狀態(tài)變化事件,變化過程會一直調(diào)用
  • onStatusChangeComplete 地圖狀態(tài)變化結(jié)束事件

可以通過 event.nativeEvent 獲取事件傳遞過來的數(shù)據(jù)

定位

通過 locationEnabled 控制是否啟用定位束倍,通過 locationIntervaldistanceFilter 可以控制定位頻次被丧。

<MapView
  style={StyleSheet.absoluteFill}
  locationEnabled
  locationInterval={10000}
  distanceFilter={10}
  onLocation={({nativeEvent}) =>
    console.log(`${nativeEvent.latitude}, ${nativeEvent.longitude}`)}
/>

添加標注點

默認標注點

<MapView style={StyleSheet.absoluteFill}>
  <Marker
    active
    title='這是一個標注點'
    color='red'
    description='Hello world!'
    coordinate={{
      latitude: 39.806901,
      longitude: 116.397972,
    }}
  />
</MapView>

可拖拽的標注點

<Marker
  draggable
  onDragEnd={({nativeEvent}) =>
    console.log(`${nativeEvent.latitude}, ${nativeEvent.longitude}`)}
  coordinate={{
    latitude: 39.806901,
    longitude: 116.397972,
  }}
/>

自定義圖片

可以通過 image 屬性設(shè)置標注點圖片,image 的值是圖片資源的名字绪妹,對于 android 是 drawable甥桂,對于 ios 是 Images.xcassets。

<Marker
  title='自定義圖片'
  image='flag'
  coordinate={{
    latitude: 39.806901,
    longitude: 116.397972,
  }}
/>

自定義 View

除了 image邮旷,還可以用 View 作為標注點黄选,更自由的控制標注點的顯示。

<Marker
  active
  title='自定義 View'
  coordinate={{
    latitude: 39.806901,
    longitude: 116.397972,
  }}
  icon={() =>
    <View style={style.marker}>
      <Text style={style.markerText}>{(new Date()).toLocaleTimeString()}</Text>
    </View>
  }
/>

const style = StyleSheet.create({
  marker: {
    backgroundColor: '#009688',
    alignItems: 'center',
    borderRadius: 5,
    padding: 5,
  },
  markerText: {
    color: '#fff',
  },
})

自定義彈出窗口

<Marker
  active
  coordinate={{
    latitude: 39.806901,
    longitude: 116.397972,
  }}
>
  <View style={style.infoWindow}>
    <Text>自定義彈出窗口</Text>
  </View>
</Marker>

const style = StyleSheet.create({
  infoWindow: {
    backgroundColor: '#8bc34a',
    padding: 10,
    borderRadius: 10,
    elevation: 4,
    borderWidth: 2,
    borderColor: '#689F38',
  },
})

繪制線段

<MapView zoomLevel={10} style={StyleSheet.absoluteFill}>
  <Polyline
    width={10}
    color='rgba(255, 0, 0, 0.5)'
    coordinates={[
      {
        latitude: 40.006901,
        longitude: 116.097972,
      },
      {
        latitude: 40.006901,
        longitude: 116.597972,
      },
      {
        latitude: 39.706901,
        longitude: 116.597972,
      },
    ]}
  />
</MapView>

熱力圖

import {MapView, HeatMap} from 'react-native-amap3d'

_coordinates = (new Array(200)).fill(0).map(i => ({
  latitude: 39.5 + Math.random(),
  longitude: 116 + Math.random(),
}))

<MapView zoomLevel={9} style={StyleSheet.absoluteFill}>
  <HeatMap
    opacity={0.8}
    radius={20}
    coordinates={this._coordinates}/>
</MapView>

海量點

批量添加的 Marker 數(shù)量過多會出現(xiàn)性能問題廊移,這時可以考慮用海量點(MultiPoint)糕簿。

import {MapView, MultiPoint} from 'react-native-amap3d'

_points = Array(1000).fill(0).map(i => ({
  latitude: 39.5 + Math.random(),
  longitude: 116 + Math.random(),
}))

_onItemPress = point => Alert.alert(this._points.indexOf(point).toString())

<MapView zoomLevel={10} style={StyleSheet.absoluteFill}>
  <MultiPoint
    image='point'
    points={this._points}
    onItemPress={this._onItemPress}
  />
</MapView>

更多示例

請參考 examples

有問題歡迎在 issues 里討論狡孔,評論區(qū)不再回復(fù)懂诗。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市苗膝,隨后出現(xiàn)的幾起案子殃恒,更是在濱河造成了極大的恐慌,老刑警劉巖辱揭,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件离唐,死亡現(xiàn)場離奇詭異,居然都是意外死亡问窃,警方通過查閱死者的電腦和手機亥鬓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來域庇,“玉大人嵌戈,你說我怎么就攤上這事√螅” “怎么了熟呛?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長尉姨。 經(jīng)常有香客問我庵朝,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任九府,我火速辦了婚禮椎瘟,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘昔逗。我一直安慰自己降传,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布勾怒。 她就那樣靜靜地躺著,像睡著了一般声旺。 火紅的嫁衣襯著肌膚如雪笔链。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天腮猖,我揣著相機與錄音鉴扫,去河邊找鬼。 笑死澈缺,一個胖子當(dāng)著我的面吹牛坪创,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播姐赡,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼莱预,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了项滑?” 一聲冷哼從身側(cè)響起依沮,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎枪狂,沒想到半個月后危喉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡州疾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年辜限,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片严蓖。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡薄嫡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出谈飒,到底是詐尸還是另有隱情岂座,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布杭措,位于F島的核電站费什,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鸳址,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一瘩蚪、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧稿黍,春花似錦疹瘦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至酣栈,卻和暖如春险胰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背矿筝。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工起便, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人窖维。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓榆综,卻偏偏與公主長得像,于是被迫代替她去往敵國和親铸史。 傳聞我的和親對象是個殘疾皇子鼻疮,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,500評論 2 359

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