GMSCameraPosition: convert kilometers to zoom level - google-maps

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.

Related

Get total degrees latitude/longitude displayed on a Google map when world wraps- Javascript v3 API

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.

Displaying distance on an elevation graph when rendering directions

I would like to display an elevation graph within my application that also shows the distance from the start of a particular routes.
I found this example which shows a basic elevation graph for a route.
http://www.geocodezip.com/v3_elevation-profile_distance.html
Is there a way to accurately display the distance along the x axis in the graph at the bottom?
The fact that the google.maps.ElevationResult doesn't include a distance seems to make this very difficult.
Here's another example with driving directions and mileage in each county:
http://maps.forum.nu/v3/gm_directions.html
Is that what you're looking for?
The elevation service results contains the latitude/longitude coordinates, elevation and resolution of the result. From the coordinates you can compute the distance between the points (using the geometry library computeDistanceBetween method).

ElasticSearch geo-database: geo_distance filter returns geo pins in ellipse not in circle

i have elasticsearch database of geo objects.
I want to search the nearest objects around some geo_point in custom distance and then display them on google map.
using default elasticsearch filters - geo_distance filter, it works fine, but the problem is that returned pins are in eliptical range, not in circular range (as usually when you search anything in X km RADIUS) maybe screenshots would help to understand.
I don't know where the problem can be, google maps only takes the result that elastic returns - set of pins to display
Does anybody know anything about this issue?
When geo points are represented as arrays in elasticsearch they are following GeoJSON format, which is [lon, lat]. So, from elasticsearch perspective, your center pin is not in Paris but just off the coast of Somalia.
Based on your screenshots I came up with a couple of points in your circle.
Assuming we have centre coordinate of lat:48.853647, lon:2.347894, and a point at lat:48.853647 lon:2.32 (a point at the same lat but further west):
Using distance_type:plane the distance calculated is ~ 3.1km, while using distance_type:arc the distance is ~ 2km.
If we use a different point, lat:48.8717 lon:2.347894 (further north of the centre point, but some lon), using distance_type:plane the distance is ~ 2km and using distance_type:arc it is also ~ 2km.
Consequently if the filtered distance was say 2.5km, then using distance_type:arc would correctly include both points, while using distance_type:plane would only include the northern point, giving the elliptical shape.

calculate longitude and latitude from topleft static google map

I use a static map (image) from google in a mobile phone app. What i have is: the longitude-latitude data from the center (of the image), the size of image, the zoomlevel.
How can i calculate the longitude-latitude data of the top-left (or any other place) of the image?
You need to compute the lat/lon from a range/bearing calculation (see destination point from distance and bearing given here: http://www.movable-type.co.uk/scripts/latlong.html). The zoom level will give you the range part. For the bearing part, is the map north up? If it is, then the bearing for the upper left will be 315 degrees (360 - 45). If you don't know if the map is north up, then you will need another lat/lon point.

How to figure out the size of the area in a google maps window

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.