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

前端 未结 4 1119
旧时难觅i
旧时难觅i 2020-12-08 22:22

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.get

相关标签:
4条回答
  • 2020-12-08 23:05

    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

    0 讨论(0)
  • 2020-12-08 23:10

    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.

    0 讨论(0)
  • 2020-12-08 23:21

    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
    }
    
    0 讨论(0)
  • 2020-12-08 23:22

    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.

    0 讨论(0)
提交回复
热议问题