简体   繁体   English

如何使用javascript处理谷歌地图中两个标记的冲突

[英]how to handle conflict of two marker in google map using javascript

i trying to create multiple markers on Google map using java script, here problem arise when two markers have the same Geo Location, if such two markers arise then other markers which occur after this marker are not display on the map, please guide me if anybody has the solution.我尝试使用 java 脚本在 Google 地图上创建多个标记,当两个标记具有相同的地理位置时会出现问题,如果出现这两个标记,则在此标记之后出现的其他标记不会显示在地图上,如果有人请指导我有解决方案。

var geocoder;
var map;
var geocoder = new google.maps.Geocoder();
var newLocation =[];
var SelectIndex =0;
var Markers =[];
var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/pin.jpg';
var InfoWindowContent ;

var MarkerJson = eval('{{$json_data}}');
console.log(MarkerJson);

var Markers = [];
var i=0;

var Media;

for(var Marker in MarkerJson)
{  
  if(MarkerJson[Marker].media_type == '1')
  {
     Media = '{{$BASE_URL}}add_images/thumb/'+MarkerJson[Marker].image_video_name; 
  }
  else
  {
    Media =  '{{$BASE_URL}}add_images/thumb/play_back.png';
  }
  Markers[i] = ['<div class="info_abstract"><div class="first_show">'+MarkerJson[Marker].title+'</div><div class="second_show"><img src="'+Media+'" /></div></div><div class="info_desc"><div class="status_container">'+MarkerJson[Marker].product_status+'</div><div>'+MarkerJson[Marker].desc.substring(0,100)+'</div><div><a class="detail_link"><a href="{{$BASE_URL}}detail-view/'+MarkerJson[Marker].id+'" >View Detail</a></div></div>',Number(MarkerJson[Marker].ad_address_latitude),Number(MarkerJson[Marker].ad_address_longitude),MarkerJson[Marker].city_name,MarkerJson[Marker].desc,i+1];

  i=i+1;
}


geocoder.geocode( { 'address':'{{$CurrentCity}}'}, function(results, status) {
     if (status == google.maps.GeocoderStatus.OK) {
       var mapOpt = {
         center:results[SelectIndex].geometry.location,
         zoom:13,
         mapTypeId:google.maps.MapTypeId.ROADMAP  
      };

      var map=new google.maps.Map(document.getElementById("my-map"),mapOpt);

      var CenterMarker = new google.maps.Marker();

      var infowindow = new google.maps.InfoWindow();

      for(var j=0; j < Markers.length; j++)
      { 
         marker = new google.maps.Marker({
         position: new google.maps.LatLng(Markers[j][1], Markers[j][2]),
         map: map
        });

        InfoWindowContent = Markers[j][0];

       google.maps.event.addListener(marker, 'click', (function(marker, j) {
          return function(InfoWindowContent) {
              infowindow.setContent(Markers[j][0]);
              infowindow.open(map,marker);
            }
         })(marker, j));

      }

   } else {
      alert('{{$lang_var_name.geocode_was_not_successful_for_the_following_reason}}: ' + status);
    }
  });

the way i approach it is by increase the lat & lng of the marker's position if it is duplicated, i created new array that holds all duplicated lat and lng, search through the array, find the lat and lng are exists, get the updated lat and lng, assign it to the marker's position, store the last lat lng in the array.我接近它的方法是通过增加标记位置的纬度和经度(如果它是重复的),我创建了新数组来保存所有重复的经度和经度,搜索数组,找到经度和经度是否存在,获取更新的经度和 lng,将其分配给标记的位置,将最后一个 lat lng 存储在数组中。

function getUpdatedLatLng(lt, ln) {
        var amount = 0.004,
            duplicatedLatLng = null,
            lat = parseFloat(lt),
            lng = parseFloat(ln),
            newLat = 0,
            newLng = 0,
            increasedLat = 0,
            increasedLng = 0;
        if (myNamespace.DuplicatedLatLng.length > 0) {
            var obj = null;
            for (var i = 0; i < myNamespace.DuplicatedLatLng.length; i++) {
                var item = myNamespace.DuplicatedLatLng[i];
                if (item.Lat == lat && item.Lng == lng) {
                    obj = item;
                    break;
                }
            }
            if (obj) { // if latlng found in the duplicated array then increase the lat and lng, create the new latlng obj.
                increasedLat = (++obj.LatIncrease);
                increasedLng = (++obj.LngIncrease);
                newLat = (obj.Lat + (amount * increasedLat));
                newLng = (obj.Lng + (amount * increasedLng));
            } else { // if latlng didn't found in the duplicated array; then create new latlng and add to array.
                newLat = (lat + amount);
                newLng = (lng + amount);
                increasedLat = 1;
                increasedLng = 1;
            }
        }
        else { // if nothing is in the duplicated array, then create the new latlng item and add to the array.
            newLat = (lat + amount);
            newLng = (lng + amount);
            increasedLat = 1;
            increasedLng = 1;
        }
        duplicatedLatLng = new myNamespace.DuplicatedLatLngItem(lat, lng, increasedLat, increasedLng);
        myNamespace.DuplicatedLatLng.push(duplicatedLatLng);

        // return new latlng
        return new google.maps.LatLng(newLat, newLng);
    }

// Constructor: used for updating new latlng. Saved in myNamespace.DuplicatedLatLng array.
DuplicatedLatLngItem: function (lat, lng, latIncrease, lngIncrease) {
    this.Lat = lat;
    this.Lng = lng;
    this.LatIncrease = latIncrease;
    this.LngIncrease = lngIncrease;
}

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

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