简体   繁体   English

Google Maps Js API仅加载第一条路线

[英]Google Maps Js API only loads first route

I created a prototype for a project I'm working on, in which the user should create various markers in a map, be able to remove them, calculate a route with the markers, and go back to manage the markers. 我为正在处理的项目创建了一个原型,其中用户应该在地图上创建各种标记,可以删除它们,使用标记计算路线,然后返回以管理标记。

I used some code found here in geocodezip.com to calculate the route, and wrote some for the markers, etc. 我使用了geocodezip.com上的一些代码来计算路线,并为标记写了一些代码,等等。

My problem is that once the user calculates the route, no matter how he edits the markers, when clicking the button to calculate the route, the map only returns the route with the markers that were there on the first time he clicked the button. 我的问题是,一旦用户计算了路线,无论他如何编辑标记,在单击按钮以计算路线时,地图只会返回带有他第一次单击按钮时存在的标记的路线。 And the strangest thing is that I checked the coordinates that are being passed to the script that generates the route and the function is sending the markers as it should, but no matter the coordinates sent, it only works correctly on the first time. 最奇怪的是,我检查了传递到生成路线的脚本的坐标,并且该功能正在按原样发送标记,但是无论发送的坐标如何,它只能在第一次正确运行。

Js Fiddle: https://jsfiddle.net/1kmg2u65/2/ Js Fiddle: https : //jsfiddle.net/1kmg2u65/2/

The code is really really long so it's all in the Fiddle, but this is what it does: 1. User clicks on map, generate marker, marker goes to an array 2. If user deletes marker, it becomes null in array, to maintain the indexes 3. 'Clean' markers array receives all the markers in order, without the items that are null 4. A function is called with all the markers, this function creates the route 5. To manage the markers, a function reload the map just like it was in the start, but render all the markers that already are in the markers array

So here is what I believe is going on. 所以这就是我所相信的。

In the function markMap() you are instantiating new markers that belong to the google map object. markMap()函数中,您将实例化属于google map对象的新标记。

  for (var i = 0; i < markerElements.length; i++){    //Loop para gerar os marcadores
    if (markerElements[i] != null){
      marker = new google.maps.Marker({
        position: markerElements[i].position,
        map: map,
        title: markerElements[i].title
      });
    }
  }

This if fine, but you are not storing that constructed object anywhere. 如果可以的话,这很不错,但是您不会在任何地方存储该构造对象。 You need to be able to reference that marker to UN-associate it from the map. 您需要能够引用该marker以将其与地图关联。

At the end of the this for loop you need to add THAT mark to a global array so you can manage it later in the script. 在此for循环的结尾,您需要在全局数组中添加THAT标记,以便稍后可以在脚本中对其进行管理。

EXAMPLE

// defined at the top of the script
var markerGlobal = [];

  for (var i = 0; i < markerElements.length; i++){    //Loop para gerar os marcadores
    if (markerElements[i] != null){
      marker = new google.maps.Marker({
        position: markerElements[i].position,
        map: map,
        title: markerElements[i].title
      });
      // push marker onto global array
      markerGlobal.push(marker);
    }
  }

Now we can loop through the array and setMap to null 现在我们可以遍历数组并将setMap为null

  // un-reference marker from map
  markerGlobal[2].setMap(null);

I see you tried to do this with the removeMarker() function, but it doesn't have the handles to the markers already added to the map. 我看到您尝试使用removeMarker()函数执行此操作,但是它没有已添加到地图的标记的句柄。

Some Suggestions 一些建议

If I was you, I would think about refactoring the code to have one multi dimensional object that holds all the markers, their row info, variables etc. 如果我是你,我会考虑将代码重构为一个多维对象,其中包含所有标记,它们的行信息,变量等。

You could take it one step further and create a constructor function that handles the map and its associated markers. 您可以更进一步,并创建一个构造函数来处理地图及其关联的标记。 It would be most efficient. 这将是最有效的。

Good luck. 祝好运。

如果您在Tour_startUp函数定义中删除条件if (!window.tour) ,它会Tour_startUp工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM