2011-06-21 96 views
0

我查看了api文檔,但找不到太多內容。我堅持要創建使用的代碼可點擊圖標下方顯示的信息的彈出框:如何在google maps api v3中創建可點擊的圖標?

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

請考慮改善您的問題。目前形式的問題不包含足夠的細節。請閱讀[常見問題](http://stackoverflow.com/faq)和[如何提問](http://stackoverflow.com/questions/how-to-ask)部分,瞭解如何改進問題。你有什麼錯誤,如果有的話?什麼不行? – JohnP

+0

我真的很抱歉,我真的很早就寫了這個,我基本上是指當我點擊標記時,其中一個文本框應該彈出。 – Yesterday

回答

5

您正在引用addListener中的錯誤對象 - 使用您應該在其中使用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); 
}); 
+0

我現在正在使用您建議的代碼(除了image_balloon之外,我使用了infowindow,不過現在當我點擊標記時它只是凍結頁面:http://www.projectstratos.com/map.php – Yesterday

+0

是的!我知道了,它只是氣球不image_balloon! – Yesterday

+0

很高興聽到它的工作! –

-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

+0

該代碼適用於已棄用的V2。 –

相關問題