2009-08-21 88 views

回答

1

如果您只有城市名稱或地址,使用以下功能:

var map = new GMap2(document.getElementById("map")); 
var geocoder = new GClientGeocoder(); 

function showAddress(address) { 
    geocoder.getLatLng(
    address, 
    function(point) { 
     if (!point) { 
     alert(address + " not found"); 
     } else { 
     map.setCenter(point, 13); 
     var marker = new GMarker(point); 
     map.addOverlay(marker); 
     marker.openInfoWindowHtml(address); 
     } 
    } 
); 
} 

否則如果按要求下面使用下面的代碼你把所有的值:

var json_loc = { "locationName": "xxxx", "locationAddress": "xxxx", "latitude": xxxx, "longitude": xxxx }; 

如果您有以上值,您可以使用以下功能獲得谷歌地圖。

var map = new GMap2(document.getElementById("map")); 
var point = new GLatLng(json_loc.latitude, json_loc.longitude); 
var locationName = json_loc.locationName; 
var locationAddress = json_loc.locationAddress; 
map.setCenter(point, 14); 
map.addControl(new GSmallMapControl()); 
map.addControl(new GMapTypeControl()); 
map.addOverlay(locationView.createMarker(point, locationName, locationAddress)); 
// Creates a marker whose info window displays the letter corresponding 
// to the given index. 
createMarker: function(point, locationName, locationAddress) { 
    var marker = new GMarker(point);  
    GEvent.addListener(marker, "mouseover", function() { 
     marker.openInfoWindowHtml("<b>" + locationName + "</b><br>" + locationAddress); 
    }); 
    return marker; 
} 
0

這是向地圖添加標記的最簡單方法。

var point = new GLatLng(40,10); 
var marker = new GMarker(point); 
map.addOverlay(marker);