Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps Polyline and Marker together

Tags:

google-maps

I have this code for polyline which is working perfect. How do i add marker for each of location point?

var userCoor = [
<?php
for($i=0; $i<sizeOf($lat); $i++)
echo "new google.maps.LatLng({$lat[$i]}, {$long[$i]}),";
?>
];

var userCoordinate = new google.maps.Polyline({
path: userCoor,
strokeColor: "#FF0000",
strokeOpacity: 1,
strokeWeight: 2
});

userCoordinate.setMap(map);

I tried

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < userCoor.length; i++) {  
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(userCoor[i][1], userCoor[i][2]),
    map: map
  });

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(userCoor[i][0]);
      infowindow.open(map, marker);
    }
  })(marker, i));
}
}

Marker seems missing? Any idea how? Thanks.

like image 982
cicakman Avatar asked Dec 03 '25 19:12

cicakman


1 Answers

I tested with some bogus data here and there doesn't seem to be a problem. The difference is I'm using userCoorPath for the polyline and userCoor to set the markers. I'm repeating the information, but they are being used differently. One is an array of LatLngs, the other an array of string and two floats.

http://jsfiddle.net/nSf9N/

like image 144
Tina CG Hoehr Avatar answered Dec 06 '25 04:12

Tina CG Hoehr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!