React Native 高德地图组件的使用(react-native-amap3d)

这篇文章主要介绍 RN 高德地图组件 react-native-amap3d,安装过程请参考 README。

基本使用

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

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

设置地图状态

所谓的地图状态包括:中心坐标(coordinate)、缩放级别(zoomLevel)、倾斜度(titl)、旋转角度(rotation)、显示区域(region)。

通过 coordinate、zoomLevel 设置显示区域

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

通过 region 设置显示区域

region 由中心坐标和经纬跨度组成,相对于 zoomLevel,region 可以精确控制显示边界。

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

动画过渡

通过属性控制的地图状态切换会显得比较生硬,如果希望地图状态切换是动画过渡的,可以使用 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)
  • 卫星(satellite)
  • 导航(navigation)
  • 公交(bus)
  • 夜间(night)
<MapView
  style={StyleSheet.absoluteFill}
  zoomLevel={14}
  mapType='satellite'
/>
卫星地图
导航地图
公交地图
夜间地图

地图事件

目前支持的地图事件有:

  • onPress 点按事件
  • onLongPress 长按事件
  • onLocation 定位事件
  • onStatusChange 地图状态变化事件,变化过程会一直调用
  • onStatusChangeComplete 地图状态变化结束事件

可以通过 event.nativeEvent 获取事件传递过来的数据

定位

通过 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 属性设置标注点图片,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 数量过多会出现性能问题,这时可以考虑用海量点(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 里讨论,评论区不再回复。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容