簡體   English   中英

Google Maps-隱藏目的地標記

[英]Google Maps - Hide destination marker

我正在嘗試在Google地圖上顯示一些命中標記,並顯示從頭到尾的路線。

我已經顯示了路線,但是我的起點和終點坐標完全相同,導致終點標記(標記“ D”)與起點標記重疊,顯示如下:

地圖上的航點標記

理想情況下,我只想隱藏目標標記,但是我絕對不知道如何隱藏單個標記。

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
        }
      ]
    }
  }

任何幫助,將不勝感激!

編輯

抱歉,為清楚起見,這是我創建DirectionsDisplay的地方:

  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();初始化(不包括在內var directionsDisplay = new google.maps.DirectionsRenderer();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM