gpt4 book ai didi

javascript - AngularJS + Google map 不显示标记

转载 作者:太空宇宙 更新时间:2023-11-04 16:32:07 26 4
gpt4 key购买 nike

我有一个标记在 $scope.mapCreated 上工作正常。现在,当用户单击 $scope.centerOnMe() 时,我尝试显示另一个标记,但仅显示当前位置,而没有标记。

我必须使用 this example或者有最简单的解决方案吗?

Controller

.controller('mapCtrl', ['$scope', '$state', '$ionicLoading', function($scope, $state, $ionicLoading){

$scope.mapCreated = function(map) {

var latlngPlace = new google.maps.LatLng(-27.4264356, -51.7838787);
var marker = new google.maps.Marker({
map: map,
position: latlngPlace
});

$scope.map = map;
};

$scope.centerOnMe = function () {
console.log("Centering");
if (!$scope.map) {
return;
}

$scope.loading = $ionicLoading.show({
content: 'Wait...',
showBackdrop: false
});

navigator.geolocation.getCurrentPosition(function (pos) {
console.log('Got pos', pos);

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

var myPlace = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);

var marker2 = new google.maps.Marker({
position: myPlace
});


$ionicLoading.hide();

}, function (error) {

console.log('Error' + error.message)
});
};

}])

指令.js

angular.module('app.directives', [])

.directive('map', function(){

return {
restrict: 'E',
scope: {
onCreate: '&'
},
link: function ($scope, $element, $attr) {
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-27.426102, -51.784999),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map($element[0], mapOptions);

$scope.onCreate({map: map});

// Stop the side bar from dragging when mousedown/tapdown on the map
google.maps.event.addDomListener($element[0], 'mousedown', function (e) {
e.preventDefault();
return false;
});
}

if (document.readyState === "complete") {
initialize();
} else {
google.maps.event.addDomListener(window, 'load', initialize);
}
}
}

});

map .html

<ion-view title="Map" id="page9" style="background-color:#FFFFFF;">
<ion-content>
<map on-create="mapCreated(map)"></map>
</ion-content>
<ion-footer-bar class="bar-stable bar-calm">
<a ng-click="centerOnMe()" class="button button-icon icon ion-navigate">FindMe</a>
</ion-footer-bar>
</ion-view>

最佳答案

您可以在centerOnMe()函数内创建一个新标记。以下是创建新标记的示例。这是创建标记的唯一方法。

$scope.centerOnMe = function () {
console.log("Centering");
// Adding a new marker
var marker1 = new google.maps.Marker({
map: $scope.map,
position: new google.maps.LatLng(lat, lng);
});

if (!$scope.map) {
return;
}
}

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

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