在现有地图中添加新的Google地图标记

时间:2016-01-11 09:23:28

标签: google-maps google-maps-api-3

我的页面上有一个现有的地图,其中包含PHP从数据库生成的标记。工作良好。现在我想在这张地图中添加一个新标记。

我无法正常工作:

 <script src="http://maps.google.com/maps/api/js?sensor=false"
        type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var map;
var customIcons = {
  restaurant: {
    icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
    shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
  },
  bar: {
    icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
    shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
  }
      };

function load() {
  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(53.2193835,6.5665018),
    zoom: 10,
    mapTypeId: 'roadmap'
  });

     var infoWindow = new google.maps.InfoWindow;

  // Change this depending on the name of your PHP file
  downloadUrl("geo.php?gmt=<? echo $stringGMT;?>&cat=<? echo $stringAAN;?>&abo=<? echo $stringABO;?>", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");

   var markersskg=[];

创建标记代码:工作

      var icon = customIcons["bar"] || {};

        var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon,
        shadow: icon.shadow
         });



      bindInfoWindow(marker, map, infoWindow, html);

    }
        });

}


function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
}

function downloadUrl(url, callback) {
  var request = window.ActiveXObject ?
      new ActiveXObject('Microsoft.XMLHTTP') :
      new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
}

function doNothing() {}

//]]>

function addmarker(lat,lon) {
var myLatLng = {lat: lat, lng: lon};
var marker;

    //create a marker
    marker = new google.maps.Marker({          
        position: myLatLng,
        map: map,
        draggable: true
    });

}

和onclick调用:

<div id="map" style="width: 100%; height: 600px"></div>
<button id="button" onClick="addmarker(53.0241066,6.571086);">Drop Markers</button>

1 个答案:

答案 0 :(得分:3)

您在加载函数中声明了var map,然后在addMarker函数

中不可见

尝试在全局范围内声明var map

 var map ;

 var customIcons = {
     skg: {
       icon: 'http://mywebsite.nl/img/logo_transparant24.png',
       shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
     }
 };

 function load() {
  map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(53.2193835,6.5665018),
    zoom: 10,
    mapTypeId: 'roadmap'
  });