如何在Google Map V2 for Android中获取纬度/经度范围

时间:2022-12-31 16:02:51

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.

我的任务是将我的应用移动到Google Maps Android API V2。现在,我需要获得纬度/经度范围。我在以前的版本API中使用了MapView.getLatitudeSpan()和MapView.getLongitudeSpan()。现在我在V2中找不到这样的东西。

Does anybody have the same problem?

有没有人有同样的问题?

4 个解决方案

#1


25  

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

您可以使用以下代码获取lat / lng范围:

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


6  

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

首先使用GoogleMap.getProjection()获取Projection。然后,您可以调用Projection.getVisibleRegion()来获取具有LatLngBounds的VisibleRegion。

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.

LatitudeSpan和Longitude跨度不再有意义的原因是因为地图现在可以旋转和倾斜,因此视口不再是地图上的纬度/经度对齐矩形。

#3


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:

糟糕,我误解了这个问题,我的意思是我没有看到“跨度”这个词。根据API,正确的是:

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


1  

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

仅在标记落在可见区域时添加标记

#1


25  

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

您可以使用以下代码获取lat / lng范围:

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


6  

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

首先使用GoogleMap.getProjection()获取Projection。然后,您可以调用Projection.getVisibleRegion()来获取具有LatLngBounds的VisibleRegion。

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.

LatitudeSpan和Longitude跨度不再有意义的原因是因为地图现在可以旋转和倾斜,因此视口不再是地图上的纬度/经度对齐矩形。

#3


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:

糟糕,我误解了这个问题,我的意思是我没有看到“跨度”这个词。根据API,正确的是:

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


1  

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

仅在标记落在可见区域时添加标记