Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何显示PHP数组中的多个google地图标记?_Php_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

如何显示PHP数组中的多个google地图标记?

如何显示PHP数组中的多个google地图标记?,php,javascript,google-maps,google-maps-api-3,Php,Javascript,Google Maps,Google Maps Api 3,我试图从包含lat和long值的PHP数组在googlemap上显示标记 PHP数组目前看起来如下所示: Array ( [0] => Array ( [0] => 55.8490116, -4.222272800000042 [1] => 54.5979369, -7.274279400000069 ) [1] => Array ( [0] => 55.8490116, -4.2222728

我试图从包含lat和long值的PHP数组在googlemap上显示标记

PHP数组目前看起来如下所示:

Array
(
[0] => Array
    (
        [0] => 55.8490116, -4.222272800000042
        [1] => 54.5979369, -7.274279400000069
    )

[1] => Array
    (
        [0] => 55.8490116, -4.222272800000042
    )

)
然后,我将此数组添加到PHP上的JS数组中,如下所示:

<script type="text/javascript">
var markers = <?php echo $myarray; ?>;
</script>

var标记=;
然后在地图的我的JS中,它看起来像:

var infowindow = new google.maps.InfoWindow();
var marker, i;
var bounds = new google.maps.LatLngBounds();

console.log(markers);

for (i = 0; i < markers.length; i++) { 

    var pos = new google.maps.LatLng(markers[1][0]);

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

    google.maps.event.addListener(marker, 'click', (function(marker, i) {

        return function() {
            infowindow.setContent(markers[1][0]);
            infowindow.open(map, marker);
        }

    })(marker, i));

}
var infowindow=new google.maps.infowindow();
var标记,i;
var bounds=new google.maps.LatLngBounds();
控制台日志(标记);
对于(i=0;i
我不知道下一步该去哪里。我知道我的数组没有模仿GoogleMapsAPI所要求的内容,但不确定如何更改它以适应需要

谢谢
罗伯特

我认为你的方法的问题在于对阵列的回声

我试图修复它的方法是在JS中创建一个名为
标记
的空数组,循环遍历PHP数组的所有值,然后将每个值附加到
标记

还要注意,在PHP中有一个数组的数组,因此需要一个嵌套循环

如果您需要任何代码示例,请告诉我,否则这就足够了。

您可以这样做:

<?php
$marker[] = array(
                "lat"=> '17.454000',
                "lng"=> '78.434952');
$marker[] = array(
                "title"=> 'shilparamam',
                "lat"=> '17.452665',
                "lng"=> '78.435608',
                "description"=> 'Mumbai formerly Bombay, is the capital city of the Indian state of Maharashtra.');
$marker[] = array(
                "title"=> 'image hospitals',
                "lat"=> '17.452421',
                "lng"=> '78.435715',
                "description"=> 'Pune is the seventh largest metropolis in India, the second largest in the state of Maharashtra after Mumbai.');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>    
    <script type="text/javascript">
        <?php
        $objJSON = json_encode($marker);
        echo "var markers  = ". $objJSON . ";\n";
        ?>
        window.onload = function () {
            var mapOptions = {
                center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
                zoom: 10,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
            var infoWindow = new google.maps.InfoWindow();
            var lat_lng = new Array();
            var latlngbounds = new google.maps.LatLngBounds();
            for (i = 0; i < markers.length; i++) {
                var data = markers[i]
                var myLatlng = new google.maps.LatLng(data.lat, data.lng);
                lat_lng.push(myLatlng);
                var marker = new google.maps.Marker({
                    position: myLatlng,
                    map: map,
                    title: data.title
                });
                latlngbounds.extend(marker.position);
                (function (marker, data) {
                    google.maps.event.addListener(marker, "click", function (e) {
                        infoWindow.setContent(data.description);
                        infoWindow.open(map, marker);
                    });
                })(marker, data);
            }
            map.setCenter(latlngbounds.getCenter());
            map.fitBounds(latlngbounds);

        }
    </script>
    <div id="dvMap" style="width: 500px; height: 500px">
    </div>
</body>
</html>

window.onload=函数(){
变量映射选项={
中心:新建google.maps.LatLng(标记[0].lat,标记[0].lng),
缩放:10,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“dvMap”)、mapOptions);
var infoWindow=new google.maps.infoWindow();
var lat_lng=新阵列();
var latlngbounds=new google.maps.latlngbounds();
对于(i=0;i
我发布的答案有什么好运气吗?你还没有回复让我知道它是否有效。