Web Programming examples

Google Maps,AngularJS

Google Maps for AngularJS Geocoding

  • AngularJS : 1.2.23
  • Bootstrap : 3.0.3
  • Google Maps for AngularJS : 1.0.16
  • include file
  • angular.min.js, underscore-min.js, angular-google-maps.min.js, ui-bootstrap-tpls-0.11.0.min.js
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=xxxxxxxxxxxxxxxxxx&sensor=false"></script>
<script type='text/javascript' src='http://www.example.com/wp/wp-content/themes/ang/js/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">
      <google-map 
        id="map-canvas"
        center="map.center"
        zoom="map.zoom"
        draggable="true"
        options="map.options"
        control="map.control"
      >
        <marker coords="map.marker">
        </marker>
      </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','ui.bootstrap']);

mapApp.controller("MyCtrl", function ($scope) {

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

  $scope.codeAddress = function (address) {
    geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.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();
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
    return;
  };
});

One thought on “Google Maps for AngularJS Geocoding”

Comments are closed.