javascript - 谷歌地图如何只显示一个标记?

标签 javascript vb.net visual-studio google-maps windows-applications

我已经处理了这几个小时,但我仍然不知道该怎么做。

我的目标是在搜索彼此靠近的地址时只显示一个标记。

下面是我在 html 中用于搜索地址的代码,请注意 - 我正在开发一个执行此操作的 Windows 应用程序,在这种情况下,您可能会通过单击按钮找到一些缺少的东西来执行操作,因为这个通过 .NET windows 应用程序完成

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 语句但不能正确使用。

非常感谢您的帮助。

里奥·P。

最佳答案

您可以在地理编码函数的开头添加一小段代码,以便在设置新标记之前删除之前的标记。试试这个:

function geocode(address){
    if (marker) {
        marker.setMap(null);
    }
    geocoder.geocode({ 'address': (address ? address : "Miami Beach, Florida")}, function (results, status) {

关于javascript - 谷歌地图如何只显示一个标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12232370/

相关文章:

javascript - jQuery 优化 - 存储 jquery 对象引用有什么问题吗?

javascript - 使用 javascript 或 jquery 在 session 中存储 Flask 数据

c# - Git和TFS版本控制团队合作

visual-studio - 使用旧网站名称的 Visual Studio 2013

c# - 如何将引用的库打包到新库中

javascript - Angular - 如何在 ng-view 中调用 javascript 函数

javascript - Highcharts 气泡图数据标签中的居中文本

vb.net - 文件系统 FilePutobject 问题

vb.net - VB.NET接口(interface)中的读写属性

c# - 用于 Linux 的 dotnet : Console Input Cursor Navigation