Google Maps The same text appears in the info window when using multiple markers.

I have a problem with multiple markers on google maps. Currently, I have a MySQL database that stores information (location information). In php, I extract this information and go through each zip code to dynamically create the necessary javascript to place a marker for every location in my database.

This works successfully, so I know that I am passing the correct information to the js function. Now what I'm trying to achieve is adding extra information when the marker is clicked, but its display is the same in every marker window.

This is the js I'm using (I start the icon at the top, but exclude this from the code):

function usePointFromPostcode(postcode, callbackFunction, text) {

    localSearch.setSearchCompleteCallback(null, 
        function() {

            if (localSearch.results[0])
            {       
                var resultLat = localSearch.results[0].lat;
                var resultLng = localSearch.results[0].lng;
                var point = new GLatLng(resultLat,resultLng);
                callbackFunction(point, text);
            }else{
                alert("Postcode not found!");
            }
        }); 

    localSearch.execute(postcode + ", UK");
}

function placeMarkerAtPoint(point, html, icon)
{
    var marker = new GMarker(point,{icon: icon});

    GEvent.addListener(marker,"click",function() {
        marker.openInfoWindowHtml(html);
    });

    map.addOverlay(marker);
}

I have php code:

$query = "SELECT * FROM hospitalInfo";
$result = mysql_query($query);

if($result) {
    while ($row = mysql_fetch_assoc($result)) {
        $code .= "usePointFromPostcode('".$row['Postcode']."', placeMarkerAtPoint, 
        '".$row['placeName']."');";

    }
}

$ code then echo'ed.

, , ! !

0
4

, , - , . , , GClientGeocoder, - GLocalSearch, API . Google .

:

var geocoder = new GClientGeocoder();

, usePointFromPostcode():

function usePointFromPostcode(postcode, text) {
    geocoder.getLatLng(postcode, function(point) {
        if (!point) {
            //alert('address not found');
        } else {
            var marker = new GMarker(point, {icon: icon});
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(text);
            });
            map.addOverlay(marker);
        }
    });
}

. , .

, , getLocations() getLatLng(). , .

+1

/, .

:

GEvent.addListener(marker,"click",function() {
    marker.openInfoWindowHtml(html);
});

:

marker.bindInfoWindowHtml(html);

, , setSearchCompleteCallback(). , .

+2

Google Maps. html placeMarkerAtPoint localSearch. Google API , :

GLog

placeMarkerAtPoint:

GLog.write ("placeMarkerAtPoint - " + html);

localSearch:

GLog.write ("SearchCompleteCallback - " + text);

, ( ) , html .

: , PHP-. usePointFromPostcode.

google.search.SearchControl. , , , , ?

, setSearchCompleteCallback. Google AJAX Search, , . , .

+1

You reuse the name marker, so the last text you post becomes attached to all of them. Create an index and name it marker1, marker2, etc. Its easy to do in php loop.

0
source

Source: https://habr.com/ru/post/1716365/


All Articles