javascript - 如何在具有多点(纬度和经度)的 react native map 上绘制多边形?

标签 javascript reactjs react-native react-native-maps

我一直在关注这个documentation用于在 map 上绘制多边形。现在我尝试从 API 获取纬度和经度的值并绘制它。对于少量的值,这不是问题,因为我只是使用状态直接写的。

下面是我的渲染函数

import React, { Component } from 'react';
import { View, Text, StyleSheet, Dimensions } from 'react-native';
import { Card, Button } from 'react-native-elements';
import { MapView } from 'expo';
import { connect } from 'react-redux';

const window = Dimensions.get('window');

class PolygonPage extends Component {
    constructor(props) {
        super(props);

        this.state = {
            polygon: [
            {
                latitude: this.props.points.data.routeGeoJSON.features[0].geometry.coordinates[0][0][1],
                longitude: this.props.points.data.routeGeoJSON.features[0].geometry.coordinates[0][0][0]
            },
            {
                latitude: this.props.points.data.routeGeoJSON.features[0].geometry.coordinates[0][1][1],
                longitude: this.props.points.data.routeGeoJSON.features[0].geometry.coordinates[0][1][0]
            },
            {
                latitude: this.props.points.data.routeGeoJSON.features[0].geometry.coordinates[0][2][1],
                longitude: this.props.points.data.routeGeoJSON.features[0].geometry.coordinates[0][2][0]
            },
            {
                latitude: this.props.points.data.routeGeoJSON.features[0].geometry.coordinates[0][3][1],
                longitude: this.props.points.data.routeGeoJSON.features[0].geometry.coordinates[0][3][0]
            }
        ]
    };
}

onButtonPressClose = () => {
    this.props.navigation.navigate('Home');
}

render() {
    const { polygon } = this.state;
    return (
        <View style={styles.container}>
            <MapView 
                provider={this.props.provider}
                style={styles.map}
                zoomEnabled
                initialRegion={{
                    latitude: this.props.points.data.routeGeoJSON.features[0].geometry.coordinates[0][0][1],
                    longitude: this.props.points.data.routeGeoJSON.features[0].geometry.coordinates[0][0][0],
                        latitudeDelta: 0.0922,
                        longitudeDelta: 0.0421,
                    }}
            >
                <MapView.Polygon
                    coordinates={polygon}
                    fillColor="rgba(0, 200, 0, 0.5)"
                    strokeColor="rgba(0,0,0,0.5)"
                    strokeWidth={2}
                />
            </MapView>
            <View style={stylesContainer.topContainer}>
                <Card title="Route Information">
                    <View>
                        <Text>Name: {this.props.points.data.name}</Text>
                        <Text>Description: {this.props.routes.data.description}</Text>
                    </View>
                </Card>
            </View>
            <View style={stylesContainer.buttonBottomContainer}>
                    <Button
                        medium
                        title="Back"
                        backgroundColor="#94b8b8"
                        onPress={this.onButtonPressClose}
                    />
            </View> 
        </View>
    );
    }
}

PolygonPage.propTypes = {
    provider: MapView.ProviderPropType,
}; 

const styles = StyleSheet.create({
    container: {
        ...StyleSheet.absoluteFillObject,
        justifyContent: 'flex-end',
        alignItems: 'center',
    },
    map: {
        flex: 1,
        height: window.height,
        width: window.width
    }
});

function mapStateToProps({ points }) {
    return { points };
}

export default connect(mapStateToProps)(PolygonPage);

这对我有用,因为它从我从 API 获得的点渲染了一个多边形。

我的 API 响应是 JSON 格式的。这是例子

{
"timestamp": 000111,
"verb": "GET",
"object": "route",
"data": {
    "description": "describe",
    "routeGeoJSON": {
        "type": "FeatureCollection",
        "features": [
            {
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                122.18919, // this is longitude
                                4.82948294 // this is latitude
                            ],
                            [
                                124.17318,
                                5.9319319
                            ],
                            [
                                101.131191,
                                2.92492
                            ],
                            [
                                106.01010192,
                                4.492472492
                            ]
                        ]
                    ]
                },
                "type": "Feature",
                "properties": {}
            }
        ]
    },
    "id": 1,
    "routeType": "point",
    "name": "Test"
    }
}

如果点/坐标小于 10,那么我可能会像在渲染函数中那样输入它。但是如果有超过 10 个点(纬度和经度)怎么办?我想不出应该如何在 this.state = { polygon: [....]} 中使用 mapfor 循环函数。

我在网上搜索并找到了一些像 this 这样的例子和 this但未能捕获对它的理解。

如果有人有任何想法或好心在这里分享他的建议或样板,我非常感谢。

谢谢。

最佳答案

您必须通过您的响应进行映射,然后将您的坐标转换为documentation 中给出的类型

type LatLng {
  latitude: Number,
  longitude: Number,
}

假设您的 API 响应保存在 apiResponse 中,试试这个。 您可以使用 keys 和数组 Positions 访问您的对象,并通过您的坐标数组进行映射。

const polygon = apiResponse.data.routeGeoJSON[0].geometry.coordinates[0].map(coordsArr => { 
    let coords = {
        latitude: coordsArr[1],
        longitude: coordsArr[0],
      }
      return coords;
});

const polygon 是你给你的

<MapView.Polygon
    coordinates={polygon} />

关于javascript - 如何在具有多点(纬度和经度)的 react native map 上绘制多边形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47389640/

相关文章:

javascript - 如何从 parent 的样式组件访问 child 的 Prop ?

javascript - 屏幕导航标题不会显示在堆叠的抽屉导航中....React Navigation V2

javascript - 按值而不是键对 react native 对象列表进行排序

javascript - 如果我不是 Sudo,React-Native 无法运行项目

javascript - 根据用户的方向生成标记

javascript - 如何在 lernajs mono repo 的 VSCode 中使用 "jump to definition"?

.htaccess - 未捕获的语法错误 : Unexpected token < in React JS

javascript - Mongoose - 保持模型与变化相关联

javascript - jQuery .addClass 未在 $(this) 上执行

javascript - JQuery 无法识别动态生成的 id