在反应本机地图上绘制多边形

时间:2020-07-31 07:50:36

标签: react-native react-native-maps

我正在使用react-native-maps。有什么办法可以在react-native-maps中绘制多边形并获取顶点坐标?

2 个答案:

答案 0 :(得分:0)

这是我代码中的摘录


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


render: 

 {directions && (
            <MapView.Polyline coordinates={directions} strokeWidth={4} />
 )}

我正在使用此功能来获取折线:

export const getDirections = async (pickup, dropoff) => {
    let response = await callMapsAPI(pickup, dropoff)
    // console.log("Cords of direction res: ", response);
    if (response) {
        // let respJson = await response.json();
        // console.log("Cords of direction res json: ", respJson);
        let points = Polyline.decode(response.data.routes[0].overview_polyline.points);
        // console.log("Cords of direction res points: ", points);
        let coords = points.map((point, index) => {
            return {
                latitude: point[0],
                longitude: point[1]
            }
        })
        return coords
    } else return null
}

哪里

import Polyline from '@mapbox/polyline';

答案 1 :(得分:0)

对于 React Native,你可以使用 使用 PanResponder 跟踪 x y 并转换为纬度和经度。您可以在此处查看:https://github.com/dev-event/react-native-maps-draw