1

我正在使用 Google Maps API 的新(ish)v3 创建地图

我已经设法使用如下代码显示地图:

var myLatlng = new google.maps.LatLng(50.8194000,-0.1363000);

    var myOptions = {
        zoom: 14,
        center: myLatlng,
        mapTypeControl: false,
        scrollwheel: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

 var map = new google.maps.Map(document.getElementById("location-map"), myOptions);

但是我现在想添加一些我存储在 PHP 数组中的标记。

如果我将其打印到屏幕上,该数组当前看起来像这样:

Array
(
[0] => Array
    (
        [poiUid] => 20
        [poiName] => Brighton Cineworld
        [poiCode] => brighton-cineworld
        [poiLon] => -0.100450
        [poiLat] => 50.810780
        [poiType] => Cinemas
    )

[1] => Array
    (
        [poiUid] => 21
        [poiName] => Brighton Odeon
        [poiCode] => brighton-odeon
        [poiLon] => -0.144420
        [poiLat] => 50.821860
        [poiType] => Cinemas
    )
)

到目前为止我所做的所有阅读都表明我使用 json_encode 将其转换为 JSON

如果我通过这个运行数组并将其回显到屏幕上,我会得到:

[{"poiUid":"20","poiName":"Brighton Cineworld","poiCode":"brighton-cineworld","poiLon":"-0.100450","poiLat":"50.810780","poiType":"Cinemas"},{"poiUid":"21","poiName":"Brighton Odeon","poiCode":"brighton-odeon","poiLon":"-0.144420","poiLat":"50.821860","poiType":"Cinemas"}]

现在是我苦苦挣扎的地方,我不确定编码的数组是我开始填充标记所需要的,我想我需要类似下面的代码,但不确定如何从通过 JSON 传递的标记中添加标记

var locations = $jsonPoiArray;
  for (var i = 0;i < locations.length; i += 1) {
  // Create a new marker

  };
4

1 回答 1

2

好吧,我想我已经解决了,但是如果这是一种愚蠢的做法,请留下答案!

我最终使用

var poiJson = <? echo $jsonArray ?>;

    for (var i = 0;i < poiJson.length; i += 1) {
        var lat = poiJson[i].poiLat;
        var lon = poiJson[i].poiLon;
        addMarker(lat,lon,i);
    };

    function addMarker(lat,lng,no){
    var latlng = new google.maps.LatLng(lat,lng);  
    var marker = new google.maps.Marker({
    position: latlng,
    map: map
    });
    };
于 2010-04-12T23:28:33.757 回答