模板版本:v0.2.2
react-native-maps-directions
[!TIP] Github 地址
[!TIP] 该库依赖react-native-maps,可参考react-native-maps文档安装
请到三方库的 Releases 发布地址查看配套的版本信息:@react-native-oh-tpl/react-native-maps-directions Releases,并下载适用版本的 tgz 。对于未发布到npm的旧版本,请参考安装指南安装tgz包。
进入到工程目录并输入以下命令:
npm install @react-native-oh-tpl/react-native-maps-directions
yarn add @react-native-oh-tpl/react-native-maps-directions
下面的代码展示了这个库的基本使用场景:
[!WARNING] 使用时 import 的库名不变。
import React, { Component } from 'react';
import { Dimensions, StyleSheet, View, Text } from 'react-native';
import MapViewDirections from 'react-native-maps-directions';
import MapView, { Marker } from "react-native-maps";
const { width, height } = Dimensions.get('window');
const ASPECT_RATIO = width / height;
const LATITUDE = 37.771707;
const LONGITUDE = -122.4053769;
const LATITUDE_DELTA = 0.0922;
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;
const MAPS_APIKEY = 'xxxxxx';
class MapExample extends Component {
constructor(props) {
super(props);
this.state = {
coordinates: [
{
latitude: 37.3317876,
longitude: -122.0054812,
},
{
latitude: 37.771707,
longitude: -122.4053769,
},
],
};
this.mapView = null;
}
onMapPress = (e) => {
this.setState({
coordinates: [
...this.state.coordinates,
e.nativeEvent.coordinate,
],
});
}
render() {
const { coordinates} = this.state;
return (
<View style={styles.container}>
<MapView
initialRegion={{
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
}}
style={StyleSheet.absoluteFill}
ref={c => this.mapView = c}
onPress={this.onMapPress}>
{this.state.coordinates.map((coordinate, index) =>
<Marker key={`coordinate_${index}`} coordinate={coordinate} />
)}
{(this.state.coordinates.length >= 2) && (
<MapViewDirections
origin={this.state.coordinates[0]}
destination={this.state.coordinates[this.state.coordinates.length-1]}
apikey={MAPS_APIKEY}
mode='BICYCLING'
strokeWidth={6}
strokeColor={"#0000FF"}
resetOnChange={true}
onStart={(params) => {
console.log(`Started routing between "${params.origin}" and "${params.destination}"`);
}}
onReady={result => {
console.log(`Distance: ${result.distance} km`)
console.log(`Duration: ${result.duration} min.`)
this.mapView.fitToCoordinates(result.coordinates, {
edgePadding: {
right: (width / 20),
bottom: (height / 20),
left: (width / 20),
top: (height / 20),
}
});
}}
onError={(errorMessage) => {
console.log('GOT AN ERROR');
}}
/>
)}
</MapView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
...StyleSheet.absoluteFillObject,
justifyContent: "flex-end",
alignItems: "center",
height: 300,
},
map: {
...StyleSheet.absoluteFillObject,
},
});
export default MapExample;
本库在HarmonyOS NEXT侧实现依赖@react-native-oh-tpl/react-native-maps 的原生端代码,如已在HarmonyOS NEXT工程中引入过该库,则无需再次引入,可跳过本章节步骤,直接使用。
如未引入请参照@react-native-oh-tpl/react-native-maps 文档的 Link 章节进行引入
要使用此库,需要使用正确的 React-Native 和 RNOH 版本。另外,还需要使用配套的 DevEco Studio 和 手机 ROM。
请到三方库相应的 Releases 发布地址查看 Release 配套的版本信息:@react-native-oh-tpl/react-native-maps-directions Releases
[!TIP] "Platform"列表示该属性在原三方库上支持的平台。
[!TIP] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
由于渲染在屏幕上的结果是
MapView.Polyline
组件,因此也支持MapView.Polyline
属性,MapView.Polyline
属性参见react-native-maps。
Name | Description | Type | Required | Default | Platform | HarmonyOS Support |
---|---|---|---|---|---|---|
origin |
The origin location to start routing from. | LatLngor String |
true | iOS/Android | yes | |
destination | The destination location to start routing to. | LatLngor String |
true | iOS/Android | yes | |
apikey | Your Google Maps API Key or HUWEI Map API Key. HUWEI Map API Key see here | String | true | iOS/Android | yes | |
waypoints | Array of waypoints to use between origin and destination. | [LatLng or String ] |
false | [] | iOS/Android | no |
language | The language to use when calculating directions. See here for more info. | String | false | en | iOS/Android | no |
mode | Which transportation mode to use when calculating directions. Allowed values are "DRIVING" , "BICYCLING" , "WALKING" , and "TRANSIT" . |
String | false | "DRIVING" | iOS/Android | partially (Support "DRIVING" , "BICYCLING" , "WALKING" ) |
resetOnChange | Tweak if the rendered MapView.Polyline should reset or not when calculating the route between origin and destionation . Set to false if you see the directions line glitching. |
boolean | false | true | iOS/Android | yes |
optimizeWaypoints | Set it to true if you would like Google Maps to re-order all the waypoints to optimize the route for the fastest route. Please be aware that if this option is enabled, you will be billed at a higher rate by Google as stated here. | boolean | false | false | iOS/Android | no |
splitWaypoints | Directions API has a limit of 10 or 25 (depends on the billing plan) waypoints per route. When exceeding this limit you will be billed at a higher reate by Google. Set this to true if you would like to automatically split waypoints into multiple routes, thus bypassing this waypoints limit. |
boolean | false | false | iOS/Android | no |
directionsServiceBaseUrl | Base URL of the Directions Service (API) you are using. | string | false | (Google's) | iOS/Android | yes |
region | If you are using strings for origin or destination, sometimes you will get an incorrect route because Google Maps API needs the region where this places belong to. See here for more info. | String | false | iOS/Android | no | |
precision | The precision level of detail of the drawn polyline. Allowed values are "high", and "low". Setting to "low" will yield a polyline that is an approximate (smoothed) path of the resulting directions. Setting to "high" may cause a hit in performance in case a complex route is returned. | String | false | "low" | iOS/Android | no |
timePrecision | The timePrecision to get Realtime traffic info. Allowed values are "none", and "now". Defaults to "none". | String | false | "none" | iOS/Android | no |
channel | If you include the channel parameter in your requests, you can generate a Successful Requests report that shows a breakdown of your application's API requests across different applications that use the same client ID (such as externally facing access vs. internally facing access). | String | false | null | iOS/Android | no |
Event Name | Description | Type | Required | Platform | HarmonyOS Support |
---|---|---|---|---|---|
onStart |
Callback that is called when the routing has started. | Function | false | iOS/Android | yes |
onReady |
Callback that is called when the routing has succesfully finished. note: distance returned in kilometers and duration in minutes. | Function | false | iOS/Android | yes |
onError |
Callback that is called in case the routing has failed. | Function | false | iOS/Android | yes |
onStart(params:Object):void
Name | Description | Type | Required | Platform | HarmonyOS Support |
---|---|---|---|---|---|
params | 回调函数返回值,包含起点、终点和中间点的经纬度信息。数据格式为:{ origin, destination, waypoints: [] } | Object | True | iOS/Android | yes |
onReady(result:Object):void
Name | Description | Type | Required | Platform | HarmonyOS Support |
---|---|---|---|---|---|
result | 回调函数返回值,包含起点到终点的距离、路程时间、和途径路径的经纬度等信息。数据格式:{ distance: Number, duration: Number, coordinates: [], fare: Object, waypointOrder: [[]] } | Object | True | iOS/Android | yes |
onError(errorMessage:Object):void
Name | Description | Type | Required | Platform | HarmonyOS Support |
---|---|---|---|---|---|
errorMessage | 回调函数返回值,访问directions rest api的报错信息 | Object | True | iOS/Android | yes |
本项目基于 The MIT License (MIT) ,请自由地享受和参与开源。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。