question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Draw dynamic markers and polygon each render call

See original GitHub issue

I am trying to use your library and i found it very helpfull. I am trying to draw a map with some markers and draw poligons to show a path dynamically, depends on the markers count. I want to draw another marker on some user action and draw a poligon from some earlier marker to this new marker at render . The example I’ve seen is drawing polygon on ‘onGoogleMapAPILoaded()’ event which is not firing again on further component renders. Can you suggest me some way around in this regard.

My Map Component:

import React from 'react';
import shouldPureComponentUpdate from 'react-pure-render/function';
import GoogleMap from 'google-map-react';
import MyGreatPlace from './MapPlace';
import MarkerStore from '../../stores/MarkerStore'
import MarkerAction from '../../actions/MarkerAction'

export default class SimpleMapPage extends React.Component {
  static propTypes = {
    center: React.PropTypes.array,
    zoom: React.PropTypes.number,
    greatPlaceCoords: React.PropTypes.any
  };


  componentDidMount() {
  }
  componentWillReceiveProps(){}



      constructor(props) {
    var geodesicPoly=[];
    super(props);
  }

  update() {
    console.log(this.geodesicPoly);
    for (var i =1; i<this.geodesicPoly.length;i++){
      var path = [new google.maps.LatLng(this.props.markers[i-1].lat,this.props.markers[i-1].lng),
    new google.maps.LatLng(this.props.markers[i].lat,this.props.markers[i].lng)];
      //poly.setPath(path);
      this.geodesicPoly[i].setPath(path);
    }

  }
  render() {
    return (
        <div className="map">
          <GoogleMap style={{position:'absolute',width:'50%',height:'50%'}}
                 YesIWantToUseGoogleMapApiInternals={true}
                 onGoogleApiLoaded={({map, maps}) => {
              this.geodesicPoly=[];
          for (var i=1; i< this.props.markers.length; i++)
                          {
            this.geodesicPoly[i] = new maps.Polyline({
              strokeColor: '#CC0099',
              strokeOpacity: 1.0,
              strokeWeight: 3,
              geodesic: true,
              map: map
            });
            }

            this.update();
        }}
                 bootstrapURLKeys={
                {key: 'AIzaSyClrg6TsqAGm4zfUTBcZGXMxdG2Sg3LnfM'}
            }// set if you need stats etc ...
                 center={this.props.center}
                 zoom={this.props.zoom}>
        {
          this.props.markers.length >0 ?
              this.props.markers.map((marker, index) => {this.markers = this.props.markers;
                return (
                    <MyGreatPlace key={index} lat={marker.lat} lng={marker.lng} text={marker.name} />
                )

              }) :''

        }
      </GoogleMap>
        </div>
    );
  }
}

    module.exports =  SimpleMapPage;

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
imran-abbas1commented, Sep 8, 2020

@itsmichaeldiego I was able to solve the problem by setting map and maps and using it to render polygons.

0reactions
peguescommented, Apr 13, 2022

@imran-abbas1 can you please provide your full working code here as an example?

Read more comments on GitHub >

github_iconTop Results From Across the Web

qt - How can I dynamically draw a polygon and make its points ...
In the following code a marker will be added with the right click and you can drag a marker with the right click....
Read more >
Polylines and Polygons to Represent Routes and Areas
This tutorial shows you how to add a Google map to your Android app, and use polylines and polygons to represent routes and...
Read more >
Work with markers in Mapbox.js | Help
In this guide, we'll show how to add markers, customize them, and make them interactive with Mapbox.js. Think of this guide as a...
Read more >
React Google Maps Api Style Guide
GoogleMap - The map component inside which all other components render. The simplest way to get a functional map is: ⚠️ Make sure...
Read more >
Custom drawing in 2D - Godot Docs
Godot has nodes to draw sprites, polygons, particles, and all sorts of stuff. For most cases, this is enough; but not always. Before...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found