Laravel和谷歌地图:foreach纬度/经度显示标记或地图 [英] Laravel and google maps : foreach latitude/longitude show marker or map

查看:225
本文介绍了Laravel和谷歌地图:foreach纬度/经度显示标记或地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对于每个位置,我想要一个新的地图(或更好的一个新的标记),它使用纬度和纬度从表格位置的经度在地图上显示一个城市。

控制器的索引动作:

pre $ public function index()
{
$ locations = Location :: all();

$ locations-> location_latitude = Input :: get('location_latitude');
$ locations-> location_longitude = Input :: get('location_longitude');

返回视图:: make('forecasts.index')
- > with('locations',$ locations);

$ / code $ / $ p

google map with marker:

 函数初始化(){

var map_canvas = document.getElementById('map_canvas');
$ b var location = new google.maps.LatLng({{location-> location_latitude}},{{location-> location_longitude}});

var map_options = {
center:location,
zoom:10,
mapTypeId:google.maps.MapTypeId.ROADMAP
}

var map = new google.maps.Map(map_canvas,map_options)

var marker = new google.maps.Marker({
position:location,$ b $ map :map,
});

marker.setMap(map);


$ b $ google.maps.event.addDomListener(window,'load',initialize);

当我这样做的时候,它只显示上次插入纬度/经度的城市:


$ b $

  @foreach($ locations为$ location)

var location = new google.maps.LatLng({{$ location-> location_latitude}},{{$ location-> location_longitude}});

@endforeach


解决方案

可能是因为在循环收集时,实际上并没有创建单独的标记。

 函数initialize(){

var map_canvas = document.getElementById('map_canvas');

//初始化地图
var map_options = {
center:location,
zoom:10,
mapTypeId:google.maps.MapTypeId.ROADMAP

var map = new google.maps.Map(map_canvas,map_options)

//将所有位置放入数组
var locations = [
@foreach($ location为$ location)
[{{$ location-> location_latitude}},{{$ location-> location_longitude}}]
@endforeach
]; (i = 0; i< locations.length; i ++){
var location = new google.maps.LatLng(locations [i] [0],locations [i])的

, [1]);

var marker = new google.maps.Marker({
position:location,
map:map,
});
}

// marker.setMap(map); //可能没有必要,因为你把地图设置在



I have a table locations with a latitude and longitude field.

For each location I want a new map (or better a new marker) that uses the latitude and longitude from the table locations to display a city on the map.

index action of controller :

public function index()
    {
        $locations = Location::all();

        $locations->location_latitude = Input::get('location_latitude');
        $locations->location_longitude = Input::get('location_longitude');

        return View::make('forecasts.index')
            ->with('locations', $locations);
    }

google map with marker :

function initialize() {

    var map_canvas = document.getElementById('map_canvas');

    var location = new google.maps.LatLng({{ $location->location_latitude }}, {{ $location->location_longitude }});

    var map_options = {
        center: location,
        zoom: 10,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var map = new google.maps.Map(map_canvas, map_options)

    var marker = new google.maps.Marker({
        position: location,
        map: map,
    });

    marker.setMap(map);

  }

  google.maps.event.addDomListener(window, 'load', initialize);

When I do this it only displays the city from the last inserted latitude/longitude :

@foreach($locations as $location)

   var location = new google.maps.LatLng({{ $location->location_latitude }}, {{ $location->location_longitude }});

@endforeach

解决方案

It is probably because while looping through the collection you are not actually creating separate marker.

function initialize() {

    var map_canvas = document.getElementById('map_canvas');

    // Initialise the map
    var map_options = {
        center: location,
        zoom: 10,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(map_canvas, map_options)

    // Put all locations into array
    var locations = [
    @foreach ($locations as $location)
        [ {{ $location->location_latitude }}, {{ $location->location_longitude }} ]     
    @endforeach
    ];

    for (i = 0; i < locations.length; i++) {
        var location = new google.maps.LatLng(locations[i][0], locations[i][1]);

        var marker = new google.maps.Marker({
            position: location,
            map: map,
        }); 
    }

    // marker.setMap(map); // Probably not necessary since you set the map above

}

这篇关于Laravel和谷歌地图:foreach纬度/经度显示标记或地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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