admin管理员组文章数量:1394168
I'm searching for places by keyword within an array of LatLngBounds.
var boundsarr = new Array();
boundsarr[0] = new google.maps.LatLngBounds(
new google.maps.LatLng(25.941886953491675, -80.17411103748543),
new google.maps.LatLng(25.947676224813897, -80.16767330177947)
);
boundsarr[1] = new google.maps.LatLngBounds(
new google.maps.LatLng(25.941886953491675, -80.16767330177947),
new google.maps.LatLng(25.94622890698334, -80.1644544339265)
);
boundsarr[2] = new google.maps.LatLngBounds(
new google.maps.LatLng(25.927413775186118, -80.1644544339265),
new google.maps.LatLng(25.94622890698334, -80.15962613214703)
);
boundsarr[3] = new google.maps.LatLngBounds(
new google.maps.LatLng(25.927413775186118, -80.15962613214703),
new google.maps.LatLng(25.931755728677782, -80.15801669822054)
);
boundsarr[4] = new google.maps.LatLngBounds(
new google.maps.LatLng(25.927413775186118, -80.15801669822054),
new google.maps.LatLng(25.933203046508336, -80.15318839644107)
);
boundsarr[5] = new google.maps.LatLngBounds(
new google.maps.LatLng(25.92886109301667, -80.15318839644107),
new google.maps.LatLng(25.933203046508336, -80.15157896251458)
);
then adding markers on the map, to and array, and creating a list of places returned using the markers array. Duplicate entries show up in the list and I can't figure out why..
Example: /. How can I prevent duplicates from showing? Any help is greatly appreciated!
I'm searching for places by keyword within an array of LatLngBounds.
var boundsarr = new Array();
boundsarr[0] = new google.maps.LatLngBounds(
new google.maps.LatLng(25.941886953491675, -80.17411103748543),
new google.maps.LatLng(25.947676224813897, -80.16767330177947)
);
boundsarr[1] = new google.maps.LatLngBounds(
new google.maps.LatLng(25.941886953491675, -80.16767330177947),
new google.maps.LatLng(25.94622890698334, -80.1644544339265)
);
boundsarr[2] = new google.maps.LatLngBounds(
new google.maps.LatLng(25.927413775186118, -80.1644544339265),
new google.maps.LatLng(25.94622890698334, -80.15962613214703)
);
boundsarr[3] = new google.maps.LatLngBounds(
new google.maps.LatLng(25.927413775186118, -80.15962613214703),
new google.maps.LatLng(25.931755728677782, -80.15801669822054)
);
boundsarr[4] = new google.maps.LatLngBounds(
new google.maps.LatLng(25.927413775186118, -80.15801669822054),
new google.maps.LatLng(25.933203046508336, -80.15318839644107)
);
boundsarr[5] = new google.maps.LatLngBounds(
new google.maps.LatLng(25.92886109301667, -80.15318839644107),
new google.maps.LatLng(25.933203046508336, -80.15157896251458)
);
then adding markers on the map, to and array, and creating a list of places returned using the markers array. Duplicate entries show up in the list and I can't figure out why..
Example: http://jsfiddle/2KrmY/. How can I prevent duplicates from showing? Any help is greatly appreciated!
Share Improve this question edited Jun 17, 2013 at 2:41 CyberJunkie asked Jun 14, 2013 at 18:50 CyberJunkieCyberJunkie 22.7k61 gold badges154 silver badges219 bronze badges 4- Sorry, just realized now, you're calling nearbySearch inside a loop with a LatLngBounds for each call, I did not get a chance to check it but I think that might be the issue..maybe your bounds are getting overlapped or might be a precision thing? – Rafael Oltra Commented Jun 15, 2013 at 1:48
- No problem. Yes I'm doing a nearbySearch for each bound. I'm sure it's related to the loop but I've been looking at the code and can't figure where it's overlapping... – CyberJunkie Commented Jun 15, 2013 at 1:52
- 1 It's not overlapping somewhere, the duplicates are a result of the search for boundsarr[1]. When you create a rectangle of the bounds you will see that returned places(Cafe Que Rico and Net Place Cafe Inc) are not located within the defined bounds. By this, you may call this a bug, I would call it a inaccurate result. – Dr.Molle Commented Jun 15, 2013 at 7:03
- @Dr.Molle thanks! Do you think this is an API bug? – CyberJunkie Commented Jun 15, 2013 at 14:30
3 Answers
Reset to default 3 +400As an interim solution, could you build an array of found places in your Callback each time and then within the same loop, see if the place has a count of >1, then don't call createMarker if so?
Not sure if perfect, but see below:
var foundPlaces = [];
var found;
function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
found = 0;
foundPlaces.push(results[i].id);
for (var j = 0; j < foundPlaces.length; j++) {
if (foundPlaces[j] === results[i].id) {
found++;
console.log(foundPlaces[j], results[i].id);
}
}
if (found < 2) {
createMarker(results[i]);
}
}
}
}
http://jsfiddle/2KrmY/15/
The bounds parameter is described as below:
The bounds within which to search for Places. Both location and radius will be ignored if bounds is set.
But for the second bounds in your code example, 2 results outside the bounds get into the result.
You could display the bounds area by using:
new google.maps.Rectangle({
strokeColor: 'red',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: 'bulue',
fillOpacity: 0.6,
map: map,
bounds: bounds
});
And then you can see the result.
Check The demo.
This may be a bug of the google map api.
You need to check before adding new bounds, as when you click again it generates new marker in the same place..
use this...
map.getBounds().contains(marker.getPosition())
refer old Post
this will return true
or false
after checking this you may render/add the marker
I have tried in the fiddle but still no success... as its JASON syntax.. and JavaScript require specific function for the user defined data type.. I am trying to create custom function to eliminate duplicate JSON object....
I will keep you posted with updated fiddle as soon as its ready, till then you may try on your own..
I hope this will do..
本文标签: javascriptGoogle places api returns duplicate placesStack Overflow
版权声明:本文标题:javascript - Google places api returns duplicate places - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744590675a2614471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论