javascript - Google map - 隐藏目的地标记

标签 javascript google-maps typescript google-maps-api-3 google-maps-markers

我正在尝试在谷歌地图上显示一些命中标记,并显示从开始到结束的路线。

我已经显示了路线,但我的出发地和目的地具有完全相同的坐标,导致目的地标记(标记“D”)与出发地标记重叠,如下所示:

Waypoint markers on map

理想情况下,我只想隐藏目标标记,但我完全不知道如何抑制单个标记。

displayRoute 函数

     displayRoute(directionsService, directionsDisplay) {
    let numberOfHits = this.geo.Data.rows.length - 1;
    let firstHit = this.geo.Data.rows[numberOfHits];
    let lastHit = this.geo.Data.rows[0];
    let wayPoint, i;
    let wayPointsArray = [];

    this.checkForDuplicates('Id', this.geo.Data.rows);

    for (i = 1; i < numberOfHits; i++) {
      wayPoint = this.geo.Data.rows[i];

      if (this.duplicatesArr.indexOf(wayPoint.Id) === -1) {
        wayPointsArray.unshift({
          location: {
            lat: wayPoint.latitude,
            lng: wayPoint.longitude
          }
        });
      } else if (this.duplicatesArr.indexOf(wayPoint.Id) > -1) {
        console.log('wayPoint', wayPoint.Id, 'has already been hit')
      }
    }

    let request = {
      origin: {
        lat: firstHit.latitude,
        lng: firstHit.longitude
      },
      destination: {
        lat: lastHit.latitude,
        lng: lastHit.longitude
      },
      waypoints: wayPointsArray,
      travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    if((request.origin.lat == request.destination.lat) && (request.origin.lng == request.destination.lng)) {
      // Code to hide destination marker
    }

    directionsService.route(request, (response, status) => {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
    });
  }

地理数据

this.geo = {
     "Data": {
      "records": 7,
      "total": 1,
      "page": 1,
      "rows": [
        {
          "Id": 2778,
          "latitude": 51.509697,
          "longitude": -2.2
        },
        {
          "Id": 53,
          "latitude": 50.980598,
          "longitude": -1.36827
        },
        {
          "Id": 2750,
          "latitude": 51.152599,
          "longitude": -1.34676
        },
        {
          "Id": 2778,
          "latitude": 51.509697,
          "longitude": -2.2
        }
      ]
    }
  }

任何有关此问题的帮助将不胜感激!

编辑

抱歉,为了清楚起见,我在此处创建了 DirectionDisplay:

  createMap() {
let directionsDisplay = new google.maps.DirectionsRenderer;
let directionsService = new google.maps.DirectionsService;

let map = new google.maps.Map(document.getElementById('map'), {
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

let bounds = new google.maps.LatLngBounds();
map.fitBounds(bounds);

directionsDisplay.setMap(map);
this.displayRoute(directionsService, directionsDisplay);

};

这在 displayRoute() 函数之前使用

最佳答案

DirectionsRendererOptions有一个 suppressMarkers 属性。

这将隐藏起点和目的地标记。

然后您可以简单地在原点坐标处添加您自己的标记。基于您发布的代码的示例:

if ((request.origin.lat == request.destination.lat) && (request.origin.lng == request.destination.lng)) {

  // Code to hide destination marker

  var rendererOptions = {
    suppressMarkers: true,
  };

  var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);

  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(request.origin.lat, request.origin.lng),
    map: map
  });

} else {

  var directionsDisplay = new google.maps.DirectionsRenderer();
}

directionsService.route(request, (response, status) => {
  if (status == google.maps.DirectionsStatus.OK) {
    directionsDisplay.setDirections(response);
});

您没有发布完整的代码,因此上述内容意味着您删除了(未包含的)var DirectionsDisplay = new google.maps.DirectionsRenderer();的初始化

关于javascript - Google map - 隐藏目的地标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50926867/

相关文章:

javascript - 获取客户端的最大int服务器端

javascript - 异步返回问题

Android 谷歌地图绘制两点之间的路径

Angular6 - 使用 API 调用加载翻译到后端使用 ngx-translate

node.js - Mongoose 数组对象中的数字递增

android - 在 chrome 开发工具上模拟低分辨率设备

javascript - 使用 jQuery 选择没有值的选项

javascript - 如何检查 jQuery 加载的 PHP 文件是否发生变化并在发生变化时执行函数?

javascript - 位置+ map View - ComponentWillMount 在渲染之前解析? react native |世博会

java - 抽屉导航中的 map Activity