如何在google maps api v3中创建可点击的图标?

时间:2011-06-21 07:33:53

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

我查看了api文档但找不到多少。我试图使用下面的代码创建一个可点击的图标,显示一个弹出的信息框:

  function initialize() {
  var myLatLng = new google.maps.LatLng(52.288, 0.78);
  var myOptions = {
    zoom: 8,
    center: myLatLng,
    mapTypeId: google.maps.MapTypeId.HYBRID
  };

  var map = new google.maps.Map(document.getElementById("map_canvas"),
      myOptions);

<?php

    $sql_launch = mysql_query("SELECT * FROM pred_route WHERE time = (SELECT MIN(time) FROM pred_route)");
    $sql_land = mysql_query("SELECT * FROM pred_route WHERE time = (SELECT MAX(time) FROM pred_route)");
    $sql_pop = mysql_query("SELECT * FROM pred_route WHERE alt = (SELECT MAX(alt) FROM pred_route)");

 ?>

  var contentString = '<div id="content">'+
    '<div id="siteNotice">'+
    '</div>'+
    '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
    '<div id="bodyContent">'+
    '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
    'sandstone rock formation in the southern part of the '+
    'Northern Territory, central Australia. It lies 335 km (208 mi) '+
    'south west of the nearest large town, Alice Springs; 450 km '+
    '(280 mi) by road. Kata Tjuta and Uluru are the two major '+
    'features of the Uluru - Kata Tjuta National Park. Uluru is '+
    'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
    'Aboriginal people of the area. It has many springs, waterholes, '+
    'rock caves and ancient paintings. Uluru is listed as a World '+
    'Heritage Site.</p>'+
    '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
    'http://en.wikipedia.org/w/index.php?title=Uluru</a> (last visited June 22, 2009).</p>'+
    '</div>'+
    '</div>';

  var infowindow = new google.maps.InfoWindow({
    content: contentString
    });

  var image_balloon = new google.maps.MarkerImage("http://projectstratos.com/images/map/balloon.png",new google.maps.Size(32,37));
  var balloon_longlat = new google.maps.LatLng(<?php echo $current['lat'] . ", " . $current['long']; ?>);
  var balloon = new google.maps.Marker({
      position: balloon_longlat,
      map: map,
      icon: image_balloon
  });

  google.maps.event.addListener(marker, 'click', function() {
  infowindow.open(map,image_balloon);
  });

2 个答案:

答案 0 :(得分:5)

您在addListener中引用了错误的对象 - 使用marker,您应该使用balloon。标记对象未添加到地图中。

改变这个:

google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,image_balloon);
});

google.maps.event.addListener(balloon, 'click', function() {
    infowindow.open(map,image_balloon);
});

答案 1 :(得分:-2)

不完全确定你想要完成什么,但这可能有用。

var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);

map.addOverlay(createMarker(myLatLng));

function createMarker(point) {
    var marker = new GMarker(point);

    GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(contentString);
    });

    return marker;
}

如果你想要的话,这会在地图中创建一个图标。然后,点击后,它会在嵌入式地图中的气泡中显示您的contentString