简体   繁体   English

更改for循环以适合json-array?

[英]Change the for-loop to fit a json-array?

i got this code from a tutorial and it is fully functional: 我从一个教程中获得了这段代码,它功能齐全:

  var sites = [
    ['Mount Evans', 39.58108, -105.63535, 4, 'This is Mount Evans.'],
    ['Irving Homestead', 40.315939, -105.440630, 2, 'This is the Irving Homestead.'],
    ['Badlands National Park', 43.785890, -101.90175, 1, 'This is Badlands National Park'],
    ['Flatirons in the Spring', 39.99948, -105.28370, 3, 'These are the Flatirons in the spring.']
];



    function setMarkers(map, markers) {

        for (var i = 0; i < markers.length; i++) {
            var sites = markers[i];
            var siteLatLng = new google.maps.LatLng(sites[1], sites[2]);
            var marker = new google.maps.Marker({
                position: siteLatLng,
                map: map,
                title: sites[0],
                html: sites[4]



     });

        var contentString = "Some content";

        google.maps.event.addListener(marker, "click", function () {
            infowindow.setContent(this.html);
            infowindow.open(map, this);
        });
    }
}

it displays markers on a map with the information from the array. 它在地图上显示带有数组信息的标记。

I want to change the for-loop so it fits my array. 我想更改for循环,使其适合我的数组。 because when i try this with my array, the maps dont even show up.. 因为当我用我的阵列尝试这个时,地图甚至不显示..

<?php

include_once'config/connect.php';

$search= $_POST['search'];
$search = stripslashes($search);

$search = mysql_real_escape_string($search);

$sql= "select * from venue where vID in (select vID from sv where sID in (select sID from sports where sN = '$search'));";

$result=mysql_query($sql)or die(mysql_error()); 

 $data1 = mysql_fetch_array($result);
 $array = array($data1['latitude'], $data1['longitude'], $data1['venue']);

echo json_encode($array);
?> 


    var sites = <?php echo json_encode($array); ?>;

this prints exactly as it should 完全按照原样打印

sites[0] = 57.7865 //latitude
sites[1] = 11.7986 //longitude
sites[2] = fjaderborgen //venue

but it doesnt work with the map code. 但它不适用于地图代码。

I need to change the loop somehow, how can i do this? 我需要以某种方式更改循环,我该怎么做?

Actually, you don't want a loop at all, since you're only defining one site (despite the variable name " sites "). 实际上,您根本不需要循环,因为您只定义了一个站点(尽管变量名称为“ sites ”)。

var siteLatLng = new google.maps.LatLng(sites[0], sites[1]);
var marker = new google.maps.Marker({
  position: siteLatLng,
  map: map,
  title: sites[2],
  html: "This is " + sites[2]
});

google.maps.event.addListener(marker, "click", function () {
  infowindow.setContent(this.html);
  infowindow.open(map, this);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM