Web Programming examples

Google Maps,AngularJS

Google Maps for AngularJS Geocoding(v2.4.1)

  • AngularJS : 1.5.9
  • 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-6">
      <ui-gmap-google-map 
        id="map-canvas"
        center="map.center"
        zoom="map.zoom"
        draggable="true"
        options="map.options"
        control="map.control"
      >
        <ui-gmap-marker coords="map.marker" idKey="map.marker.id">
        </ui-gmap-marker>>
      </ui-gmap-google-map>
    </div>
    <div class="col-sm-6">
      <form class="form" role="form">
        <div class="form-group">
          <label class="control-label">Address</label>
          <input type="text" class="form-control" ng-model="address">
          </input>
        </div>
        <button ng-click="codeAddress(address)" type="submit" class="btn btn-default">Submit</button>
      </form>
    </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) {

  angular.extend($scope, {
    map: {
      center: {
        latitude: 35.681382,
        longitude: 139.766084
      },
      options: {
        maxZoom: 20,
        minZoom: 3
      },
      zoom: 16,
      control: {},
      marker: {id: 1}
    },
    address: ''
  });
  uiGmapGoogleMapApi.then(function(maps) {
    $scope.codeAddress = function (address) {
      geocoder = new maps.Geocoder();
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == maps.GeocoderStatus.OK) {
          $scope.map.control.getGMap().setCenter(results[0].geometry.location);
          $scope.map.marker.latitude = results[0].geometry.location.lat();
          $scope.map.marker.longitude = results[0].geometry.location.lng();
          $scope.map.marker.id = 1;
        } else {
          alert('Geocode was not successful for the following reason: ' + status);
        }
      });
      return;
    };
  });
}]);