Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.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地图标记(Javascript)_Javascript_Php_Arrays_Google Maps - Fatal编程技术网

将PHP数组传递给Google地图标记(Javascript)

将PHP数组传递给Google地图标记(Javascript),javascript,php,arrays,google-maps,Javascript,Php,Arrays,Google Maps,我将数据存储到一个php数组中,并尝试将其传递给javascript,以使google maps显示php数组中的所有点。下面是简化的php代码和谷歌地图的javascript代码 ---PHP代码---- foreach(json_decode($response)->data as $item){ $name= $item->user->name; $latitude = $item->latitude; $longitude = $item->l

我将数据存储到一个php数组中,并尝试将其传递给javascript,以使google maps显示php数组中的所有点。下面是简化的php代码和谷歌地图的javascript代码

---PHP代码----

foreach(json_decode($response)->data as $item){ 
$name= $item->user->name;
   $latitude = $item->latitude;
   $longitude = $item->longitude;
$Array[$count] = array('name'=>$name,'lat'=>$latitude, 'long'=>$longitude, 'rgb'=>$count);
$js_array = json_encode($Array);
$count++;}
---javascript

<script type="text/javascript">   
 var locations = <?php echo $js_array;?>;    
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 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));
}
更新的JAVASCRIPT


在更新的javascript中,需要删除PHP中已经存在的json_编码。你做了两次

只有json_对循环后的最终数组进行编码。哪个部分不工作,或者您遇到了什么具体问题?地图显示,但没有一个标记在控制台中显示为“第一次查看”,可能抛出了错误。您没有引用lat/lng ie位置[i]的属性。lat和位置[i]。如果没有看到代码,很难提供帮助。记录循环中的位置[i],然后检查它。我假设您已经查看了最终的源输出,并且很久以前就通过了该点。祝你好运我已经到了这个地步,在这方面我无能为力了
var locations = <?php echo json_encode($js_array);?>;

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 10,
  center: new google.maps.LatLng(<?php echo $latitude;?>, <?php echo $longitude;?>),
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

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

var marker, i;

for (i = 0; i < locations.length; i++) {  
console.log(locations[i].lat);
    marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i].lat, locations[i].long),
    map: map
  });

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