开发者

Avoid loading marker with the same latitude and longitude on google map

开发者 https://www.devze.com 2023-01-31 20:37 出处:网络
I would like to get marker through ajax as my map move. However, is there any method to avoid loading marker with the same latitude and l开发者_高级运维ongitude on google map? Let say once the marker

I would like to get marker through ajax as my map move. However, is there any method to avoid loading marker with the same latitude and l开发者_高级运维ongitude on google map? Let say once the marker for a certain latitude and longitude is loaded, it will not be added the second time when the map is move back to the location. Thank you.


Keep an array of markers and look at it before loading new markers, to ensure they haven't already been loaded. As each new marker is added, append it to the array.


Store the markers in an object "hash" and key it on lat-long.

Thus:

var marker_dict = {}, i=0, l=latLongArray.length, lat, lng;

while ( i < l ) {
    lat = latLongArray[i][0], lng = latLongArray[i][1]
    marker_dict[lat + '-' + lng] = new google.maps.Marker(
                                       new google.maps.LatLng(lat, lng)
                                   );
    i++;
}

Then, as you are adding in new markers, simply make sure to check that the key is not already in your marker_dict.

That way you don't have to loop over (potentially) the entire array every time the map is moved.


In order to create markers, you must have the lat and long data somewhere. Just be sure this set of data doesn't contain duplicates before calling the method to create the marker.

For example, I have an array of lat and long values, var latLong = [ [1.8, 2.6], [3.4, 1.1], ...];. I can iterate through this array and remove the duplicates. Then pass the resulting data set to my marker creator routine.


You can remove all the markers when the viewport is changing and retrieving the new markers for the current viewport and display them.

If you want all the markers to be kept on the map which i don't recommend because of the speed reducing you can push all the newcome markers to an array and make some kind of check(if the marker already exists in this array don't push).

0

精彩评论

暂无评论...
验证码 换一张
取 消