点击时如何删除谷歌地理编码 [英] how to remove google geocode on click
本文介绍了点击时如何删除谷歌地理编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Google地图 - 路线和地理编码。加载页面时,地理编码会放置一个标记。当用户点击提交按钮时,是否可以删除该标记?
I am using google maps- directions and geocoding. The geocode places a marker when the page loads. Is it possible to have this marker removed when a user clicks the submit button?
以下是代码:
Here is the code:
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);
}
});
推荐答案
假设您使用v3 api,请尝试:
Assuming you're using the v3 api, try:
marker.setMap(null);
这将从地图中移除它。
http://code.google.com/apis/maps /documentation/javascript/reference.html#Marker
这篇关于点击时如何删除谷歌地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文