谷歌地图怎么去只显示一个标志?(Google Maps how to get to show onl

2019-09-22 01:34发布

我一直在处理这几个小时,我仍然无法人物如何做到这一点。

我的目标是寻找接近对方地址时,只显示一个标记。

下面你的代码我在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.

Answer 1:

你可以的一小段代码添加到您的地址解析函数的开始,将设置一个新的前删除以前的标记。 尝试这个:

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


Answer 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>


文章来源: Google Maps how to get to show only one marker?