mercredi 1 juillet 2015

Find polygon for place in KML

NOTE: Please make sure you read my updates at the bottom!!


I have a KML file that I load 'onto' a Google Map; I also have a searchbox where users can search for a city. When the city is found I place a marker which, in most (if not all) cases should fall in one of the polygons defined in the KML.

I can click a polygon and it shows an info-popup with the areacode for that area; however: when a marker is placed I would like to have this info-popup shown automatically (and possibly other(s) that are shown before placing the marker hidden).

I have looked over the Maps V3 documentation but was unable to find anything. Is this possible?

You can view the project at http://ift.tt/1f38EtI, the source can be found at http://ift.tt/1dz7lRG

The relevant (snippet of) code is:

google.maps.event.addListener(autocomplete, 'place_changed', function () {
    var place = autocomplete.getPlace();
    if (!place.geometry)
        return;

    // Remove any existing markers
    for (var i = 0, marker; marker = markers[i]; i++)
        marker.setMap(null);
    markers = [];

    // Create a marker for place.
    var marker = new google.maps.Marker({
        map: map,
        icon: image,
        title: place.name,
        position: place.geometry.location
    });
    markers.push(marker);

    // ... Here I should figure out in which polygon the marker is positioned
    // ... and preferrably open/display the info-window which is shown when a
    // ... polygon is clicked.

    // Scroll (pan) to marker
    map.panTo(place.geometry.location);
});

Also, as a side-question: autocomplete.getPlace(); always returns an object; what is the best way to find out if the object is an actual (useful) place? When I search for xxxx for example, getPlace() returns Object {name: "xxxx"}, an actual result (like searching for Amsterdam) returns Object {address_components: Array[4], adr_address: "<span class="locality">Amsterdam</span>, <span class="country-name">Nederland</span>", …}. I currently use if (!place.geometry) to find out if the place is useful / an actual place; however I guess there's a better way ('best practice'?) to do this?


Edit 1: just stumbled across With Google Maps API V3, determine if a marker is inside a KML Layer boundary. Currently looking into it. However, I should probably note that the KML is hosted by a 3rd party and updated at will by them; as I use static site hosting I prefer not to have a separate process to extract the coordinates from polygons from the KML file. I prefer to "read" the KML (in)directly.


Edit 2: I moved from (direct) KML to a FusionTables based solution. I can now "highlight" polygons where the place is in. Now all I (still) need is a way to figure out how to show it's InfoWindow. I'll look into that tomorrow; it seems I need to query (again) into a DataTable or something and get the label info that way.


Edit 3: Solved!

Aucun commentaire:

Enregistrer un commentaire