Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google places api returns duplicate places

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.net/2KrmY/. How can I prevent duplicates from showing? Any help is greatly appreciated!

like image 800
CyberJunkie Avatar asked Jun 14 '13 18:06

CyberJunkie


3 Answers

As 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.net/2KrmY/15/

like image 84
Matt Rowles Avatar answered Oct 18 '22 18:10

Matt Rowles


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.

like image 24
xdazz Avatar answered Oct 18 '22 20:10

xdazz


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..

like image 1
MarmiK Avatar answered Oct 18 '22 18:10

MarmiK