【问题标题】:Display marker to google maps向谷歌地图显示标记
【发布时间】:2018-02-01 21:59:30
【问题描述】:

我一直想在谷歌地图上显示一个标记。我已经可以从数据库中获取纬度和经度,但我的问题是它不会显示标记。这是我的代码。

    var map = new google.maps.Map(document.getElementById('map_canvas'), {
        zoom: 15,
        center: new google.maps.LatLng(10.3157, 123.8854),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });


    var locations = [
    <?php foreach($routes as $route){?>
        {
            "title": "{{ $route->destination }}",
            "lat": "{{ $route->lat }}",
            "lng": "{{ $route->lng }}"
        },

    <?php } ?>
   ];
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i].lat,locations[i].lng),
        icon: pin,
        map: map
    })

    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infowindow.setContent(locations[i].title);
            infowindow.open(map, marker);
        }
    })(marker, i));

    console.log(locations[i].lat);
    console.log(locations[i].lng);
}

我尝试对经纬度进行console.log 并成功显示,但标记不会显示。

【问题讨论】:

  • 显示你创建地图的代码
  • @scaisEdge 已经更新了

标签: php google-maps laravel-5 marker


【解决方案1】:

您需要先创建infowindow。 您在点击事件中使用了该变量,但尚未创建。

var map = new google.maps.Map(document.getElementById('map_canvas'), {
    zoom: 15,
    center: new google.maps.LatLng(10.3157, 123.8854),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});


var locations = [
<?php foreach($routes as $route){?>
    {
        "title": "{{ $route->destination }}",
        "lat": "{{ $route->lat }}",
        "lng": "{{ $route->lng }}"
    },

<?php } ?>
];

// Create infowindow here
var infowindow = new google.maps.InfoWindow();

for (i = 0; i < locations.length; i++) {
    marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i].lat,locations[i].lng),
    icon: pin,
    map: map
})

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
        infowindow.setContent(locations[i].title);
        infowindow.open(map, marker);
    }
  })(marker, i));

  console.log(locations[i].lat);
  console.log(locations[i].lng);
}

【讨论】:

  • 是的,我做到了。我就是这样做的var infowindow = new google.maps.InfoWindow();
猜你喜欢
  • 2018-08-04
  • 2014-10-08
  • 2014-05-17
  • 2013-04-20
  • 1970-01-01
相关资源
最近更新 更多