使用Google Maps v3,PHP和Json绘制标记 [英] Using Google Maps v3, PHP and Json to plot markers

查看:120
本文介绍了使用Google Maps v3,PHP和Json绘制标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a map using the new(ish) v3 of the Google Maps API

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

I have managed to get a map displaying using code as below:

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数组中。

However I now want to add a number of markers I have stored in a PHP array.

如果我将它打印到屏幕上,Array目前看起来像这样:

The Array currently looks like this if I print it out to screen:

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

All the reading I have done so far suggests I turn this into JSON by using json_encode

如果我通过这种方式运行阵列并将其回显到屏幕上我得到:
$ b

If I run the Array though this and echo it to the screen I get:

[{"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传递

The bit now is where I am struggling, I am not sure the encoded array is what I need to start populating markers, I think I need something like the code below but not sure how to add the markers from my passed through JSON

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

  };


推荐答案

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

Well I think I have solved it, however if it is a stupid way of doing it please leave an answer!

我结束了使用

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

这篇关于使用Google Maps v3,PHP和Json绘制标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆