gpt4 book ai didi

angularjs - Angular-ui ui-map 点击事件未收到 $params

转载 作者:行者123 更新时间:2023-12-04 12:41:25 26 4
gpt4 key购买 nike

我正在尝试使用 ui.Map ( http://angular-ui.github.io/ui-map/ ) 在 Angularjs 中实现 Google map

我非常仔细地遵循了示例并加载了 map ,我可以在 map 中心创建一个标记,并且 'map-tilesloaded' 事件工作正常。

我的问题是在用户点击的地方添加一个标记。 click 函数正在接收一个空的 $params 参数。在我的 Controller 中:

$scope.newMapOptions = {
center : new google.maps.LatLng($scope.position.lat, $scope.position.lng),
zoom : 18,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
$scope.getLocation = function() {
if (navigator.geolocation) {
return navigator.geolocation.getCurrentPosition(setPosition);
}
};

$scope.addMarker = function($event, $params) {
$scope.newTingMarker = new google.maps.Marker({
map : $scope.myNewTingMap,
position : $params[0].latLng
});
};

$scope.initMap = function() {
if (!$scope.mapLoaded)
$scope.getLocation();
$scope.mapLoaded = true;
};

function setPosition(pos) {
$scope.position = {
lat : pos.coords.latitude,
lng : pos.coords.longitude
};
$scope.meMarker = new google.maps.Marker({
map : $scope.myNewTingMap,
position : new google.maps.LatLng($scope.position.lat, $scope.position.lng)
});

$scope.myNewTingMap.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
$scope.$apply();
}

html:
<div ui-map-info-window="myInfoWindow">
<b>Current location</b>
</div>
<div ui-map-marker="meMarker" ></div>
<div ui-map-marker="newTingMarker" ui-event="{'map-click': 'openMarkerInfo(newTingMarker)'}"></div>
<section id="newTingMap" >
<div ui-map="myNewTingMap" ui-options="newMapOptions" class="map-canvas"
ui-event="{'map-tilesloaded': 'initMap()', 'map-click': 'addMarker($event, $params)' }"></div>
</section>

$scope.addMarker 应该接收 $event 和 $params ,其中 $params[0] 具有 latlng 对象。目前它是一个空数组:[]

我使用的是 angular 1.1.5,但我尝试使用与 ui.Map 示例相同的方法,但没有任何效果。

我还应该注意,这是在一个 View 中,但将它放在主 Controller 中的 View 之外没有任何区别。

如果我尝试遵循从 ui-map 指令运行的代码,我可以看到 latlng 对象确实在事件中启动:

ui-map.js:
 angular.forEach(eventsStr.split(' '), function (eventName) {
//Prefix all googlemap events with 'map-', so eg 'click'
//for the googlemap doesn't interfere with a normal 'click' event
google.maps.event.addListener(googleObject, eventName, function (event) {
element.triggerHandler('map-' + eventName, event);
//We create an $apply if it isn't happening. we need better support for this
//We don't want to use timeout because tons of these events fire at once,
//and we only need one $apply
if (!scope.$$phase){ scope.$apply();}
});
});

element.triggerHandler('map-' + eventName, event); ...在“事件”中有 latlng 对象,但在那之后似乎丢失了

最佳答案

不确定你的问题是什么,我拿了你的代码并创建了一个 fiddle效果很好(你应该做的事情)。

当您单击该日志时,我做了一个控制台日志 $params.
需要注意的最重要的事情是您的代码一开始崩溃,因为您引用了 $scope.position.lat在设置之前。我将其更新为默认为 RVA。

enter image description here ,

你确实需要更优雅地处理这个案子。

function MapCtrl($scope, watchArray) {
var center;
if ($scope.position) {
center = new google.maps.LatLng($scope.position.lat, $scope.position.lng);
}
else {
center = new google.maps.LatLng(37.5410, 77.4329); //if null use rva
}
$scope.newMapOptions = {
center: center,
zoom: 18,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
...
}

控制台日志:
[Ps]
v 0: Ps
> la: Q
> latLng: O
> pixel: Q
> __proto__: Ps
length: 1
> __proto__: Array[0]

关于angularjs - Angular-ui ui-map 点击事件未收到 $params,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19957358/

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