Web Programming examples

Google Maps,AngularJS

Google Maps for AngularJS Geocoding(v2.0.7)

  • AngularJS : 1.2.23
  • Bootstrap : 3.2.0
  • Google Maps for AngularJS : 2.0.7
  • include file
  • angular.min.js, lodash.underscore.min.js, angular-google-maps.min.js, ui-bootstrap-tpls-0.11.0.min.js
>
<script type='text/javascript' src='http://www.example.com/wp/wp-content/themes/ang/js/lodash.underscore.min.js'></script>
<script type='text/javascript' src='http://www.example.com/wp/wp-content/themes/ang/js/angular-google-maps.min.js'></script>
<script type='text/javascript' src='http://www.example.com/wp/wp-content/themes/ang/js/app.js'></script>
<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', ['google-maps'.ns(),'ui.bootstrap']);
mapApp.config(['GoogleMapApiProvider'.ns(), function (GoogleMapApi) {
  GoogleMapApi.configure({
    key: 'your Google Map api key',
    v: '3.17',
    libraries: ''
  });
}]);

mapApp.controller("MyCtrl",['$scope', 'GoogleMapApi'.ns(), function ($scope,GoogleMapApi) {

  angular.extend($scope, {
    map: {
      center: {
        latitude: 35.681382,
        longitude: 139.766084
      },
      options: {
        maxZoom: 20,
        minZoom: 3
      },
      zoom: 16,
      control: {},
      marker: {id: 1}
    },
    address: ''
  });

  GoogleMapApi.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;
    };
  });
}]);