I've looked on the net but couldn't find an accurate answer. I have an ImageView to use as a marker centred on map, Its position not change when I pan the map. I want to get the latlng of position when I stop panning on the map. I guess I have to use projection but because of I am not so expert in android I got stuck. Can anybody help please.
Use map.getProjection().fromScreenLocation(Point) and map.getProjection().toScreenLocation(LatLng) to convert between screen position and geo location.
Use the Projection via map.getProjection().
This will allow you to convert between screen coordinates and LatLngs. The pixels returned are relative to the View containing the map.
For Maps API v2
VisibleRegion vr = mMap.getProjection().getVisibleRegion();
double left = vr.latLngBounds.southwest.longitude;
double top = vr.latLngBounds.northeast.latitude;
double right = vr.latLngBounds.northeast.longitude;
double bottom = vr.latLngBounds.southwest.latitude;
Also you can use,
LatLngBounds curScreen = mMap.getProjection()
.getVisibleRegion().latLngBounds;
Hope this will help.
Related
I have found many discussions about given LatLngBounds, how to get a fit zoom level. But now I meet a problem that I know current location of map center, and I can get what current zoom level is, and I wanna know the LatLng of the north-east corner and the south-west corner of my map view.
Any ideas? Thanks.
You can get the Projection of the map and the VisibleRegion:
VisibleRegion visibleRegion = mMap.getProjection().getVisibleRegion();
Then you can get the VisibleRegion's farLeft, farRight, nearLeft and nearRight coordinates. So, if you need a LatLngBounds you can do:
LatLngBounds mapBounds = new LatLngBounds(visibleRegion.farLeft, visibleRegion.nearRight);
I want to get the LatLng of the top right corner and bottom left corner of a Google Map frame to create a LatLngBounds. Is it possible to do that?
This can easily be determined using the Map View API:
yourMapFragment.getMap().getProjection().getVisibleRegion().latLngBounds
Which the docs state is:
The smallest bounding box that includes the visible region defined in this class.
Alternatively, you can also get the individual corners of the projection by replacing latLngBounds with farLeft, farRight, nearLeft or nearRight
In your case, you can simply use
LatLng bottomLeft =
yourMapFragment.getMap().getProjection().getVisibleRegion().nearLeft;
LatLng topRight =
yourMapFragment.getMap().getProjection().getVisibleRegion().farRight;
Of course. You have the Visible Region Object, that has the coordinates of all the four angles. Please refer to documentation.
I have been searching but I couldn't find it. I am using google map v2. Is there any way I can convert distance ( meters) to pixels on my screen? I need the pixels equivalent of the distance. I have the distance and I have the zoom level.
Any help is appreciated.
If I understand correctly, you have a MapFragment on your screen showing a map. You know the distance in meters between 2 points on the map that are shown on the screen and you want to calculate the distance between those two points in pixels. If you know the LatLng location of the two points, you can use the Projection class like this:
Point point1 = map.getProjection().toScreenLocation(latLng1);
Point point2 = map.getProjection().toScreenLocation(latLng2);
and then you just need to use the distance mathematic formula:
My V2 Map has been set in a random position, with a random bearing from north (so the compass symbol is likely to be pointing anywhere, but probably not north).
I want to shift/pan the map directly up the screen as the user looks at it, a short distance.
This may be very simple or require some complex calculations with lats, longs and trigonometry.
But I'm struggling to find a solution that works whatever the angle and zoom level is.
How can i do this ?
Well I solved this by finding a position on the screen and converting it to a LatLng, and then panning the map to this position. It worked perfectly.
Projection projection = mapView.getProjection();
Point p = new Point(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
LatLng pos = projection.fromScreenLocation(p);
I want to draw a static circle over a map with Google Maps. When the user pinches, the map will zoom in/out.
I need to know the map radius (related to the area contained in the circle) and change the seekbar at the bottom accordingly.
Does anybody know a solution how to retrieve the distance from the left to the right screen edge? I didn't find anything at the Google Maps API doc.
Something like this:
using VisibleRegion you can get the all corner cordinates and also the center.
VisibleRegion vr = mMap.getProjection().getVisibleRegion();
double left = vr.latLngBounds.southwest.longitude;
double top = vr.latLngBounds.northeast.latitude;
double right = vr.latLngBounds.northeast.longitude;
double bottom = vr.latLngBounds.southwest.latitude;
and you can calcuate distance from two region by this
Location MiddleLeftCornerLocation;//(center's latitude,vr.latLngBounds.southwest.longitude)
Location center=new Location("center");
center.setLatitude( vr.latLngBounds.getCenter().latitude);
center.setLongitude( vr.latLngBounds.getCenter().longitude);
float dis = center.distanceTo(MiddleLeftCornerLocation);//calculate distane between middleLeftcorner and center
I got a while to write the code for "MiddleLeftCornerLocation" variable in the great answer given by #Md. Monsur Hossain Tonmoy, so just to complete it (I haven't enough reputation points to comment your answer, sorry):
VisibleRegion vr = map.getProjection().getVisibleRegion();
double bottom = vr.latLngBounds.southwest.latitude;
Location center = new Location("center");
center.setLatitude(vr.latLngBounds.getCenter().latitude);
center.setLongitude(vr.latLngBounds.getCenter().longitude);
Location middleLeftCornerLocation = new Location("center");
middleLeftCornerLocation.setLatitude(center.getLatitude());
middleLeftCornerLocation.setLongitude(left);
float dis = center.distanceTo(middleLeftCornerLocation);
First get the map center point with
googleMap.getCameraPosition().target;
then figure out the width of the map and get the height/2 and convert them to a Point using the x y values you just got.
then relate that point to the map
LatLng widthPoint = map.getProjection().fromScreenLocation(point);
now that you have the tagret point and the width point you can calculate the distance between the 2 points which will give you the radius.
Note
this assumes always in portrait mode, if you are in landscape mode the circle will be bigger than the visible area so in this case you will want to get the distance from the height