Google地图-使用多个标记时,信息窗口中会显示相同的文本 [英] Google Maps - Same text appears in info window when using multiple markers

查看:90
本文介绍了Google地图-使用多个标记时,信息窗口中会显示相同的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google地图上有多个标记时遇到了问题-我目前有一个MySQL数据库,用于存储信息(位置信息).然后,在php中,我提取此信息并遍历每个邮政编码以动态创建所需的javascript,以在数据库中为每个位置放置一个标记.

I'm having an issue with multiple markers on google maps - I currently have a MySQL database storing info (location info). In php I then extract this info and loop through each postcode to dynamically create the required javascript to place a marker for each place in my database.

这成功地工作了,所以我知道我正在将正确的信息传递给js函数-现在,我要实现的目标是单击标记时添加其他信息,但在每个标记窗口上都显示相同的信息.

This works successfully so I know that I'm passing the correct information to the js function - Now what I'm trying to achieve is adding additional information when the marker is clicked but its showing the same on every marker window.

这是我正在使用的js(我在顶部启动了一个图标,但现在从代码中排除了它):

This is the js I'm using (I initiate an icon at the top but excluded this from the code for now):

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);
}

我拥有的php代码是:

The php code I have is:

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

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

    }
}

然后

$ code被回显.

$code is then echo'ed.

任何关于为什么发生这种情况的建议将不胜感激!谢谢!

Any advice on why this is occuring would be much appreciated! Thanks !

推荐答案

正如您在评论中提到的那样,问题在于您在获得任何结果之前先发送了多个请求,并且标记文本的值每次都会更改您发送请求.我认为您可以使用GClientGeocoder大大简化您的代码-除非绝对需要使用GLocalSearch,而这实际上并不是Maps API的一部分.这是针对地理编码器的 Google教程.

As you mentioned in your comment, the problem is that you're sending several requests before you get any results, and the value of the marker text changes each time you send the request. I think you could greatly simplify your code by using the GClientGeocoder - unless it's absolutely necessary to use GLocalSearch, which isn't really part of the Maps API. Here's Google's tutorial for the geocoder.

首先像这样创建地址解析器:

First create the geocoder like this:

var geocoder = new GClientGeocoder();

下一步,这是您的新 usePointFromPostcode()函数:

Next, here's your new usePointFromPostcode() function:

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);
        }
    });
}

这对我来说很棒.试试看,让我们知道它的进展.

This worked great for me. Try it out and let us know how it goes.

如果您需要有关返回点的更多信息(例如准确性),请使用

If you need more information about the returned point, like accuracy, use getLocations() instead of getLatLng(). The tutorial explains how it works.

这篇关于Google地图-使用多个标记时,信息窗口中会显示相同的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆