Web Programming examples

Google Maps,AngularJS

Google Maps for AngularJS Marker Event(v2.4.1)

  • AngularJS : 1.6.5
  • Google Maps for AngularJS : 2.4.1
  • include file
  • angular.min.js, lodash.underscore.min.js, angular-simple-logger.min.js,angular-google-maps.min.js

<div ng-controller="MyCtrl">
  <div class="row">
    <div class="col-sm-12">
      <ui-gmap-google-map 
        id="map-canvas"
        center="map.center"
        zoom="map.zoom"
        draggable="true"
        options="map.options"
      >
        <ui-gmap-marker ng-repeat="m in map.markers" coords="m" events="m.events" idKey="m.id" options="m.options" isLabel='true' >
        </ui-gmap-marker>
      </ui-gmap-google-map>
    </div>
  </div>
</div>

var mapApp = angular.module('googleMapApp', ['uiGmapgoogle-maps']);

mapApp.config(
    ['uiGmapGoogleMapApiProvider', function(GoogleMapApiProviders) {
        GoogleMapApiProviders.configure({
          key: 'your Google Map api key',
          v: '3', //defaults to latest 3.X anyhow
          libraries: 'weather,geometry,visualization'
        });
    }]
);

mapApp.controller("MyCtrl",['$scope', 'uiGmapGoogleMapApi', function ($scope,uiGmapGoogleMapApi) {
  var onMarkerDblclickd = function (marker, eventName, args) {
    $scope.map.zoom = 17;
    $scope.map.center.latitude = marker.getPosition().lat();
    $scope.map.center.longitude = marker.getPosition().lng();
    $scope.$apply();
  }

  angular.extend($scope, {
    map: {
      center: {
        latitude: 35.681382,
        longitude: 139.766084
      },
      options: {
        maxZoom: 20,
        minZoom: 3
      },
      zoom: 15,
      markers: [
        {
          latitude: 35.681382,
          longitude: 139.766084,
          showWindow: false,
          id:1,
          options: {
            labelContent: 'Tokyo Station',
            labelAnchor: '0 30'
          },
          events: {
            dblclick: function (marker, eventName, args) {
              onMarkerDblclickd(marker, eventName, args);
            }
          }
        },
        {
          latitude: 35.682032,
          longitude: 139.762294,
          showWindow: false,
          id: 2,
          options: {
            labelContent: 'wadakuramon',
            labelAnchor: '0 30'
          },
          events: {
            dblclick: function (marker, eventName, args) {
              onMarkerDblclickd(marker, eventName, args);
            }
          }
        },
        {
          latitude: 35.678354,
          longitude: 139.761028,
          showWindow: false,
          id: 3,
          options: {
            labelContent: 'babasakimon',
            labelAnchor: '0 30'
          },
          events: {
            dblclick: function (marker, eventName, args) {
              onMarkerDblclickd(marker, eventName, args);
            }
          }
        }
      ]
    }
  });
  uiGmapGoogleMapApi.then(function(maps) {});
}]);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.