我尝试使用java脚本在谷歌地图上创建多个标记,这里出现问题,当两个标记具有相同的地理位置时,如果出现这样的两个标记,那么此标记后出现的其他标记不会显示在地图上,请引导我,如果有人有解决方案。
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);
}
});
答案 0 :(得分:1)
我接近它的方式是增加lat&amp;如果标记的位置是重复的,我创建了新的数组,它包含所有重复的lat和lng,搜索数组,找到lat和lng存在,获取更新的lat和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;
}