简体   繁体   English

谷歌地图从 php 源添加标记

[英]google maps add markers from php source

I am trying to add an array of markers generated in php to a google map.我正在尝试将在 php 中生成的标记数组添加到谷歌地图。

Here is my map code (js):这是我的地图代码(js):

var map;
var markers = [];
var mapOptions = {
    //center: new google.maps.LatLng(locations[0].lat, locations[0].lng),
    center: new google.maps.LatLng(49.6327939,15.4174414),
    minZoom: 2,
    zoom: 2,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    disableDefaultUI: true,
    scrollwheel: 0,
    keyboardShortcuts: false,
    restriction: {
        latLngBounds: {
            north: 85,
            south: -85,
            west: -180,
            east: 180
        }
    },
};


map = new google.maps.Map(document.getElementById("propertymap"), mapOptions);     
var infowindow  = new google.maps.InfoWindow();
var latlngbounds = new google.maps.LatLngBounds();
var marker, i;

   
for (i = 0; i < locations.length; i++) {
   // var data = locations[i];
    var myLatlng = new google.maps.LatLng(locations[i][1], locations[i][2]);
    marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: locations[i][0],
        icon: mapPin,
    });
    markers.push(marker);
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            for (var j = 0; j < markers.length; j++) {
                markers[j].setIcon(mapPin);
            }
            infowindow.setContent("<div>" + locations[i].description + "</div>");
            this.setIcon(mapPinActive)
            infowindow.open(map, marker);
        }
    })(marker, i));
    latlngbounds.extend(marker.position);
}

If i manually insert the locations like so, then they go into the map as expected.如果我像这样手动插入位置,那么它们会按预期进入地图。

var locations = [
    ['ZADUN, A RITZ-CARLTON RESERVE RESIDENCE', 23.06954546, -109.6484434],
    ['Coogee Beach', -33.923036, 151.259052, 5],
    ['Cronulla Beach', -34.028249, 151.157507, 3],
    ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
    ['Maroubra Beach', -33.950198, 151.259302, 1]
  ];

However if i try and get them from a php souce via ajax, then the markers simply do now show on the map, i have even tried to parse the data:但是,如果我尝试通过 ajax 从 php 源中获取它们,那么标记现在只是显示在地图上,我什至尝试解析数据:

markersURL = url;
    $.ajax({
        url:markersURL,
        success:function(data){
            //locations = JSON.parse(data);
            //console.log(locations);
            //console.log(data);
            locations = data;
        }
    })

The PHP file looks like so: PHP 文件如下所示:

$markers[] = array('title' => get_the_title(), 'lat' => $lat, 'lng' => $lng);

echo json_encode($markers);

The output from the php file looks like so: php 文件的输出如下所示:

[
  {
    "title": "Location 1",
    "lat": "23.06954546",
    "lng": "-109.6484434"
  },
  {
    "title": "Location 2",
    "lat": "35.78127686",
    "lng": "-5.76759696"
  },
  {
    "title": "Location 3",
    "lat": "25.7974489",
    "lng": "-80.12701864"
  }
]

Any suggestion on if the structure of the data is right?关于数据结构是否正确的任何建议? I have tried json parse, and also not encoding the data, but everything i try the data doesnt seem correctly formatted for the markers to show.我已经尝试过 json 解析,也没有对数据进行编码,但是我尝试的所有数据似乎都没有正确格式化以显示标记。

If you're locations var contains an Array of object like you're last example.如果您的locations var 包含一个对象数组,就像您是最后一个示例一样。 You should be able to access to object properties inside your for loop, moreover I didn't test it but I think that google maps latLng method needs numbers as params not string so you certainly need to parse your string coordinates to float :您应该能够访问 for 循环中的对象属性,而且我没有对其进行测试,但我认为 google maps latLng方法需要将数字作为参数而不是字符串,因此您当然需要将字符串坐标解析为 float :

...
for (i = 0; i < locations.length; i++) {
   // var data = locations[i];
    var myLatlng = new google.maps.LatLng(parseFloat(locations[i].lat), parseFloat(locations[i].lng));
    marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: locations[i].title,
        icon: mapPin,
    });
    markers.push(marker);
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            for (var j = 0; j < markers.length; j++) {
                markers[j].setIcon(mapPin);
            }
            infowindow.setContent("<div>" + locations[i].description + "</div>");
            this.setIcon(mapPinActive)
            infowindow.open(map, marker);
        }
    })(marker, i));
    latlngbounds.extend(marker.position);
}

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

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