gpt4 book ai didi

javascript - AngularJS + Google map (多个标记)

转载 作者:行者123 更新时间:2023-11-28 04:10:38 25 4
gpt4 key购买 nike

我已经完成了一个简单的 AngularJS Google map 集成,带有多个标记,但我得到的只是一个“灰色/棕色屏幕”,控制台中没有错误,之前已经从控制台清除了一些代码,但仍然没有运气好。

我不认为这与标记有任何关系,我的想法是可能是谷歌地图导致了问题?

任何帮助都会很棒,我在下面添加了代码预览:

var myApp = angular.module("myApp", []);
myApp.directive("myMaps", function() {
return {
restrict: 'E',
template: '<div></div>',
replace: true,
link: function(scope, element, attrs) {
var mapOptions = {
zoom: 10,
mapTypeId: google.maps.MapTypeId.SATELLITE
};

var cities = [{
lat: 52.238983,
lng: -0.888509
},
{
lat: 52.238168,
lng: -52.238168
},
{
lat: 52.242452,
lng: -0.889882
},
{
lat: 52.247234,
lng: -0.893567
},
{
lat: 52.241874,
lng: -0.883568
}
];

var map = new google.maps.Map(document.getElementById(attrs.id), mapOptions);

cities.forEach(function(value, index) {
var myLatLng = new google.maps.LatLng(value.lat, value.lng);

var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: "My Town"

});

marker.setMap(map);
});
}
};
});
<html>

<head>
<title>Angular.JS and Google Maps</title>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC6GScFE4uWv-c27R_0rlOTApBR7KEK6DM"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
<script src="../scripts/script_test.js"></script>
<link rel="stylesheet" media="screen" href="https://bootswatch.com/darkly/bootstrap.min.css">
<style>
#map {
height: 700px;
width: 100%;
}
</style>
</head>

<body>
<div class="row">
<div class="col-md-12">
<span class="text-center">
<img src="https://s26.postimg.org/pxgc1jo0p/angular-card.png" class="img-responsive" style="width:20%;">
</span>
</div>
</div>
<div ng-app="myApp">
<my-maps id="map"></my-maps>
</div>
</body>

</html>

最佳答案

map 中心也需要指定,可以设置:

如何自动将 map 居中

根据提供的示例, map 可以像这样居中:

var bounds = new google.maps.LatLngBounds();
cities.forEach(function (value, index) {
var myLatLng = new google.maps.LatLng(value.lat, value.lng);
bounds.extend(myLatLng);

var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: "My Town"
});
});
map.fitBounds(bounds);

这是一个修改示例:

var myApp = angular.module("myApp", []);
myApp.directive("myMaps", function () {
return {
restrict: 'E',
template: '<div></div>',
replace: true,
link: function (scope, element, attrs) {
var mapOptions = {
//zoom: 10,
mapTypeId: google.maps.MapTypeId.SATELLITE
};

var cities = [{
lat: 52.238983,
lng: -0.888509
},
{
lat: 52.238168,
lng: -52.238168
},
{
lat: 52.242452,
lng: -0.889882
},
{
lat: 52.247234,
lng: -0.893567
},
{
lat: 52.241874,
lng: -0.883568
}
];

var map = new google.maps.Map(document.getElementById(attrs.id), mapOptions);
var bounds = new google.maps.LatLngBounds();
cities.forEach(function (value, index) {
var myLatLng = new google.maps.LatLng(value.lat, value.lng);
bounds.extend(myLatLng);

var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: "My Town"
});
//marker.setMap(map);
});
map.fitBounds(bounds);
}
};
});
#map {
height: 700px;
width: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
<script src="app.js"></script>
<link rel="stylesheet" media="screen" href="https://bootswatch.com/darkly/bootstrap.min.css">
<div class="row">
<div class="col-md-12">
<span class="text-center">
<img src="https://s26.postimg.org/pxgc1jo0p/angular-card.png" class="img-responsive" style="width:20%;">
</span>
</div>
</div>
<div ng-app="myApp">
<my-maps id="map"></my-maps>
</div>

关于javascript - AngularJS + Google map (多个标记),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46303955/

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