简体   繁体   中英

Search existing points on Google Maps V3 using Places Library

Alright so since I posted this the first time and I failed hardcore at properly explaining what I was looking to do, here's my second attempt.

On my site I've got Google Maps V3 Javascript implemented. A predefined set of points is created (from within the CMS) and pushed to the map via an array with the required information. What my client is wondering is if the users of this site can execute a search to find an existing point from this array. I was thinking that this was going to involve using the Places library but this returns all valid results from google, not from the array of custom points that are being created in the CMS.

$('#points-search').submit(function(){
    geocoder = new gm.Geocoder();

    var searchAddress = $(this).children('input[type="text"]').val();
    var searchPoints = [];

    if (geocoder){
        geocoder.geocode({'address' : searchAddress}, function(results, status){
            if (status == gm.GeocoderStatus.OK){
                $.each(results, function(i, result){
                    searchPoints.push({"lat" : result.geometry.location.Xa, "lng" : result.geometry.location.Ya});
                });
            } else {
                                    alert('nothing found!')
            }
        });
    }

    return false;
});

That code isn't yet finished, of course. Theoretically what I'd like to do is, say, if I search for "4777 Avenue Pierre-de Coubertin", it would find the proper point in my $points array instead of finding that existing address in Montreal AND all the other points in the world with that address.

lat: 45.5
lng: -73.553459
location: "2 Rue de la Commune Ouest, <br />Montreal, QC H2Y 2E2, Canada"
name: "Montreal Science Centre"
pointType: "Community"
url: "/points/item/montreal-science-centre"

lat: 45.514229
lng: -73.531342
location: "4777 Avenue Pierre-de Coubertin<br />Montreal, QC H1V 1B3, Canada"
name: "Montreal Biodome"
pointType: "Curated"
url: "/points/item/montreal-biodome"

I hope this is a bit clearer of a description of what I'm trying to do. Thanks very much.

What my client is wondering is if the users of this site can execute a search to find an existing point from this array.

The answer is yes. What criteria are involved in the search? Data in the infowindow? Closest point? Do you have a database that might do the search more efficiently?

Theoretically what I'd like to do is, say, if I search for "4777 Avenue Pierre-de Coubertin", it would find the proper point in my $points array instead of finding that existing address in Montreal AND all the other points in the world with that address.

You have choices:

  1. search the text in the infowindows for that string (or the properties of your objects for that address, you might want to make a version without the HTML formatting to make the search easier)
  2. geocode the input address and find the closest point in your markers array (assuming the result was in your area of interest).
  3. something else or a combination of the above.

You want to reverse geocode all your addresses and use the harvesine formula to look for proximities. There is also other technology like r-tree, spatial index and space filling curves. Those are also proximity search and can be much faster then a search one-by-one for example with the harvesine formula.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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