Using bounds to search for coordinates - google-maps

I try to create a google maps API call that searches an address in a specific boundary.
So in my test I want to search for "8670" which is a postal code from a village in Belgium. This village:
https://www.google.com/maps/place/8670+Koksijde,+Belgium/#51.1129798,2.6459201,13z/data=!3m1!4b1!4m5!3m4!1s0x47dcba8d702c832d:0x10129f1eb06df0d2!8m2!3d51.117577!4d2.6744402
Using the country-parameter in the API call gives me the right answer:
https://maps.googleapis.com/maps/api/geocode/json?address=8670|country:BE
But using the bounds-parameter returns me a city in the USA:
https://maps.googleapis.com/maps/api/geocode/json?address=8670&bounds=53.6014224,7.0979623|49.4057287,2.4896213
I know bounds is not a strict filter, but as there is a city in range of the bounds with that postal code leaves me with the question why does google not find it and return me a city far away from my bounds?
The reason why I want to use bounds and not country-parameter is because the search is for 3 countries (NL, BE, LU) and as far as I know it is not possible to use OR in the country-parameter.

The documentation says that bounds parameter defines the latitude/longitude coordinates of the southwest and northeast corners of this bounding box.
https://developers.google.com/maps/documentation/geocoding/intro#Viewports
So, the first coordinate should be coordinate of southwest and second coordinate should be coordinate of northeast. In you example you define bounds in opposite order NE|SW. Just swap values in bounds parameter and you will have expected result '8670 Koksijde, Belgium' (place ID ChIJLYMscI263EcR0vBtsB6fEhA):
https://maps.googleapis.com/maps/api/geocode/json?address=8670&bounds=49.4057287%2C2.4896213%7C53.6014224%2C7.0979623&key=YOUR_API_KEY
or
https://maps.googleapis.com/maps/api/geocode/json?components=postal_code%3A8670&bounds=49.4057287%2C2.4896213%7C53.6014224%2C7.0979623&key=YOUR_API_KEY
I hope this helps!

Related

Whats the difference between location and viewport coordinates when geocoding with google API v3

I am new with using the Google maps geocoding API. I gave it an address and have a few different coordinates in the response. Why are there different coordinates?
More on Viewports: https://developers.google.com/maps/documentation/geocoding/intro#Viewports
"In a geocoding request, you can instruct the Geocoding service to prefer results within a given viewport (expressed as a bounding box). You do so within the request URL by setting the bounds parameter. Note that biasing only prefers results within the bounds; if more relevant results exist outside of these bounds, they may be included.
The bounds parameter defines the latitude/longitude coordinates of the southwest and northeast corners of this bounding box using a pipe (|) character to separate the coordinates."
Viewport allows you to create a boundary within a specific region, sort of like looking at a larger section of the map within a set of coordinates. Locations coordinates are giving you the exact spot of a location.

Find cafes within certain distance of location from Google Maps

I am using the textSearchQuery method of PlacesApi (com.google.maps.PlacesApi) to find all cafes within a 1000 m radius from a particular location. My problem is that cafes outside the given radius are being returned too. How can I find only the cafes within the radius?
Note: I want to implement this in a Java program instead of JavaScript(+HTML).
This is my textSearchQuery call:
PlacesSearchResponse response = PlacesApi.textSearchQuery(context, "cafe")
.location(currentPoint)//where currentPoint = new LatLng(13.039083, 77.610890)
.radius(1000)//1 KM
.await()
Use a Places Search (example) instead of a textSearchQuery.
From the docs, Text search won't filter results outside of given bounds:
You may bias results to a specified circle by passing a location and a radius parameter. This will instruct the Places service to prefer showing results within that circle. Results outside the defined area may still be displayed.

Estimating the radius of a location using google maps

I am using google maps API to get coordinates from a typed location. What I was also hoping to do was get the radius of the place, possibly using "bounds".
For example, if I geocode "England" it gives me a coordinate, but it doesnt tell me it's radius is very large. Whereas if I geocode "London" it again gives me a coordinate but doesnt tell me that it's radius is much smaller.
So my question is, is it possible to get an estimate of the size of the area being geocoded from the google API as well as it's center coordinate point?
Thanks!
You should be able to get the bounds via the GeocoderResult's geometry.bounds or geometry.viewport properties.
https://developers.google.com/maps/documentation/javascript/reference#GeocoderGeometry

Google maps search by address component

I have implemented a basic google places autocomplete in my web app, for example saying "Heraclion, Crete" and it translates it to the latlng coordinates that I want. I also used the mysql radius example from the api to show nearest entries. I also have locations in a second area of Crete, called "Rethymnon".
The problem now is the following. Supposedly one types just Crete. How can I get all the entries from Heraclion and Rethymnon? My code uses the radius approach as mentioned before. So I need something else to define it in a rectangular area.
Is this possible?
If the result contains a viewport (LatLngBounds), you could use that.
PlaceResult
geometry: The Place's geometry-related information. This includes:
location provides the latitude and longitude of the Place.
viewport defines the preferred viewport on the map when viewing this Place

Directions and distance from within bounds and outside bounds

I try to create a WordPress plugin, that will use the Google maps directions in combination with bounds.
This is an application for km based charge, that has low cost/km into the bounds and high cost/km outside the bounds.
So the question is:
Is there a way to know what is the distance from within the bounds point to the bounds border
Is there a way to know what is the distance from a point outside the bounds to the bounds borders?
Note that the bounds area will be made with poligons. I have find the way to check if a point in the poligon or outside the poligon.
The problem is what I describe above in the list.
Yes, there's a function in the API that measures distances between LatLngs.
It's called : computeLength() in the geometry library.
Please read this
https://developers.google.com/maps/documentation/javascript/geometry
Distance and Area Functions
The distance between two points is the length of the shortest path between them. This shortest path is called a geodesic. On a sphere all geodesics are segments of a great circle. To compute this distance, call computeDistanceBetween(), passing it two LatLng objects.
You may instead use computeLength() to calculate the length of a given path if you have several locations.
Distance results are expressed in meters.
To compute the area (in square meters) of a polygonal area, call computeArea(), passing the array of LatLng objects defining a closed loop.