如何使用PHP在谷歌地图上显示标记

时间:2013-02-08 10:37:44

标签: php codeigniter google-maps

我需要使用PHP codeigniter从数据库中显示标记 我不会得到地图

function initialize() {
    var myLatLng = new google.maps.LatLng(18.5236, 73.8478);
    var mapOptions = {
        zoom: 14,
        center: myLatLng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    // Add 5 markers to the map at random locations.
    var southWest = new google.maps.LatLng(18.5236,73.8478);
    var northEast = new google.maps.LatLng(19.5236,75.8478);

    var bounds = new google.maps.LatLngBounds(southWest,northEast);
    map.fitBounds(bounds);
    var lngSpan = northEast.lng() - southWest.lng();
    var latSpan = northEast.lat() - southWest.lat();


//If I comment below line it show blank map

    beaches =  [ <?php foreach($records as $row) echo "[".$row->property_title.', '.$row->property_latitude.', '.$row->property_longitude.'],'?>]"

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

2 个答案:

答案 0 :(得分:2)

如果$row->property_title是一个字符串,则需要在引号之间:

echo "['".$row->property_title."', ".$row->property_latitude.', '.$row->property_longitude.'],'?>]"

或者,您可以使用json_encode输出数组:

<?php
$beaches = array();
foreach ($records as $row) {$beaches[] = array($row->property_title, $row->property_latitude, $row->property_longitude);}
echo 'beaches = ' . json_encode($beaches) . ';';
?>

答案 1 :(得分:0)

    $beaches = "['".$row->property_title."', '".$row->property_latitude."', '".$row->property_longitude."']";

var beaches=[<?php echo $beaches;?>];