0

我在我的房地产网站中使用谷歌地图来显示不同位置的项目

位置以绿色标记显示,如下链接所示

地图链接

var markers = new Array();
var i;


function DrawMap(locations)
{   
  var map = new google.maps.Map(document.getElementById('map'), {
              zoom: 10,
              center: new google.maps.LatLng(-33.92, 151.25),
              mapTypeId: google.maps.MapTypeId.ROADMAP
        });

var flagIcon_front = new google.maps.MarkerImage("images/marker.png");

var flagIcon_shadow = new google.maps.MarkerImage("images/marker_shadow.png")
flagIcon_shadow.size = new google.maps.Size(105, 53);
flagIcon_shadow.anchor = new google.maps.Point(20, 52);         
var boxText = document.createElement("div");
boxText.style.cssText = "border: 1px solid black; margin-top: 8px; padding: 5px; display:block; ";

var myOptions = {
content: boxText
,disableAutoPan: false
,maxWidth: 0
,pixelOffset: new google.maps.Size(-261, -268)
,zIndex: null
,boxStyle: { 
background: "url('images/metro-plot-bg-1.png') no-repeat -286px -1361px"
,opacity: 1
,width: "393px"
,height: "233px"
}
,closeBoxMargin: "11px 32px 2px 2px"
,closeBoxURL: "images/close_small.png"
,infoBoxClearance: new google.maps.Size(1, 1)
,isHidden: false
,pane: "floatPane"
,enableEventPropagation: false              
};  
var infowindow = new google.maps.InfoWindow();  
var marker;
for (i = 0; i < locations.length; i++) 
{  
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
icon: flagIcon_front,
shadow: flagIcon_shadow,         
map: map
});       
markers.push(marker);
}
for(i=0;i<locations.length;i++) 
{  
 marker1 = markers[i];
google.maps.event.addListener(marker1, 'click', (function(marker1, i) {
return function() {
          //infowindow.setContent(locations[i][0]);
          //infowindow.open(map, marker);
          ib.setContent(locations[i][0]);
          ib.open(map, marker1);
          }
      })(marker, i));
 //marker.setMap(null);
var ib = new InfoBox(myOptions);
//ib.open(map, marker);
}   
}

var locations = [
                              ['<div class="map_view"></div>', -33.890542, 151.274856, 4],
                              ['<div class="map_view"></div>', -33.923036, 151.259052, 5],
                              ['<div class="map_view"></div>', -34.028249, 151.157507, 3],
                              ['<div class="map_view"></div>', -33.80010128657071, 151.28747820854187, 2],
                              ['<div class="map_view"></div>', -33.950198, 151.259302, 1]
                            ];



                            $('document').ready(function(e) {
                               DrawMap(locations);
                            });

上面的 DrawMap() 函数以纬度和经度为参数

 var locations = [
                              ['<div class="map_view"></div>', -33.890542, 151.274856, 4],
                              ['<div class="map_view"></div>', -33.923036, 151.259052, 5],
                              ['<div class="map_view"></div>', -34.028249, 151.157507, 3],
                              ['<div class="map_view"></div>', -33.80010128657071, 151.28747820854187, 2],
                              ['<div class="map_view"></div>', -33.950198, 151.259302, 1]
                            ];



                            $('document').ready(function(e) {
                               DrawMap(locations);
                            });

页面第一次加载时地图工作正常。所有标记都以正确的方式显示。但是当我通过提供纬度和经度作为参数来填充地图时,我得到了按钮上的 ajax 调用的 responseText,地图显示标记在一个地方分组。

ajax调用的脚本如下

function getLocations()
{
  for(i=0;i<locations.length;i++) 
  {
    markers[i].setMap(null);
  }
  url = 'map.php';      
  $.post(
  url,
  {
   "act" : "getLocations"   
  },
  function(responseText){                                       
   DrawMap(responseText);
  },
  "html"
);
}

工作副本在以下链接中上传

地图链接

单击底部的按钮以获取纬度和经度作为来自 ajax 请求的响应文本

请让我知道我的哪些标记未重绘到新位置

提前致谢

4

1 回答 1

1

在按钮单击中,您正在调用 $.post,然后将其发送responseText到该DrawMap方法。DrawMap期待一个 javascript 数组,而不是 JSON 文本,因此您可能希望像这样调用该方法:

function(responseText) {
    DrawMap($.parseJSON(responseText));
}

此外,您从服务器传递的不是 jQuery 可以解析的 JSON。以机智:

jQuery.parseJSON docs:
    ...
    {'test': 1} ('test' is using single quotes instead of double quotes).
    ...

您的 JSON 代码如下所示:

[
    ['<div class="map_view"></div>', -33.790542, 151.274856, 4],
    ['<div class="map_view"></div>', -33.983036, 151.259052, 5],
    ['<div class="map_view"></div>', -34.128249, 151.157507, 3],
    ['<div class="map_view"></div>', -33.85010128657071, 151.28747820854187, 2],
    ['<div class="map_view"></div>', -33.900198, 151.259302, 1]     
]

尝试更改引号,如下所示:

[
    ["<div class='map_view'></div>", -33.790542, 151.274856, 4],
    ["<div class='map_view'></div>", -33.983036, 151.259052, 5],
    ["<div class='map_view'></div>", -34.128249, 151.157507, 3],
    ["<div class='map_view'></div>", -33.85010128657071, 151.28747820854187, 2],
    ["<div class='map_view'></div>", -33.900198, 151.259302, 1]     
]
于 2012-11-16T13:00:29.943 回答