Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

Adding multiple Polylines on react-native-map with Google API coordinates

I want to place multiple polylines on Google maps in react-native. I'm using the react-native-maps and @mapbox/polyline for creating polylines which I imported at the top:

import MapView from 'react-native-maps';
import Polyline from '@mapbox/polyline';

Here is the rest of the code:

import {
  Marker
} from 'react-native-maps';

import React, { Component } from 'react';
import {
   Platform,
   StyleSheet,
   Text,
   View,
   Button
 } from 'react-native';

 export default class consults extends Component {
  constructor(props) {
  super(props);
  this.state = {
    coords: []
  }
 }

componentDidMount() {
   this.getDirections("55.23607109335242, 10.478553771972656", "55.178279530181264, 10.525074005126953");
   this.getDirections("55.067434, 10.607282", "55.093088, 10.588734");
 }

 async getDirections(startLoc, destinationLoc) {

   try {
       let resp = await fetch(`https://maps.googleapis.com/maps/api/directions/json?origin=${ startLoc }&destination=${ destinationLoc }&mode=${'walking'}`)
       let respJson = await resp.json();
       let points = Polyline.decode(respJson.routes[0].overview_polyline.points);

       let coords = points.map((point, index) => {
           return  {
               latitude : point[0],
               longitude : point[1]
           }
       })


       this.setState({coords: coords})
       return coords
   } catch(error) {
       alert(error)
       return error
   }
}


static navigationOptions = {
title: 'Kort over stier'
};

render () {
 const { navigate } = this.props.navigation;
 const { region } = this.props;
 console.log(region);

 return (
   <View style ={styles.container}>
    <MapView
      style={styles.map}
      region={{
        latitude: 55.0598,
        longitude: 10.6068,
        latitudeDelta: 0.40,
        longitudeDelta: 0.40,
      }}
    >

    <MapView.Polyline
        coordinates={this.state.coords}
        strokeWidth={2}
        strokeColor="red"/>

    </MapView>
  </View>
 )
}

}

const styles = StyleSheet.create({
container: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
justifyContent: 'flex-end',
alignItems: 'center',
},
map: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
},

});

When i'm just requesting coordinates from one positing to another it draw a line example:

this.getDirections("55.23607109335242, 10.478553771972656", "55.178279530181264, 10.525074005126953");

When I requesting two I get the coordinates for the last one. Anyone knowing how to get multiple polylines drawn on same map? In advance, thanks :-)

Update Find this example picture from another stakoverflow problem. I want the same but without the markers:

Polyline image

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have you tried storing array of polylines? Some changes to your component must be made, btw:

...
constructor(props) {
    super(props)
    this.state = { coordsArray: [] }
}
...
async getDirections(startLoc, destinationLoc) {
    ...
    const newCoordsArray = [ ...this.state.coordsArray, coords]
    this.setState({ coordsArray: newCoordsArray })
    ...
}
...

and for render method:

render () {
 const { navigate } = this.props.navigation;
 const { region } = this.props;

 return (
   <View style ={styles.container}>
    <MapView
      style={styles.map}
      region={{
        latitude: 55.0598,
        longitude: 10.6068,
        latitudeDelta: 0.40,
        longitudeDelta: 0.40,
      }}
    >
    {this.state.coordsArray.map((coords, index) => 
      <MapView.Polyline
        index={index}
        coordinates={coords}
        strokeWidth={2}
        strokeColor="red"/>
    }
    </MapView>
  </View>
 )
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...