簡體   English   中英

更改for循環以適合json-array?

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

我從一個教程中獲得了這段代碼,它功能齊全:

  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);
        });
    }
}

它在地圖上顯示帶有數組信息的標記。

我想更改for循環,使其適合我的數組。 因為當我用我的陣列嘗試這個時,地圖甚至不顯示..

<?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); ?>;

完全按照原樣打印

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

但它不適用於地圖代碼。

我需要以某種方式更改循環,我該怎么做?

實際上,您根本不需要循環,因為您只定義了一個站點(盡管變量名稱為“ 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