Giter Site home page Giter Site logo

qiuxiang / react-native-amap3d Goto Github PK

View Code? Open in Web Editor NEW
1.2K 26.0 323.0 1.69 MB

react-native 高德地图组件,使用最新 3D SDK,支持 Android + iOS

Home Page: https://qiuxiang.github.io/react-native-amap3d/api/

License: MIT License

JavaScript 1.18% Objective-C 4.41% Ruby 1.28% Kotlin 34.45% TypeScript 40.65% Swift 18.04%
react-native maps amap mapview

react-native-amap3d's Introduction

react-native-amap3d

注意:该项目目前只维护,不加新功能。

react-native 高德地图组件,使用最新 3D SDK,支持 Android + iOS,受 react-native-maps 启发,提供功能丰富且易用的接口。

相关项目推荐:

功能

  • 地图模式切换(常规、卫星、导航、夜间)
  • 3D 建筑、路况、室内地图
  • 内置地图控件的显示隐藏(指南针、比例尺、定位按钮、缩放按钮)
  • 手势交互控制(平移、缩放、旋转、倾斜)
  • 中心坐标、缩放级别、倾斜度的设置,支持动画过渡
  • 地图事件(onPress、onLongPress、onLocation、onCameraMove、onCameraIdle 等)
  • 地图标记(Marker)
  • 折线绘制(Polyline)
  • 多边形绘制(Polygon)
  • 圆形绘制(Circle)
  • 热力图(HeatMap)
  • 海量点(MultiPoint)
  • 点聚合(Cluster)

接口文档

https://qiuxiang.github.io/react-native-amap3d/api/

安装

npm i react-native-amap3d

添加高德 API Key

首先你需要获取高德地图 API Key:

然后你需要在显示地图前调用接口设置 API key:

import { AMapSdk } from "react-native-amap3d";
import { Platform } from "react-native";

AMapSdk.init(
  Platform.select({
    android: "c52c7169e6df23490e3114330098aaac",
    ios: "186d3464209b74effa4d8391f441f14d",
  })
);

用法

显示地图

import { MapView, MapType } from "react-native-amap3d";

<MapView
  mapType={MapType.Satellite}
  initialCameraPosition={{
    target: {
      latitude: 39.91095,
      longitude: 116.37296,
    },
    zoom: 8,
  }}
/>;

监听地图事件

import { MapView } from "react-native-amap3d";

<MapView
  onLoad={() => console.log("onLoad")}
  onPress={({ nativeEvent }) => console.log(nativeEvent)}
  onCameraIdle={({ nativeEvent }) => console.log(nativeEvent)}
/>;

添加标记

其中 icon 支持 ImageSource

同时支持 children 作为标记图标。

import { MapView, Marker } from "react-native-amap3d";

<MapView>
  <Marker
    position={{ latitude: 39.806901, longitude: 116.397972 }}
    icon={require("../images/flag.png")}
    onPress={() => alert("onPress")}
  />
  <Marker
    position={{ latitude: 39.806901, longitude: 116.297972 }}
    icon={{
      uri: "https://reactnative.dev/img/pwa/manifest-icon-512.png",
      width: 64,
      height: 64,
    }}
  />
  <Marker position={{ latitude: 39.906901, longitude: 116.397972 }}>
    <Text
      style={{
        color: "#fff",
        backgroundColor: "#009688",
        alignItems: "center",
        borderRadius: 5,
        padding: 5,
      }}
    >
      {new Date().toLocaleString()}
    </Text>
  </Marker>
</MapView>;

点聚合

Marker 数量过多(尤其是使用自定义 View 的情况下)会导致性能问题,而且显示过于密集,这时候可以用点聚合改善。

import { Cluster, MapView, Marker } from "react-native-amap3d";

const markers = Array(1000)
  .fill(0)
  .map((_, i) => ({
    position: { latitude: 39.5 + Math.random(), longitude: 116 + Math.random() },
    properties: { key: `Marker${i}` },
  }));

<MapView
  ref={(ref) => (this.mapView = ref)}
  onLoad={() => this.mapView?.moveCamera({ zoom: 8 }, 100)}
  onCameraIdle={({ nativeEvent }) => {
    this.status = nativeEvent;
    this.cluster?.update(nativeEvent);
  }}
>
  <Cluster
    ref={(ref) => (this.cluster = ref)}
    points={markers}
    renderMarker={(item) => (
      <Marker
        key={item.properties.key}
        icon={require("../images/flag.png")}
        position={item.position}
      />
    )}
  />
</MapView>;

更多示例

参考 example

常见问题

  • 尽量使用真实设备进行测试,在模拟器可能存在一些问题(常见的是 Android 模拟器因为缺少 GPU 加速而导致闪退)。
  • onLocation 没有返回定位数据通常是因为 key 不正确,或没有申请 PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION 权限

react-native-amap3d's People

Contributors

25juan avatar koppel-zhou avatar lenmoon avatar noirvoider avatar qiu800820 avatar qiuxiang avatar summer88123 avatar sunnylqm avatar xhuang3 avatar yiiu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-native-amap3d's Issues

0.47.1版本的项目,地图不显示

配置了高德地图的权限和KEY,在启动也里面添加了地图控件,但是没有显示地图出来,也没有报错。
image

  <View style={styles.container}>
    <Text style={styles.welcome}>
      Welcome to React Native!
    </Text>
    <MapView>
      <Marker
          draggable
          title='这是一个可拖拽的标记'
          onDragEnd={({nativeEvent}) =>
          console.log(`${nativeEvent.latitude}, ${nativeEvent.longitude}`)}
                  coordinate={{
          latitude: 39.91095,
          longitude: 116.37296,
        }}/>
    </MapView>
    <Text style={styles.welcome}>
      Welcome to React Native!
    </Text>
  </View>

通过State批量动态添加自定义图片Marker后,拖动地图,点位跟随地图一起移动

通过state 动态render 自定义图片Marker以后拖动地图,Marker会随着地图一同移动,有的时候放大缩小地图图片还能回到正常的坐标,我把example中的 Marker那块稍微修改了一下,贴出来。

import React, {Component} from 'react'
import {
  StyleSheet,
  Alert,
  Text,
  Image,
  View,
  TouchableOpacity,
} from 'react-native'
import {MapView, Marker} from 'react-native-amap3d'

export default class MarkerExample extends Component {
  static navigationOptions = {
    title: '添加标记',
  }
  constructor(props) {
    super(props);

    this.state = {
      list:[
        1,2,3,4,5,7,8,9,10
      ]
    };
  }
  state = {
    time: new Date(),
  }

  componentDidMount() {
    this.mounted = true
    setInterval(() => {
      if (this.mounted) {
        this.setState({time: new Date()})
      }
    }, 1000)
  }

  componentWillUnmount() {
    this.mounted = false
  }

  _handleDragEvent = ({nativeEvent}) =>
    Alert.alert(`新坐标:${nativeEvent.latitude}, ${nativeEvent.longitude}`)

  _handleInfoWindowPress = () => Alert.alert('信息窗口点击事件')

  _handleCustomInfoWindowPress = () => Alert.alert('Custom View InfoWindow onPress')

  _renderCustomMarker = () =>
    <View style={styles.customMarker}>
      <Text style={styles.markerText}>{this.state.time.toLocaleTimeString()}</Text>
    </View>

  _renderColoredMarker = () =>
    <View style={styles.customIcon}>
      <Image
        style={[styles.customIcon, styles.coloredImage]}
        source={require('../../images/marker.png')}/>
    </View>

  _renderImageMarker = () =>
    <View style={styles.customIcon}>
      <Image style={styles.customIcon} source={require('../../images/flag.png')}/>
    </View>
  _renderMarker=()=>{
    let rtnVal=[];
    this.state.list.map((item,key)=>{
      rtnVal.push(
        <Marker
          key={key}
          icon={this._renderImageMarker}
          title='自定义图片'
          description="Note the use of nativeOnly above. Sometimes you'll have some special properties that you need to expose for the native component, but don't actually want them as part of the API for the associated React component."
          coordinate={{
            latitude: 39.906901+(item*0.01),
            longitude: 116.397972+(item*0.01),
          }}
        />
      );
    })
    return rtnVal;
  }
  render() {
    return <View style={{flex:1}}>


      <MapView style={StyleSheet.absoluteFill}>
        {this._renderMarker()}
        {/* <Marker
          active
          draggable
          title={'一个可拖拽的 Marker ' + this.state.time.toLocaleTimeString()}
          onDragEnd={this._handleDragEvent}
          onInfoWindowPress={this._handleInfoWindowPress}
          coordinate={{
            latitude: 39.806901,
            longitude: 116.397972,
          }}
        />
        <Marker
          icon='green'
          onInfoWindowPress={this._handleCustomInfoWindowPress}
          coordinate={{
            latitude: 39.806901,
            longitude: 116.297972,
          }}>
          <TouchableOpacity activeOpacity={0.9} onPress={this._handleCustomInfoWindowPress}>
            <View style={styles.customInfoWindow}>
              <Text>Custom View InfoWindow</Text>
            </View>
          </TouchableOpacity>
        </Marker>
        <Marker
          icon={this._renderImageMarker}
          title='自定义图片'
          description="Note the use of nativeOnly above. Sometimes you'll have some special properties that you need to expose for the native component, but don't actually want them as part of the API for the associated React component."
          coordinate={{
            latitude: 39.906901,
            longitude: 116.397972,
          }}
        />
        <Marker
          icon={this._renderColoredMarker}
          title='自定义颜色'
          coordinate={{
            latitude: 39.806901,
            longitude: 116.457972,
          }}
        />
        <Marker
          title='Custom View Marker'
          icon={this._renderCustomMarker}
          coordinate={{
            latitude: 39.706901,
            longitude: 116.397972,
          }}
        /> */}
      </MapView>
      <View style={{height:100,width:200,backgroundColor:'red'}}>
        <TouchableOpacity style={{height:20,width:20}} onPress={()=>{
          this.setState({
            list:[11,12,13,14,15,16]
          });
        }}>
          <Text>dsadsa</Text>
        </TouchableOpacity>
      </View>
    </View>
  }
}

const styles = StyleSheet.create({
  customIcon: {
    width: 40,
    height: 40,
  },
  customInfoWindow: {
    backgroundColor: '#8bc34a',
    padding: 10,
    borderRadius: 10,
    elevation: 4,
    borderWidth: 2,
    borderColor: '#689F38',
  },
  customMarker: {
    backgroundColor: '#009688',
    alignItems: 'center',
    borderRadius: 5,
    padding: 5,
  },
  markerText: {
    color: '#fff',
  },
  coloredImage: {
    tintColor: '#e91e63',
  },
})

iOS marker active 的处理有问题

  • _handleTap 直接把 _active 设成 YES,会导致地图上多个 marker active,但实际上只能有一个 active
  • 仅当当前 active == false,且新的 active == true 是才 selectAnnotation

大佬求助,0.44.3 Android 编译失败

* What went wrong:
Execution failed for task ':app:transformNativeLibsWithMergeJniLibsForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK lib/x86/libRoadLineRebuildAPI.so
	File1: C:\Users\86839\.gradle\caches\modules-2\files-2.1\com.amap.api\3dmap\5.2.1\6e52e58811a7d3f04a23cd8233a505f79008c481\3dmap-5.2.1.jar
	File2: K:\source_code\ReactNative\pointers\node_modules\react-native-amap3d\android\build\intermediates\bundles\default\jni

Navigation.Drive

Demo 里的导航功能会闪退,并没有找到这个组件Navigation.Drive

ios 无法运行报错

"react-native": "0.47.2",
"react-native-amap3d": "^0.3.2"
运行报错
No visible @interface for 'MAMultiPolyline' declares the selector 'setDrawStyleIndexes:'

请教是否有办法通过region来设置显示区域?

react-native-amap3d中似乎是通过 中心坐标点 和 zoomLevel来设置显示区域的。请问能否通过region来设置显示区域,就像设置limitRegion一样?
不能的话,请问如何将region转换到 中心坐标点 + zoomLevel?

xcode无法运行

2017-08-15 9 35 59

按照说明:使用Pod install安装,没有运行react-native link。是全新创建的项目,RN0.46. react-native run-ios却可以正常运行,而使用xcode则报上面的错误。

定位问题

安卓权限已获取,无法获取到当前定位信息,locationEnabled已设置为true,onLocation事件,nativeEvent的值为 0。

很抱歉,“******”已停止运行。

在电脑A正常运行,在电脑B弹出该错误Alert,没有红屏,后台也没有输出错误,比较诡异。

注释掉 MapView 代码就不会出现该问题。

如果两个位置点超过了当前地图的可视范围,会显示异常

simulator screen shot 2017 8 30 11 21 01
屏幕最上方的位置点是在杭州

render() {
    return <MapView style={StyleSheet.absoluteFill}>
      <Marker
          active
          draggable
          title={'一个可拖拽的标记 ' + this.state.time.toLocaleTimeString()}
          onDragEnd={this._onDragEvent}
          onInfoWindowPress={this._onInfoWindowPress}
          coordinate={this._coordinates[0]}
      />
      <Marker
          icon='green'
          onInfoWindowPress={this._onCustomInfoWindowPress}
          coordinate={this._coordinates[1]}>
        <TouchableOpacity activeOpacity={0.9} onPress={this._onCustomInfoWindowPress}>
          <View style={styles.customInfoWindow}>
            <Text>Custom View InfoWindow</Text>
          </View>
        </TouchableOpacity>
      </Marker>
      <Marker
          icon={() =>
              <View style={styles.customIcon}>
                <Image style={styles.customIcon} />
              </View>
          }
          title='自定义图片'
          description="Sometimes you'll have some special properties that you need to expose for the native component, but don't actually want them as part of the API for the associated React component."
          coordinate={this._coordinates[2]}
      />
      <Marker
          title='自定义标记'
          icon={() =>
              <View style={styles.customMarker}>
                <Text style={styles.markerText}>{this.state.time.toLocaleTimeString()}</Text>
              </View>
          }
          coordinate={{latitude:30.182159,longitude:120.139843}}
      />
    </MapView>
  }

rn升级到0.47.1后报错

e: G:\MyProject\node_modules\react-native-amap3d\android\src\main\java\cn\qiuxiang\react\amap3d\AMap3DPackage.kt: (10, 5): 'createJSModules' overrides nothing
:react-native-amap3d:compileReleaseKotlin FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':react-native-amap3d:compileReleaseKotlin'.

Compilation error. See log for more details

一个路由导致的问题。。。

使用 router-flux 作为路由(3.X) rn 版本 0.44
使用了 tab 其中两个tab页面都使用了地图
然后 页面二使用地图时 页面一已存在的地图会覆盖在页面二的地图上
如果页面二没有用地图的话 页面一的地图不会覆盖到页面二(这才是正常情况。。。)
。。。。。 具体请看图 请各位大大分析一下 thanks
ty

关于 配置的问题

各位大佬 我想问一下 如果我安卓react-native link iOS不用CocoaPods 是不是也行 iOS 也不用 手动配置了 而且 看官网手动配置 整个人都不好了 这几天 为地图发愁 能不能 给一份 这个插件的 配置文档 拜托了 辛苦了各位

Apple Mach-O Linker Error Group

Xcode8.3 编译报错 Apple Mach-O Linker Error Group ,clang: error: linker command failed with exit code 1 (use -v to see invocation)

setState之后,貌似地图会重载

我是自定义的InfoWindow,在Marker的onPress事件里面去从网络获取当前选中的marker的信息,然后setState更新一个对象,用于在InfoWindow里显示用,但是在setState之后,Marker会闪一下,InfoWindow会消失,导致获取的数据无法显示出来

BUILD FAILED

Installing build/Build/Products/Debug-iphonesimulator/Conference.app
An error was encountered processing the command (domain=NSPOSIXErrorDomain, code=22):
Failed to install the requested application
The bundle identifier of the application could not be determined.
Ensure that the application's Info.plist contains a value for CFBundleIdentifier.
Print: Entry, ":CFBundleIdentifier", Does Not Exist

Command failed: /usr/libexec/PlistBuddy -c Print:CFBundleIdentifier build/Build/Products/Debug-iphonesimulator/Conference.app/Info.plist
Print: Entry, ":CFBundleIdentifier", Does Not Exist

导航功能问题

导航功能在 iOS ,Android 两端还有那些东西需要完善??

安卓组件引用没有问题,但只要一开始加载地图就cash

react-native 0.44.3
platform: android
npm: 0.2.1

模拟器: Android 7.0 Nexus_5X_API_24
以下为android studio 的 monitor返回的error信息
07-19 15:59:30.775 5944-5985/com.hydcarrier E/AndroidRuntime: FATAL EXCEPTION: GLThread 308 Process: com.hydcarrier, PID: 5944 java.lang.RuntimeException: createContext failed: EGL_SUCCESS at android.opengl.GLSurfaceView$EglHelper.throwEglException(GLSurfaceView.java:1205) at android.opengl.GLSurfaceView$EglHelper.throwEglException(GLSurfaceView.java:1196) at android.opengl.GLSurfaceView$EglHelper.start(GLSurfaceView.java:1046) at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1416) at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1253)

测试项目为刚刚init的0.44.3,无其他组件影响

尝试过如下操作:

  1. 删除./node_modules重新install
  2. 删除./android/build目录
  3. 删除./android/app/build目录

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.