在 Vue/Laravel 中使用谷歌地图 [英] Using google map in Vue / Laravel
问题描述
尝试在Vue组件中实现谷歌地图.但是过得很辛苦.实际上,没有错误.但也没有地图 :) 好吧,我在下面尝试了这么多.
Trying to implement google map into Vue component. But having a hard time. Actually, there is no error. But no map also :) Okay, what I tried so far down below.
在 Laravel Blade 中,我设置了我的 api.
In laravel blade I set my api.
<script async defer src="https://maps.googleapis.com/maps/api/js?key={{env('GOOGLE_MAPS_API')}}&callback=initMap"></script>
然后在Vue组件中;
data() {
return {
mapName: "map",
//some other codes
}
},
mounted() {
this.fetchEstates();
},
methods: {
fetchEstates(page = 1) {
axios.get('/ajax', {
params: {
page
}}).then((response) => {
// console.log(response);
this.estates = response.data.data;
//some other codes....
//some other codes....
},
computed: {
//some other functions in computed...
//
initMap: function(){
var options =
{
zoom : 6,
center : {
lat:34.652500,
lng:135.506302
}
};
var map = new google.maps.Map(document.getElementById(this.mapName), options);
var marker = new google.maps.Marker({
map: map,
icon: 'imgs/marker.png',
url: "/pages/estates.id",
label: {
text: this.estates.price,
color: "#fff",
},
position: {
lat: this.estates.lat,
lng: this.estates.lng
}
});
google.maps.event.addListener(marker, 'click', function () {
window.location.href = this.url;
});
}
<div id="map"></div>
和最后一个标记 url
id 绑定在这样的控制器中,
and last marker url
id bind is in controller like this,
public function details($id)
{
$estates = allestates::where('id', $id)->first();
return view('pages.details', compact('estates'));
}
我在 Vue js 中遗漏了什么吗?谢谢!
Do I missing something in Vue js? Thank you!
推荐答案
从我们在评论中的讨论,我意识到你的问题是因为 initMap 时
被执行.请记住,您正在使用异步操作(通过 axios)来填充 this.estates
仍未定义()this.estates
,因此它在运行时是未定义的.你可以做的是:
From our discussion in the comments, I realise that your issue is because this.estates
is still not defined when initMap()
is executed. Remember that you are using an asynchronous operation (via axios) to populate this.estates
, so it is undefined at runtime. What you can do is:
- 保持地图初始化逻辑在
initMap()
- 移动所有 Google 地图标记创建,直到 axios 承诺得到解决.您可以将所有这些抽象为另一种方法,例如
insertMarkers()
另外,记住你需要在应用/组件数据中定义estates
,否则它不会是reactive的.
Also, remember that you need to define estates
in the app/component data, otherwise it will not be reactive.
这是一个例子:
data() {
return {
mapName: "map",
// Create the estate object first, otherwise it will not be reactive
estates: {}
}
},
mounted() {
this.fetchEstates();
this.initMap();
},
methods: {
fetchEstates: function(page = 1) {
axios.get('/ajax', {
params: {
page
}}).then((response) => {
this.estates = response.data.data;
// Once estates have been populated, we can insert markers
this.insertMarkers();
//pagination and stuff...
});
},
// Iniitialize map without creating markers
initMap: function(){
var mapOptions =
{
zoom : 6,
center : {
lat:34.652500,
lng:135.506302
}
};
var map = new google.maps.Map(document.getElementById(this.mapName), mapOptions);
},
// Helper method to insert markers
insertMarkers: function() {
var marker = new google.maps.Marker({
map: map,
icon: 'imgs/marker.png',
url: "/pages/estates.id",
label: {
text: this.estates.price,
color: "#fff",
},
position: {
lat: this.estates.lat,
lng: this.estates.lng
}
});
google.maps.event.addListener(marker, 'click', function () {
window.location.href = this.url;
});
}
},
<小时>
更新:事实证明,您还没有解决this.estates
的数据结构问题.看来您正在从端点接收一个数组而不是对象,因此 this.estates
将返回一个数组,当然 this.estates.lat
将是未定义的.
Update: It also turns out that you have not addressed the issue of the data structure of this.estates
. It appears that you are receiving an array from your endpoint instead of objects, so this.estates
will return an array, and of course this.estates.lat
will be undefined.
如果你想遍历整个数组,你将不得不使用 this.estates.forEach()
在添加标记时遍历每个单独的庄园,即:
If you want to iterate through the entire array, you will have to use this.estates.forEach()
to go through each individual estates while adding the marker, i.e.:
data() {
return {
mapName: "map",
// Create the estate object first, otherwise it will not be reactive
estates: {}
}
},
mounted() {
this.fetchEstates();
this.initMap();
},
methods: {
fetchEstates: function(page = 1) {
axios.get('/ajax', {
params: {
page
}}).then((response) => {
this.estates = response.data.data;
// Once estates have been populated, we can insert markers
this.insertMarkers();
//pagination and stuff...
});
},
// Iniitialize map without creating markers
initMap: function(){
var mapOptions =
{
zoom : 6,
center : {
lat:34.652500,
lng:135.506302
}
};
var map = new google.maps.Map(document.getElementById(this.mapName), mapOptions);
},
// Helper method to insert markers
insertMarkers: function() {
// Iterate through each individual estate
// Each estate will create a new marker
this.estates.forEach(estate => {
var marker = new google.maps.Marker({
map: map,
icon: 'imgs/marker.png',
url: "/pages/estates.id",
label: {
text: estate.price,
color: "#fff",
},
position: {
lat: estate.lat,
lng: estate.lng
}
});
google.maps.event.addListener(marker, 'click', function () {
window.location.href = this.url;
});
});
}
},
这篇关于在 Vue/Laravel 中使用谷歌地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!