Problem: I am having trouble displaying a marker on a Google map. I want the user to click, the marker to display, and the map to center the marker.
What is broken: When clicked, the marker does not appear. The log / long div below the map is also not displayed.
What works: The map is displayed and correctly centered on the click site. The variable is mapClicked
updated to true
.
Self-diagnosis: I think that the problem is related to the problem of the area using vm.map
or changes in vm.map
not being digested in the AngularJS life cycle.
Third-party package: I use Angular Google Maps.
My code snippets:
HTML:
<ui-gmap-google-map id="listingMap"
center='listingCtrl.map.center'
zoom='listingCtrl.map.zoom'
events='listingCtrl.map.events'
style="height: 250px; width: 100%">
<ui-gmap-marker ng-if="listingCtrl.mapClicked"
coords="listingCtrl.map.marker"
idKey=" 'userClickLocation' ">
</ui-gmap-marker>
</ui-gmap-google-map>
<div class="col-xs-12" ng-if="listingCtrl.map.marker.latitude">
<span style="font-weight: bold">Latitude: </span>{{ listingCtrl.map.marker.latitude }}
<span style="font-weight: bold">Longitude: </span>{{ listingCtrl.map.marker.longitude }}
</div>
Controller:
(function () {
'use strict';
angular.
module('boat').
controller('NewListingController', NewListingController);
function NewListingController() {
var vm = this;
vm.activePage = 1;
vm.map = getMap();
vm.mapClicked = false;
function getMap() {
var map = {
center: {latitude: 20.9, longitude: -78.1},
zoom: 5,
marker: {},
events: {
click: function (map, eventName, originalEventArgs) {
var e = originalEventArgs[0];
map.marker = {latitude: e.latLng.lat(), longitude: e.latLng.lng()};
map.panTo(new google.maps.LatLng(map.marker.latitude, map.marker.longitude));
vm.mapClicked = true;
}
}
};
return map;
}
}
})();
Recap: The desired behavior is to click on a map to add a marker. Coordinates should be displayed below the map. The map should be focused on the click. Additional clicks update one marker and coordinates.
You can help?
source
share