Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google map two markers on one map

Hey. I'mm trying to put two points on a single map with same div id..
but it doesn't work..
code:

for 1st,

  var map = new GMap2(document.getElementById("map-canvas"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.setCenter(new GLatLng(<?=$lat;?>,<?=$lng;?>), 6);

  var point = new GLatLng(<?=$lat;?>,<?=$lng;?>);
  var marker = createMarker(point,'Welcome:<b></b><br>Second Info Window with an image<br><img src="http://localhost/gps/user_photo/" width=80 height=80>')
  map.addOverlay(marker);


  function createMarker(point,html) {
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        return marker;
      }

for 2nd,

  var map = new GMap2(document.getElementById("map-canvas"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.setCenter(new GLatLng(<?=$mylat;?>,<?=$mylng;?>), 6);

  var point1 = new GLatLng(<?=$mylat;?>,<?=$mylng;?>);
  var marker = createMarker1(point1,'Welcome:<b></b><br>Second Info Window with an image<br><img src="http://localhost/gps/user_photo/" width=80 height=80>')
  map.addOverlay(marker);


  function createMarker1(point,html) {
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        return marker;
      }

why two points not shown in one map????? and it will seen in two different maps...

like image 380
keyur Avatar asked Nov 14 '22 23:11

keyur


1 Answers

  //set up map as before

  var point0 = new GLatLng(<?=$mylat0;?>,<?=$mylng0;?>);
  var point1 = new GLatLng(<?=$mylat1;?>,<?=$mylng1;?>);
  var marker0 = createMarker(point0,'Welcome:<b></b><br>First Info Window etc);
  var marker1 = createMarker(point1,'Welcome:<b></b><br>Second Info Window etc);
  map.addOverlay(marker);

// only declare this once
  function createMarker(point,html) {
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        return marker;
      }

This is one way of doing it just to get it working for you, but essentially the js points and markers should be arrays which you loop through - ditto probably the $mylat and $mylong vars in PHP.

like image 76
Cups Avatar answered Dec 28 '22 13:12

Cups