我對angularjs和googlemaps非常陌生,請幫助我。Angularjs在獲得指令輸出後將值分配給控制器
我的HTML文件:
<body ng-app="myApp" ng-controller="carLocationCtrl">
<div my-map=""></div>
<p>{{myCurrentPosition}}</p>
<script type="text/javascript" src="E:\Projects\Angular Projects\angularmap.js"></script>
</body>
在這裏,我想從我的指令的currentPosition值然後傳遞或指派該值在我的控制變量myCurrentLocation.then只有我能在我的<p>
顯示此值標籤。
我的控制器:
var OEMControllers=angular.module('myApp',[]);
OEMControllers.controller('carLocationCtrl', ["$scope", "$rootScope", function ($scope, $rootScope) {
$scope.isEditLocation = false;
$scope.myCurrentLocation='';
$scope.saveCarLocation = function() {
$scope.isEditLocation = true;
};
$scope.editCarLocation = function() {
$scope.isEditLocation = false;
};
}]).directive('myMap', function() {
var link = function(scope, element, attrs) {
var map, infoWindow;
var markers = [];
// map config
var mapOptions = {
center: new google.maps.LatLng(50, 2),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
};
// init the map
function initMap() {
if (map === void 0) {
map = new google.maps.Map(element[0], mapOptions);
}
}
// place a marker
function setMarker(map, position) {
var marker;
var markerOptions = {
position: position,
center:position,
map: map,
icon: 'https://maps.google.com/mapfiles/ms/icons/green-dot.png'
};
marker = new google.maps.Marker(markerOptions);
markers.push(marker); // add marker to array
//pan to marker..
console.log('marker.getPosition() :: '+marker.getPosition());
map.panTo(marker.getPosition());
map.setZoom(16);
console.log('pan called');
//here i can able to get my currentPosition but how can i send this value to my controller .........
scope.myCurrentLocation=marker.getPosition();
console.log('output :: '+scope.myCurrentLocation);
}
// show the map and place some markers
initMap();
getLocation();
var watchID=null;
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(onSuccess);
/* var options = { timeout: 30000 };
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);*/
} else {
console.log('Not obtain geolocation');
}
}
//geolocation succes..
function onSuccess(position) {
console.log('lat :: '+position.coords.latitude);
console.log('long :: '+position.coords.longitude);
setMarker(map, new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
}
function onError(error)
{
console.log(error.message);
}
};
return {
restrict: 'A',
template: '<div id="gmaps"></div>',
replace: true,
link: link
};
});
工作,我也跟着這一點,在我的代碼進行了更改,但仍它不工作.... – user3668984 2015-04-06 06:42:06