How to get Latitude/Longitude span in Google Map V2 for Android

这一生的挚爱 提交于 2019-12-17 18:19:00

问题


I have a task for move my app to Google Maps Android APIs V2. Now, I need to get Latitude/Longitude span. I used MapView.getLatitudeSpan() and MapView.getLongitudeSpan() in previous version APIs. Now I can't find something like this in V2.

Does anybody have the same problem?


回答1:


You can use the following code to get the lat/lng span:

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;

I hope this helps.




回答2:


First obtain a Projection using GoogleMap.getProjection(). Then you can call Projection.getVisibleRegion() to obtain a VisibleRegion which has a LatLngBounds.

The reason why a LatitudeSpan and Longitude span no longer makes sense is because the map can now be rotated and tilted and so viewport is no longer a latitude/longitude aligned rectangle on the map.




回答3:


This way works for me:

CameraPosition camPos2 = mapa.getCameraPosition();
LatLng pos = camPos2.target;
Toast.makeText(MainActivity.this,"Lat: " + pos.latitude + " - Lng: " +pos.longitude,  Toast.LENGTH_LONG).show();


Oops, i misunderstood the question, i mean i did not saw "span" word. According the API the correct would be:

First get the bounds:

LatLngBounds bounds = gMap.getProjection().getVisibleRegion().latLngBounds;

And then ask if any point is in bounds:

LatLng point = new LatLng (latitude, longitude);
if(bounds.contains(point)){
    //do something
}



回答4:


Here is the answer

LatLngBounds bounds = googleMap.getProjection().getVisibleRegion().latLngBounds;
if (bounds.contains(ROMA)) {
   marker = googleMap.addMarker(
     new MarkerOptions()
      .position(ROMA)
      .title("Hello")
      .snippet("Nice Place")
      .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
     );     
   System.out.println("Marker added");
}

Add the marker only when it falls in the visible region



来源:https://stackoverflow.com/questions/13739075/how-to-get-latitude-longitude-span-in-google-map-v2-for-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!