2011-01-21 7 views
1

私はgoogle maps-directionsとgeocodingを使用しています。ジオコードは、ページが読み込まれるときにマーカーを配置します。ユーザーが送信ボタンをクリックしたときにこのマーカーを削除することは可能ですか?ここでGoogle geocodeをクリックして削除するには

コードです:あなたはv3のAPIを使用していると仮定すると、

var address = document.getElementById("address").textContent.replace(/\n/g, " "); 
geocoder.geocode({ 'address': address}, 
function(results, status) 
{ 
    if (status == google.maps.GeocoderStatus.OK) { 
    map.setCenter(results[0].geometry.location); 
    var marker = new google.maps.Marker({ 
     map: map, 
     position: results[0].geometry.location 
    }); 
    } 
    else 
    { 
    alert("Geocode was not successful for the following reason: " + status); 
    } 
}); 




var from = document.getElementById("from").value; 
var to = document.getElementById("to").value; 
var request = { 
    origin: from, 
    destination: to, 
    travelMode: google.maps.DirectionsTravelMode.DRIVING 
}; 
document.getElementById("map").style.width = "70%"; 
directionsService.route(request, function(response, status) { 
    if (status == google.maps.DirectionsStatus.OK) { 
    directionsDisplay.setDirections(response); 
    } 
}); 

答えて

1

、試してみてください。マップから削除されます

marker.setMap(null); 

を。

http://code.google.com/apis/maps/documentation/javascript/reference.html#Marker

+0

どこにコードを配置しますか?私は試しました: var marker = new google.maps.Marker(null); –

+0

マーカを最初に作成するときに、マーカをグローバル変数 "marker"に格納します。したがって、submitをクリックすると "marker"が呼び出されます.setMap(null); –

関連する問題