How to create a closer drive search with google api

Asked

Viewed 225 times

0

I need to mount a search that the person selects the state and on the map appear the nearest units, as in the image, how do I? I don’t know where to start: inserir a descrição da imagem aqui

  • You need it to be in PHP or it can be with Javascript?

  • could be anyone

  • I did something like that the other day. In the case I did taking the client’s current location, then made the filter based on a given radius, something like: $limit = '1,86411'; // 2,999994244 km $query = '( 6371 * acos( cos( radians('. $lat. ') * cos( radians( addresses.latitude ) * cos( radians( addresses.longitude ) - radians('. $long. ') ) + sin( radians('. $lat. ') ) * sin( radians( addresses.latitude ) )' ), '<=', $limit); From there you can use your imagination and think about how to mount the query

1 answer

0

You can use the Library of Google Places:

var map;
var service;
var infowindow;

function initialize() {
  var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);

  map = new google.maps.Map(document.getElementById('map'), {
      center: pyrmont,
      zoom: 15
    });

  var request = {
    location: pyrmont,
    radius: '500', //Ditância máxima em todas direlções a partir da Longitude e Latitude informados
    types: ['store'] //Que tipos de lugares deve mostrar
  };

  service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);
}

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

It is very complete and you can make several filters as maximum distance, organize the items by proximity, among others recommend you read the documentation

Browser other questions tagged

You are not signed in. Login or sign up in order to post.