简体   繁体   English

Google Maps从数组中编辑多个标记

[英]Google Maps edit multiple markers from array

EDIT : I've managed to get this working and the last code snippet is the solution to my code. 编辑:我已经设法使此工作,最后的代码片段是我的代码的解决方案。

Fairly new here and I'm sorry if my question is not perfect. 这里还很新,如果我的问题不完美,对不起。

I'm creating a website with a Google Map. 我正在使用Google Map创建一个网站。

The user enters his own address or Geolocate to find its position, then the script calculates the distance between the user and all the offices branches. 用户输入自己的地址或地理位置以查找其位置,然后脚本计算用户与所有办公室分支之间的距离。 The 3 closest branches are selected. 选择3个最接近的分支。

The user has the option to change his address on the fly, or switch to geolocation, vice versa in a fixed menu. 用户可以选择在固定菜单中即时更改其地址,或切换到地理位置,反之亦然。

I will need to route the user to closest office and have marker content div with buttons to call JS functions, so the markers need to be edited completely. 我需要将用户路由到最近的办公室,并使用按钮来调用标记内容div,以调用JS函数,因此标记需要进行完全编辑。 You guys tell me if it's better to simply delete all markers and respawn them or if it's better practice to edit the markers in place. 你们告诉我,最好只是删除所有标记并重新生成它们,还是最好的做法是在适当位置编辑标记。

Once geolocation or geocoding, it calls this function: 一旦进行地理定位或地理编码,它将调用此函数:

Edit : Warning BAD CODE! 编辑:警告错误代码!

function successCall(position) {

    //Gets data from both getLocation & codeAddress !

    // Branch_id and distance for all branches
    // Resets the arrays.
    var branches_distance = [];
    var branches_closest = [];

    var userlat = position.coords.latitude;
    var userlng = position.coords.longitude;

    //Store ID and Distance of branch in array
    for (i = 0; i < branches_array.length; i++){
        var branch_distance = {
            id: branches_array[i].branch_id,
            distance: calculateDistance(userlat, userlng, branches_array[i].branch_lat, branches_array[i].branch_lng)
        }
        branches_distance.push(branch_distance);
    }

    //Sort branches_distance from closest to farthest
    for (i = 0; i < branches_distance.length; i++){
        sortByKey(branches_distance, 'distance');
    }

    //Populate branches_closest with branch IDs
    for (i = 0; i < branchesToShow; i++){ // SETTING
        branches_closest.push(branches_distance[i].id);
    }


    //TIME TO MAKE MARKERS AND ROUTES
    createMarkers(branches_closest, userlat, userlng);

}

and createMarkers function : 和createMarkers函数:

Edit : Warning BAD CODE! 编辑:警告错误代码!

var markers = [];

zero = 0;

function createMarkers(closest_items, ulat, ulng) {


    //If markers array is empty, create markers
    if(zero == markers.length){

        //Create User Loc marker and push in the array       
        var userLatLng = new google.maps.LatLng(ulat, ulng);

        var uMarker = new google.maps.Marker({
                position: userLatLng,
                id: 'user'
        });

        markers.push(uMarker);

        //Create branches markers
        for (i = 0; i < closest_items.length; i++){

            var result = $.grep(branches_array, function(e){ return e.branch_id == closest_items[i]; });

            var latLng = new google.maps.LatLng(result[0].branch_lat, result[0].branch_lng);

            var marker = new google.maps.Marker({
                position: latLng,
                id: closest_items[i]
            });

            markers.push(marker);
        }


        for (index in markers) {
            addMarker(markers[index]);   
        }

        console.log(markers);


    } else {

        console.log('I just want to update my markers :\'( ');


    }


} 

I'm at the point where when you type in a second address, it console.log a string. 当您输入第二个地址时,它就是console.log一个字符串。 I tried many things but I can't get to edit markers or delete them. 我做了很多尝试,但无法编辑或删除标记。

If you guys can simply point out where to look, any help would be appreciated. 如果你们能简单指出在哪里看,任何帮助将不胜感激。

Thank you very much and keep in mind I'm fairly new here so if my question is unappropriate / incomplete please leave positive info about it so I can get better !! 非常感谢并记住我在这里还很陌生,所以如果我的问题不适当/不完整,请留下有关它的正面信息,以便我可以变得更好!

Edit : Now I got it working, the main things is that you need to call this function for every marker : 编辑:现在我开始工作了,主要的事情是您需要为每个标记调用此函数:

google.maps.event.addListener(marker,"click",function(){}); google.maps.event.addListener(标记, “点击”,函数(){});

I removed the whole addMarker function and put code directly into createMarkers(); 我删除了整个addMarker函数,并将代码直接放入createMarkers();中。

var markers = []; var markers = []; // storing markers //存储标记

function createMarkers(closest_items, ulat, ulng) { 函数createMarkers(closest_items,ulat,ulng){

  clearOverlays(); var infowindow = new google.maps.InfoWindow(); //Markers have been cleared, hurray, lets pop some new var uLatLng = new google.maps.LatLng(ulat, ulng); umarker = new google.maps.Marker({ position: uLatLng, map: map, title: 'user' }); markers.push(umarker); google.maps.event.addListener(umarker,"click",function(){ infowindow.setContent(umarker.title); infowindow.open(map, umarker); }); for (i = 0; i < closest_items.length; i++) { var result = $.grep(branches_array, function(e){ return e.branch_id == closest_items[i]; }); var latLng = new google.maps.LatLng(result[0].branch_lat, result[0].branch_lng); marker = new google.maps.Marker({ position: latLng, map: map, title: closest_items[i].toString() }); markers.push(marker); google.maps.event.addListener(marker,"click",function(){ infowindow.setContent(this.title); infowindow.open(map, this); }); } makeRoute(); } 

Clearing the current markers: 清除当前标记:

function clearOverlays() {
    for (var i = 0; i < markers.length; i++ ){
        markers[i].setMap(null);
    }
    markers.length = 0;
}

I suggest you delete all markers and call the function again: 我建议您删除所有标记,然后再次调用该函数:

 ......
} else {
      for (var i = 0; i < markers.length; i++) {
          markers[i].setMap(null); // remove marker
      }

      // Reset the array
      markers = [];

      // Call the function again. The markers size will be zero and 
      // everything will work fine!
      createMarkers(closest_items, ulat, ulng);
}

You could update the existing map with the new pins. 您可以使用新的图钉更新现有地图。 Personally I think this would create a better user experience. 我个人认为这将创建更好的用户体验。 The user can see office locations updating without having to remeber where they were on their previous search. 用户可以看到办公室位置更新,而不必记住他们在先前搜索中的位置。

DEMO http://jsfiddle.net/cmd932z3/20/ 演示http://jsfiddle.net/cmd932z3/20/

function clearMarkers() {
  setAllMap(null);
}

function showMarkers() {
  setAllMap(map);
}

function updateMarkers() {
  clearMarkers();
  markers = [];
}

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

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