谷歌地图中的多个标记

时间:2013-10-09 11:33:21

标签: javascript php google-maps google-maps-api-3

我在google map中放置多个标记有问题。我的code.it在页面重新加载时只显示一个标记,当页面重复加载时,不显示地图。
javascript code

<head>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false">
</script>
<script>
function initialize(lat,lon)
{
  var myCenter=new google.maps.LatLng(lat,lon);
  var mapProp = {
  center:myCenter,
  zoom:9,
  mapTypeId:google.maps.MapTypeId.ROADMAP
  };

 var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

 var marker=new google.maps.Marker({
  position:myCenter,
 });
marker.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);
 </script>
</head>
<body>
<div id="googleMap" style="width:500px;height:380px;"></div>
</body>
</html>

php code

<?php
require_once 'geocode.php';

$addarray=array(0=>'a, ahmedabad,india',1=>'b,ahmedabad,india');
foreach($addarray as $add)
{
// define the address and set sensor to false
$opt = array (
    'address' => urlencode($add),
    'sensor'  => 'false'
);

// now simply call the function
$result = getLatLng($opt);

// if status was successful, then print the lat/lon ?
if ($result['status']) {

   echo '<pre>';
?>
 <script>
initialize(<?php echo $result['lat'];?>,<?php echo $result['lon'];?>);
 </script>  

<?php
   echo '</pre>';
}
}
?>

这里首先我根据地址得到了lat和lon然后我调用了javascript函数来放置marker。但是缺少了某些东西或者产生了问题。
提前谢谢。

2 个答案:

答案 0 :(得分:1)

您必须将数组设置为全局变量。另请注意,markers_array是一个包含所有标记纬度和经度的对象,如果你想初始化一个信息窗,还有其他数据。

var markers;
var map;

var myOptions = {
    zoom: 8,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);



for(i=0;i<markers_array.length;i++){
    addMarker(markers_array[i].lat,markers_array[i].long);
}


function addMarker(lat,lng) {
    var myLatlng = new google.maps.LatLng(lat,lng);

    var marker = new google.maps.Marker({
        map: map,
        position: myLatlng,
        name: zip
    });
    markers.push(marker);
 }

LATER EDIT:

以下是json格式的标记数组示例:

[
{
"name":"Marker 1 Italy",
"lat":"46.027482",
"long":"11.114502"
},
{
"name":"Marker 2 France",
"lat":"48.019324",
"long":"3.555908"
},
{
"name":"Marker 3 Spain",
"lat":"40.329796",
"long":"-4.595948"
}
]

答案 1 :(得分:1)

感谢@Tudor Ravoiu的帮助。通过改变一些事情来解决它 我已经在php中以json格式创建了数组并将其传递给javascript。

<?php
require_once 'geocode.php';

$addarray=array(0=>'a,ahmedabad,india',1=>'b,ahmedabad,india',2=>'c,ahmedabad,india');
$lat1=array();
foreach($addarray as $add)
{
// define the address and set sensor to false
$opt = array (
    'address' => urlencode($add),
    'sensor'  => 'false'
);

// now simply call the function
$result = getLatLng($opt);

// if status was successful, then print the lat/lon ?
if ($result['status'])
{ 
   array_push($lat1,array($result['address'],$result['lat'],$result['lon']));
}
}echo json_encode($lat1);
?>

javascript code

<script type="text/javascript">
var locations = <?php echo json_encode($lat1);?>;
var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 10,
  center: new google.maps.LatLng(23.0171240, 72.5330533),
  mapTypeId: google.maps.MapTypeId.TERRAIN
});

var infowindow = new google.maps.InfoWindow();

var marker, i;
for (i = 0; i < locations.length; i++) 
{ 
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
   });

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i][0]);
      infowindow.open(map, marker);
    }
  })(marker, i));
}