1

我一直在处理这几个小时,但我仍然不知道该怎么做。

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

下面你有我在我的 html 中使用的代码来搜索地址,注意 - 我正在开发一个这样做的 Windows 应用程序,在这种情况下,你可能会发现一些缺少的东西可以通过单击按钮来执行操作,因为这是通过.NET 窗口应用程序

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。

4

2 回答 2

3

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

function geocode(address){
    if (marker) {
        marker.setMap(null);
    }
    geocoder.geocode({ 'address': (address ? address : "Miami Beach, Florida")}, function (results, status) {
于 2012-09-02T01:06:31.807 回答
2

仅创建一个标记作为全局变量并根据需要更改其位置。这样您还可以节省内存,因为您没有在每个请求上创建新的标记对象:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style>
    html, body {width:100%; height:100%}
    </style>

    <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 marker;
           var geocoder = new G.Geocoder();


            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);

// Create a single marker, (global variable), and don't give it a position just yet.
        marker = new G.Marker({
        map: map,
        animation: google.maps.Animation.DROP,
        });

            }

    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);

                //Position the marker
                marker.setPosition(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>
于 2012-09-02T10:04:22.830 回答