gpt4 book ai didi

带有多个标记的 AngularJS 谷歌地图

转载 作者:行者123 更新时间:2023-12-04 16:52:12 24 4
gpt4 key购买 nike

我目前正在开发一个 Ionic Framework (AngularJS) 项目,该项目使用地理位置和谷歌地图来显示用户的位置。

我正在尝试显示用户地理位置和该区域周围的多个标记。

我的地理位置工作正常,但似乎无法添加多个标记。

地点

var markers = [
{'London Eye, London', 51.503454,-0.119562},
{'Palace of Westminster, London', 51.499633,-0.124755}
];

Controller .JS
// 1. Google Map // 
FCCAppCtrl.controller('MapController', function($scope, $ionicLoading) {
$scope.initialise = function() {
var myLatlng = new google.maps.LatLng(37.3000, -120.4833);
var mapOptions = {
center: myLatlng,
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);

navigator.geolocation.getCurrentPosition(function(pos) {
map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
var myLocation = new google.maps.Marker({
position: new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude),
map: map,
animation: google.maps.Animation.DROP,
title: "My Location"
});
});
$scope.map = map;
};
google.maps.event.addDomListener(document.getElementById("map"), 'load', $scope.initialise());
});

最佳答案

// 1. Google Map // 
var cities = [
{
city : 'Location 1',
desc : 'Test',
lat : 52.238983,
long : -0.888509
},
{
city : 'Location 2',
desc : 'Test',
lat : 52.238168,
long : -52.238168
},
{
city : 'Location 3',
desc : 'Test',
lat : 52.242452,
long : -0.889882
},
{
city : 'Location 4',
desc : 'Test',
lat : 52.247234,
long : -0.893567
},
{
city : 'Location 5',
desc : 'Test',
lat : 52.241874,
long : -0.883568
}
];

FCCAppCtrl.controller('MapController', function($scope, $ionicLoading) {
// Map Settings //
$scope.initialise = function() {
var myLatlng = new google.maps.LatLng(37.3000, -120.4833);
var mapOptions = {
center: myLatlng,
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
// Geo Location /
navigator.geolocation.getCurrentPosition(function(pos) {
map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
var myLocation = new google.maps.Marker({
position: new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude),
map: map,
animation: google.maps.Animation.DROP,
title: "My Location"
});
});
$scope.map = map;
// Additional Markers //
$scope.markers = [];
var infoWindow = new google.maps.InfoWindow();
var createMarker = function (info){
var marker = new google.maps.Marker({
position: new google.maps.LatLng(info.lat, info.long),
map: $scope.map,
animation: google.maps.Animation.DROP,
title: info.city
});
marker.content = '<div class="infoWindowContent">' + info.desc + '</div>';
google.maps.event.addListener(marker, 'click', function(){
infoWindow.setContent('<h2>' + marker.title + '</h2>' + marker.content);
infoWindow.open($scope.map, marker);
});
$scope.markers.push(marker);
}
for (i = 0; i < cities.length; i++){
createMarker(cities[i]);
}

};
google.maps.event.addDomListener(document.getElementById("map"), 'load', $scope.initialise());

});

关于带有多个标记的 AngularJS 谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29891296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com