javascript - set marker in map using angular JS -
i working on application. there 2 pages. in first page, showing country , there lat & lng. when click on country redirect 2nd page. in page showing google map. according map marker showing. that's work perfectly.
problem in 2nd page use 2 textbox, textbox enter lat & lng , when hit button marker should change there place. it's not working. not understanding doing wrong. if missing thing please me.
code
(function () { 'use strict'; myapp.controller('mapctrl', function ($scope, $http, $stateparams) { $scope.lat = $stateparams._lat; $scope.lng = $stateparams._lng; $scope.clickme = function (clicked) { $scope.lat = $scope.txtlatitude; $scope.lng = $scope.txtlongitude; }; }) .factory('mapsinit', mapsinitfactory) .directive('mymap', mymap); function mapsinitfactory($window, $q) { //google's url async maps initialization accepting callback function var asyncurl = 'https://maps.googleapis.com/maps/api/js?key=aizasydpqr0uw01dcvmuscxo7xizh47-zuuyezm&callback=', mapsdefer = $q.defer(); //callback function - resolving promise after maps loaded $window.googlemapsinitialized = mapsdefer.resolve; // removed () //async loader var asyncload = function (asyncurl, callbackname) { var script = document.createelement('script'); //script.type = 'text/javascript'; script.src = asyncurl + callbackname; document.body.appendchild(script); }; //start loading google maps asyncload(asyncurl, 'googlemapsinitialized'); //usage: initializer.mapsinitialized.then(callback) return { mapsinitialized: mapsdefer.promise }; } function mymap(mapsinit) { return { restrict: 'e', scope: { mapid: '@id', // map id lat: '@', // latitude long: '@' // longitude }, link: function (scope) { console.log(scope.mapid, scope.lat, scope.long); if (angular.isdefined(scope.lat) && angular.isdefined(scope.long)) { // initialize map var initialize = function () { var location = new google.maps.latlng(scope.lat, scope.long); var mapoptions = { zoom: 6, center: location }; var map = new google.maps.map(document.getelementbyid(scope.mapid), mapoptions); new google.maps.marker({ position: location, map: map }); }; mapsinit.mapsinitialized.then(function () { initialize(); }, function () { }); } } }; } })();
first, need reference marker in question:
scope.mainmarker = new google.maps.marker({ position: location, map: map }); in controller:
$scope.clickme = function (clicked) { $scope.lat = parsefloat( $scope.txtlatitude ); $scope.lng = parsefloat( txtlongitude ); $scope.mainmarker.setlocation(new google.maps.latlng( $scope.lat, $scope.lng )); };
Comments
Post a Comment