Web Programming examples

Google Maps,AngularJS

Google Maps for AngularJS Place searches(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

Place searches

{{result}}
<div ng-controller="MyCtrl">
  <div class="row">
    <div class="col-sm-8">
      <ui-gmap-google-map 
        id="map-canvas"
        center="map.center"
        zoom="map.zoom"
        draggable="true"
        options="map.options"
        control="map.control"
        event="map.event"
      >
        <ui-gmap-marker ng-repeat="m in map.markers" idkey='m.id' coords="m" click="onMarkerClicked(m)">
          <ui-gmap-window show="m.showWindow"
                closeClick="closeClick(m)"
                templateUrl="m.templateUrl"
                templateParameter="m.templateParameter"
          >
          </ui-gmap-window>
        </ui-gmap-marker>
      </ui-gmap-google-map>
    </div>
    <div class="col-sm-4">
      <H4 class="well well-sm">Place searches</H4>
      <form class="form" role="form">
        <div class="form-group">
          <label class="control-label">Place types(ex.finance)</label>
          <input type="text" class="form-control" ng-model="place.types">
          </input>
        </div>
        <div class="form-group">
          <label class="control-label">radius(meters)</label>
          <input type="text" class="form-control" ng-model="place.radius">
          </input>
        </div>
        <button ng-click="placeSearch(place)" type="submit" class="btn btn-primary">submit</button>
        <button class="btn btn-danger" ng-click="removeMarkers()">Clear Map</button>
      </form>
      {{result}}
    </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,places'
        });
    }]
);

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: {},
      markers: []
    },
    place: {},
    result: ''
  });

  $scope.placeSearch = function (place) {
    var request = {
      location: {
         lat: $scope.map.center.latitude,
         lng: $scope.map.center.longitude
      },
      radius: place.radius,
      types: [place.types]
    };
    var map = $scope.map.control.getGMap();
    var service = new google.maps.places.PlacesService(map);

    service.nearbySearch(request, callback);
    return;
  };

  var callback = function (results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
      for (var i = 0; i < results.length; i++) {
        createMarker(results[i],i);
      }
    }
  }

  var createMarker = function (place,id) {
    $scope.map.markers.push({
      id: id,
      latitude: place.geometry.location.lat(),
      longitude: place.geometry.location.lng(),
      showWindow: false,
      name: place.name,
      templateUrl: 'assets/templates/place.html',
      templateParameter: {
        message: place.name
      }
    });
    $scope.$apply();
  }
  $scope.closeClick = function (marker) {
    marker.showWindow = false;
  };
  $scope.onMarkerClicked = function (marker) {
    marker.showWindow = true;
  };
  $scope.removeMarkers = function () {
    $scope.map.markers.length = 0;
  };
  uiGmapGoogleMapApi.then(function(maps) {});
}]);


(assets/templates/place.html)
<div>
  <strong>Place</strong>
  <div>
    {{ parameter.message }}
  </div>
</div>

Leave a Reply

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