AngularJS Google Maps: Add / Update Token and Update Center by Click

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 mapClickedupdated to true.

Self-diagnosis: I think that the problem is related to the problem of the area using vm.mapor changes in vm.mapnot 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?

+4
source share
1 answer

The problem is that the parameter mapin the event handler clickcontradicts the existing local variable with the name mapin the function getMap.

click event-handler mapObject panTo .

      // ...
      click: function(mapObject, eventName, originalEventArgs) {
                      /*^^^^^*/
        var e = originalEventArgs[0];
        map.marker = {
          latitude: e.latLng.lat(),
          longitude: e.latLng.lng()
        };
        mapObject.panTo(new google.maps.LatLng(map.marker.latitude, map.marker.longitude));
        /*^^^^^*/
        vm.mapClicked = true;
      }
      // ...

JS Fiddle .

. .

angular.module('boat', ['nemLogging', 'uiGmapgoogle-maps']);


(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(mapObject, eventName, originalEventArgs) {
            var e = originalEventArgs[0];
            map.marker = {
              latitude: e.latLng.lat(),
              longitude: e.latLng.lng()
            };
            mapObject.panTo(new google.maps.LatLng(map.marker.latitude, map.marker.longitude));
            vm.mapClicked = true;
          }
        }
      };
      return map;
    }
  }
})();
.angular-google-map-container {
  height: 150px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdn.rawgit.com/nmccready/angular-simple-logger/master/dist/browser.js"></script>
<script src="//maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-google-maps/2.2.1/angular-google-maps.min.js"></script>
<div ng-app="boat">
  <div ng-controller="NewListingController as listingCtrl">


    <!-- CODE IN QUESTION BEGINS -->


    <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>


    <!-- CODE IN QUESTION ENDS -->


  </div>
</div>
Hide result
+3

Source: https://habr.com/ru/post/1611517/


All Articles