Google Maps如何仅显示一个标记? [英] Google Maps how to get to show only one marker?
问题描述
这两个小时我一直在处理,但我仍然不知道该怎么做.
i've been dealing with this couple of hours and i still cant figure how to do it.
我的目标是在搜索彼此接近的地址时仅显示一个标记.
my objective is to display only one marker when searching addresses close to each other.
下面有我在html中用于搜索地址的代码,请注意-我正在开发一个执行此操作的Windows应用程序,在这种情况下,您可能会发现一些缺少的内容,通过单击按钮进行操作,因为这通过.NET Windows应用程序完成
below you have the code i use in my html in order to search for addresses, note - i'm developing a windows application that does such, in which case you might find some missing stuff to do actions by clicking buttons since this is done via .NET windows application
html代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com.mx/maps/api/js?sensor=true&language=es"></script>
<script type="text/javascript">
var G = google.maps;
var map;
var geocoder = new G.Geocoder();
var marker;
var markersArray = [1];
function initialize() {
createMap();
geocode('Chicago');
}
function createMap() {
var myOptions = {
center: new G.LatLng(0,0),
zoom: 17,
mapTypeId: G.MapTypeId.ROADMAP
}
map = new G.Map(document.getElementById("map_canvas"), myOptions);
}
function geocode(address){
geocoder.geocode({ 'address': (address ? address : "Miami Beach, Florida")}, function (results, status) {
if (status == G.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker = new G.Marker({
map: map,
animation: google.maps.Animation.DROP,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
阅读以前的文章,我知道必须使用if/else语句,但无法正确处理.
reading previous posts i know that if/else statement has to be used but cant get it right.
非常感谢您的帮助.
狮子座.
推荐答案
您可以在Geocode函数的开头添加一小段代码,该代码将在设置新标记之前删除先前的标记.试试这个:
You can add a small bit of code to the beginning of your Geocode function that will remove the previous marker before setting a new one. Try this:
function geocode(address){
if (marker) {
marker.setMap(null);
}
geocoder.geocode({ 'address': (address ? address : "Miami Beach, Florida")}, function (results, status) {
这篇关于Google Maps如何仅显示一个标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!