【问题标题】:Hide polyline from A to B using in Google Map api v3使用 Google Map api v3 隐藏从 A 到 B 的折线
【发布时间】:2014-02-22 16:25:36
【问题描述】:

我正在用下面的代码显示谷歌地图,我想隐藏 A 到 B 之间的折线。谷歌上的所有答案都是关于创建一个数组然后执行array.setmap(null)。我可以在不使用数组的情况下隐藏折线吗?在其他情况下,我应该如何使用数组来使用下面的代码隐藏折线。

编辑:我需要显示标记 A 和 B

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var time;    
function initialize() {

        var rendererOptions = {
            map: map,
            draggable: true               
        }
        // Instantiate a directions service.
        directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);

        // Create a map and center it on islamabad.
        var islamabad = new google.maps.LatLng(33.7167, 73.0667);
        var mapOptions = {
            zoom: 13,
            center: islamabad
        }
        map = new google.maps.Map(document.getElementById('map'), mapOptions);
        directionsDisplay.setMap(map);
        calcRoute();
    }

    function calcRoute() {
        var start = document.getElementById('MainContent_txtFrom').value;
        var end = document.getElementById('MainContent_txtTo').value;
        var request = {
            origin: start,
            destination: end,
            travelMode: google.maps.TravelMode.DRIVING
        };
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {

                directionsDisplay.setDirections(response);
            }
        });
    }      
    google.maps.event.addDomListener(window, 'load', initialize);

【问题讨论】:

  • 你做对了。您只需要调用directionsDisplay.setMap(null); 来删除路由。
  • 通过写directionsDisplay.setMap(null); 地图根本没有渲染。它一直显示中心
  • 你想在什么时候删除它?你在哪里添加了上面的代码?
  • 我在用户填写开始和结束位置文本框后绘制地图,我只想显示 A 和 B 标记,而不是它们之间的蓝线。我在directionsDisplay.setDirections(response); 之前和之后以及directionsService.route(...){...} 块之外都尝试了directionsDisplay.setMap(null);
  • 然后只显示 A 和 B 的 2 个标记。如果只是在地图上显示 2 个点,我仍然不明白为什么需要路线服务。

标签: google-maps google-maps-api-3


【解决方案1】:

如下面的demo所示,可以通过两种方式去除折线:

  • directionsDisplay 中的选项suppressPolylines 设置为true,使用您的google.maps.DirectionsRenderer

    directionsDisplay.setOptions({
            suppressPolylines: true
          });
    

    这将保留起点和终点标记。

    方法setOptions(options:DirectionsRendererOptions)在初始化后改变DirectionsRenderer的选项设置。

  • 使用directionsDisplay.setMap(null); 删除所有方向渲染,但这包括标记,因此如果您这样做,则需要向地图添加额外的标记。

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var time;

var pointA = new google.maps.LatLng(48.86, 2.35);
var pointB = new google.maps.LatLng(33.7167, 73.0667);

function initialize() {

  var rendererOptions = {
      map: map,
      draggable: true
    }
    // Instantiate a directions service.
  directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);

  // Create a map and center it on islamabad.
  var islamabad = new google.maps.LatLng(33.7167, 73.0667);
  var mapOptions = {
    zoom: 13,
    center: islamabad
  }
  map = new google.maps.Map(document.getElementById('map'), mapOptions);
  directionsDisplay.setMap(map);
  calcRoute();
}

function calcRoute() {
  var start = pointA;
  var end = pointB;
  var request = {
    origin: start,
    destination: end,
    travelMode: google.maps.TravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
};

function removeRoute() {
  directionsDisplay.setOptions({
    suppressPolylines: true
  });
  // this "refreshes" the renderer
  directionsDisplay.setMap(map);
};

function removeRouteNull() {
  directionsDisplay.setMap(null);
};
google.maps.event.addDomListener(window, 'load', initialize);
#map {
  height: 280px;
}
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<button onclick="removeRoute()">Remove route (suppressPolylines)</button>
<button onclick="removeRouteNull()">Remove route (setMap(null))</button>
<button onclick="initialize()">Undo all</button>
<section id="map"></section>

【讨论】:

    【解决方案2】:

    如果您想渲染方向但隐藏折线,请使用DirectionsRendererOptions suppressPolylines

    function initialize() {
    
        var rendererOptions = {
            suppressPolylines: true,
            map: map,
            draggable: true               
        }
        // Instantiate a directions service.
        directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
    

    【讨论】:

    • 太好了。这就是我一直在寻找的。这是我的问题的准确而简单的答案,但正如@MrUpsidown 建议的那样,我不需要Directions Service 只是为了显示A 和B 标记而不是路由,所以这也是一个更好的主意(为未来的提问者解释)。
    【解决方案3】:

    如果您参考此答案:Google Maps v3 directionsRenderer.setMap(null) not working to clear previous directions,您应该会看到您要查找的内容。

    在实现方向渲染器对象时,您不需要使用数组。如果您在全局范围内声明(Edit 我现在看到您已经这样做了)(这样您在任何给定时间都只有一个实例),那么您可以简单地使用 commandsDisplay.setMap(null) 删除以前的路线呈现。

    如果您想从响应中渲染标记但隐藏折线,我想最简单(但我想绝不是最干净)的方法是简单地改变折线对象的不透明度:

    var myPolylineOptions = new google.maps.Polyline({
        strokeColor: '#FF0000',
        strokeOpacity: 0.00001,
        strokeWeight: 0
    });
    

    然后将其分配给您的渲染器:

    directionsDisplay = new google.maps.DirectionsRenderer({polylineOptions: myPolylineOptions});
    

    【讨论】:

    • 通过写directionsDisplay.setMap(null); 地图根本没有渲染。它一直显示中心
    • 顺便说一句,我没有呈现以前的方向的问题
    • 你只是想隐藏折线吗?但保留起点等?
    • 我认为这应该可以满足您的需求,但可能不是最干净的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2012-12-26
    • 2012-01-10
    • 2011-08-04
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多