Android - how to get map coordinates from map view
Asked Answered
S

4

5

In my application I am displaying places using markers on google maps. But I am marking all the available places at once which I don't want to do. I wanted to display the places which only fits into the screen by fetching the maps coordinates as shown below:

enter image description here

I wanted only to fetch the places that are available in this range from the source by sending the coordinates of the maps. I am not sure whether I should capture all the 4-coordinates from the corners of the screen?

Can anyone help me to figure out this?

Seriate answered 4/2, 2013 at 9:38 Comment(0)
S
4

You can get those by using this method:

    yourMapView.getMap().getCameraPosition();

This returns you a CameraPosition object. The coordinates of the camera is given by the field target and the zoom level by the field zoom. Now you should be able to calculate the aproximate coordinates of the screen corners.

Sump answered 4/2, 2013 at 9:45 Comment(5)
thanks Zarkaos. I am still confused with what to do? does this link have something to help me. #10985987Seriate
I'm not an expert with using maps in Android :s But your link seems to be for a diffrent situation with OpenGl. I don't tink you can use it in your application..Sump
ok. after obtaining camera position how can i resolve this into lat lon values? because i guess i should compare this lat lon value with the other places lat lon values in my database.Seriate
As I said in my answer, you have the field target in the CameraPosition which gives you a LatLng object. Refer to LatLng reference for getting lon and lat values from LatLng object.Sump
I tried with your solution. It is returning the lat lon values of the center of the screenSeriate
A
1

To obtain the TopLeft and BottomRight coordinates of the map at any time, use the following:

public GeoPoint getCurrentTL(MapView map){
    Projection pr = map.getProjection();
    return pr.fromPixels(0, 0);
}

public GeoPoint getCurrentBR(MapView map){
    Projection pr = map.getProjection();
    return pr.fromPixels(map.getWidth(), map.getHeight());
}
Assiniboine answered 4/2, 2013 at 9:49 Comment(1)
thanks for the answer. see whether you have something to comment on Zarkoas's answer.Seriate
S
1

So, this is an old question, but here's a fresh take on the issue.

You can ask for the visible region when the map is up (visible) with:

VisibleRegion vRegion = mMap.getProjection().getVisibleRegion();
LatLng upperLeft = vRegion.farLeft
LatLng lowerRight = vRegion.nearRight;
Sweetmeat answered 1/6, 2015 at 1:38 Comment(0)
R
1

The other way, we can use LatLngBounds. It returns 2 points: northeast and southwest.

VisibleRegion vRegion = mMap.getProjection().getVisibleRegion();
LatLng southwest = vRegion.latLngBounds.southwest;
LatLng northeast = vRegion.latLngBounds.northeast;

https://developers.google.com/maps/documentation/android-api/views

Rhesus answered 22/10, 2016 at 1:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.