Web Programming examples

Google Maps,AngularJS

Google Maps for AngularJS Place Text Search Requests(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 Text Search

<div ng-controller="MyCtrl">
  <div class="row">
    <div class="col-sm-9">
      <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-3">
      <H3 class="well well-sm">Place Text Search</H3>
      <form class="form" role="form">
        <div class="form-group">
          <label class="control-label">Place text(ex.cafe)</label>
          <input type="text" class="form-control" ng-model="place.query">
          </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>
    </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,
      query: place.query
    };
    var map = $scope.map.control.getGMap();
    var service = new google.maps.places.PlacesService(map);

    service.textSearch(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>