2

I understand that the title might add a little confusion, I'll do my best to explain it here. I'm trying to create an app for a charity, and they want a map for their site that they can navigate around. I'm currently using the google maps overlay function. I've almost got the desired outcome, but there are a few things I can't get my head around.

    @Override
    public void onMapReady(GoogleMap googleMap) {
    //LatLng NEWARK = new LatLng(51.251115, -0.599319);

    LatLngBounds NewarkBounds = new LatLngBounds(
            new LatLng(50.807686, -0.430219),
            new LatLng(50.811414, -0.424310)

    );

    mMap = googleMap;
    mMap.getUiSettings().setMapToolbarEnabled(false);
    //mMap.addMarker(new MarkerOptions().position(NEWARK).title("Reserve"));


    LatLng myPosition = new LatLng(latitude,longitude);


    GroundOverlayOptions newarkMap = new GroundOverlayOptions()
            .image(BitmapDescriptorFactory.fromResource(R.drawable.maptest))
            .positionFromBounds(NewarkBounds)
            .visible(false);


    if((myPosition.latitude <= PLACETEXT && myPosition.latitude >=PLACETEXT ) && 
    (myPosition.longitude >= -PLACETEXT && myPosition.longitude <= -PLACETEXT )){
        newarkMap.visible(true);
        mMap.setMyLocationEnabled(true);

    }
    else {
        mMap.setMyLocationEnabled(false);
    }

    GroundOverlay imageOverlay = mMap.addGroundOverlay(newarkMap);


    mMap.setMyLocationEnabled(true);
    mMap.setOnMyLocationButtonClickListener(this);
    mMap.setOnMyLocationClickListener(this);


    mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(myPosition.latitude, 
    myPosition.longitude)));
    mMap.setMapType(0);
    mMap.setLatLngBoundsForCameraTarget(NewarkBounds);
}

This is my OnMapReady function, I've tried playing around with the zoom function etc, I'm new to this stuff so I might be doing something fundamentally wrong.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</fragment>

</androidx.constraintlayout.widget.ConstraintLayout>

Layout file ^^

The image above shows whats currently displaying, ignore the map it is a placeholder

The image above shows whats currently displaying, ignore the map it is a placeholder, What I'm trying to achieve is to be fully zoomed in the person's current location, and to only be able to drag around the photo to the boundary of the LatLngs of the map?

Dumbledore
  • 95
  • 6
  • I'm coming to the impression that it just isn't possible? I was wondering if you can create a custom map with google somehow and import that instead of the google map predefined map? – Dumbledore Dec 27 '19 at 18:39

1 Answers1

1

This is possible. Please have a look at Google's documentation on Setting boundaries.

Code snippet to set the camera to the greatest possible zoom level within bounds:

private GoogleMap mMap;
// Create a LatLngBounds that includes Australia.
private LatLngBounds AUSTRALIA = new LatLngBounds(
  new LatLng(-44, 113), new LatLng(-10, 154));

// Set the camera to the greatest possible zoom level that includes the bounds
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(AUSTRALIA, 0));

Centering the camera within bounds:

private GoogleMap mMap;
private LatLngBounds AUSTRALIA = new LatLngBounds(
  new LatLng(-44, 113), new LatLng(-10, 154));

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(AUSTRALIA.getCenter(), 10));

Restricting the user's scrolling and panning within bounds:

private GoogleMap mMap;
// Create a LatLngBounds that includes the city of Adelaide in Australia.
private LatLngBounds ADELAIDE = new LatLngBounds(
  new LatLng(-35.0, 138.58), new LatLng(-34.9, 138.61));
// Constrain the camera target to the Adelaide bounds.
mMap.setLatLngBoundsForCameraTarget(ADELAIDE);

Hope this helps!

evan
  • 5,443
  • 2
  • 11
  • 20
  • Hi Evan, Sorry for the late reply. This was most helpful! – Dumbledore Jan 09 '20 at 13:43
  • Glad to hear! :) – evan Jan 09 '20 at 15:26
  • Hi Evan, This has solved most of my problems but one still exists that is very frustrating, for some reason, there is a grey padding around the ground overlay that I simply cannot seem to fix. I can sort of hack fix it but it will mess up the lat/lngs. This project seems to have the same issue but there was never a solution? "https://stackoverflow.com/questions/41606414/limit-camera-only-on-a-ground-overlay-google-map-android-api-v2?rq=1" any ideas!?? – Dumbledore Feb 07 '20 at 15:26
  • I've made a new post if that was not clear. https://stackoverflow.com/questions/60116647/android-googlemaps-groundoverlay-padding – Dumbledore Feb 07 '20 at 15:55
  • Hey! Sure, I'll look into it and will get back to you :) – evan Feb 07 '20 at 17:00
  • Hey Evan, from what i've worked out it seems to be an issue with mMap.setLatLngBoundsForCameraTarget adding extra padding? – Dumbledore Feb 11 '20 at 20:08
  • To add to that, i think its something to do with the ration between – Dumbledore Feb 11 '20 at 20:29
  • mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(groundOverlayBounds.getCenter(), 1)); //Restricting the user's scrolling and panning within bounds: mMap.setLatLngBoundsForCameraTarget(groundOverlayBounds); – Dumbledore Feb 11 '20 at 20:29
  • Hey Evan, Did you get a chance to look at this? – Dumbledore Feb 18 '20 at 13:54
  • Hey no sorry i still havent but i'm back on Thursday after taking a longish break and will look into it then! – evan Feb 18 '20 at 16:24
  • @Evan disappeared into abyss, just .like .that. – Aung Khant Nov 10 '21 at 08:14