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.
Related
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!
I have a collection of polygons in a table stored as type polygon. For a given point, I need to determine the distance to the nearest polygon border (i.e., I need to trigger a behavior change when a border is crossed - either entering or leaving a polygon, and I want to know how far the point has to move before this condition may be met).
The ST_Distance() function can provide the distance between a point and a polygon's nearest edge. Using this function I can compare a specific point against all polygons in my table and sort to find the distance to the closest one.
The ST_Distance() function, however, only returns a distance value for polygons that do not contain the point provided. If the point provided is within a polygon, the returned distance is 0 - but I need the distance to the nearest edge in this case, as well.
How can I get the distance to the nearest edge of all polygons, including those my point may be contained within?
I am writing some code which clusters markers on a Google map. The clustering algorithm relies on knowing how many degrees latitude and longitude are currently visible to the user, as I break the map into a grid of n/map_pixels_width x n/map_pixels_height squares and need to know how many degrees of lat/lon are in each square to know which square each marker point belongs to.
Under normal circumstances where the map does not wrap this is relatively easy to calculate using getBounds() on the Google Map object and doing the calculations to figure out the latitudinal and longitudinal distances between the returned North-East and Sout-West points. Where I'm running into issues is where the map is zoomed out to the extent that it wraps the entire Earth > 1 times. For example, I can zoom out the map so that the entire Earth is "tiled" 5 times over which equates to 360 * 5 = 1800 longitudinal degrees and, but then the call to getBounds() no longer provides useful information:
m.getBounds().getNorthEast().lat()
88.31833020528785
m.getBounds().getNorthEast().lng()
180
m.getBounds().getSouthWest().lat()
-88.5485785544835
m.getBounds().getSouthWest().lng()
-180
Basically, the longitudes getBounds() reports are just the min and max for one whole globe which says nothing about how many times the Earth is repeated. Although Google Maps doesn't tile the map vertically (it just inserts gray filing space if zoomed out far enough), I have conceptually the same problem -- I need to know how many total degrees of space the vertical area would consume.
Is there a way to get the total number of visible longitudinal degrees?
So based on this answer to another question, I found a (hackish) way to solve this. Basically, the Google Maps OverlayView class has a getProjection() method returning a MapCanvasProjection object, which in turn has a getWorldWidth() method which returns the width of the world at the current zoom level in pixels. So the way to solve the problem then is to:
Add a dummy OverlayView to the map that doesn't actually present an overlay.
Get the overlay's projection.
Get the world width from the projection.
Calculate the number of visible longitudinal degrees as pixel_width_of_map_element / world_width_in_pixels * 360
It would be better if there were a way to do this without creating a dummy overlay, but this method seems to work.
There is a way to convert a distance (in kilometers) into a float zoom level of a GMSCameraPosition?
I have an app where the user specifies a diameter (in kilometers) that he wants to see around the gps checkmark (his position). So, I need to convert that distance (like, 30 km) into a zoom level that fits that distance in kilometers.
I'm very new with Google Maps and didn't find a way to that until now.
Thanks in advance.
For calculating with GPS coordinates have a look here. (Especially the section "Destination point given distance and bearing from start point").
When you have your coordinates you can use the fitBounds-method of the Map-object which does exactly what you want.
I have a Map object created through the google maps api and I want to figure out the distance (km or mi) between the corners of the current view. I know how to get the coordinates of the corners, is there a utility function to calculate the distance between points?
From: http://code.google.com/apis/maps/documentation/reference.html#GLatLng
GLatLng.distanceFrom( other:GLatLng,
radius?:Number );
Returns the distance, in meters, from
this location to the given location.
By default, this distance is
calculated given the default
equatorial earth radius of 6378137
meters. The earth is approximated as a
sphere, hence the distance could be
off as much as 0.3%, especially in the
polar extremes. You may also pass an
optional radius argument to calculate
distances between GLatLng coordinates
on spheres of a different radius than
earth.
This somewhat depends on how accurate you need your answer to be.
The Google Maps API includes a function called distanceFrom, which will probably work just fine for you:
Returns the distance, in meters, from
this location to the given location.
By default, this distance is
calculated given the default
equatorial earth radius of 6378137
meters. The earth is approximated as a
sphere, hence the distance could be
off as much as 0.3%, especially in the
polar extremes. You may also pass an
optional radius argument to calculate
distances between GLatLng coordinates
on spheres of a different radius than
earth.
If you need a more accurate answer, you will need a function that takes the shape of the Earth into consideration. I haven't tried it, but this one looks great.