简体   繁体   中英

How do create clickable icons in google maps api v3?

I looked in the api docs but couldn't find much. I am stuck trying to create a clickable icon using the code below that displays a pop up box of information:

  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);
  });

You are referencing the wrong object in addListener - using marker where you should be using balloon . The Marker object isn't added to the map.

Change this:

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

to

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

Not exactly sure what you're trying to accomplish but this may be of use.

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;
}

This creates an icon within the map if that's what you're going for. Then once clicked it will display your contentString in a bubble within the embedded map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM