gpt4 book ai didi

javascript - 标记信息窗口无法使用 ngMap 工作

转载 作者:行者123 更新时间:2023-12-03 03:01:59 26 4
gpt4 key购买 nike

我正在尝试使用 在谷歌地图上显示信息窗口。这是我的代码:

<div id="map">
<ng-map zoom="13" center="[{{latitude}}, {{longitude}}]" style="display: block; height: 100%;">
<marker ng-repeat="pothole in potholeData" position="{{pothole.lat}},{{pothole.lng}}" on-click="showDetails(e, pothole)"></marker>
</ng-map>
</div>

我在每个标记上提供了一个带有纬度和经度值的点击函数。但在我的 Controller 中,我得到了未定义值。

这是我的 Controller :

$scope.showDetails = function(e, pothole) {
var infowindow = new google.maps.InfoWindow();
var center = new google.maps.LatLng(pothole.lat, pothole.lng);

infowindow.setContent(
'<h3>' + pothole + '</h3>');

infowindow.setPosition(center);
infowindow.open($scope.map);
}

showDetails 函数中,我遇到了pothole undefined。有人可以帮我吗?

最佳答案

问题出在on-click="showDetails(e, pothole),更改为on-click="showDetails(event, pothole)。我已经制作了工作片段。

angular.module('mapApp', ['ngMap'])
.controller('mapController', function($scope, NgMap) {

NgMap.getMap().then(function(map) {
$scope.map = map;
});
$scope.potholeData = [
{ lat: 59.923043, lng: 10.752839 },
{ lat: 59.339025, lng: 18.065818 },
{ lat: 55.675507, lng: 12.574227 },
{ lat: 52.521248, lng: 13.399038 },
{ lat: 48.856127, lng: 2.346525 }
];
$scope.showDetails = function(e, pothole) {
console.log(pothole);
var infowindow = new google.maps.InfoWindow();
var center = new google.maps.LatLng(pothole.lat,pothole.lng);

infowindow.setContent(
'<h3>' + pothole.lat + " " + pothole.lng + '</h3>');

infowindow.setPosition(center);
infowindow.open($scope.map);
};

});
<script src="https://maps.google.com/maps/api/js"></script>
<script src="https://code.angularjs.org/1.3.15/angular.js"></script>
<script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
<div ng-app="mapApp" ng-controller="mapController">
<ng-map default-style="true" zoom="5" center="59.339025, 18.065818">
<marker ng-repeat="pothole in potholeData"
position="{{pothole.lat}},{{pothole.lng}}" on-click="showDetails(event, pothole)">
</marker>
</ng-map>
</div>

关于javascript - 标记信息窗口无法使用 ngMap 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47340469/

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