Bart Scheffer
Bart Scheffer

Reputation: 507

Google Maps API groundoverlay doesn't work in app

For an app I'm working on I want to make an image overlay. On iOS I used the Google Maps API for this which works great. But with the exact same settings, configuration and API it doesn't work on Android. This is the result I'm getting: current result

This is clearly not what I'm hoping to see since my image is square and should appear lower according to the coordinates

I use the following code:

    public class MapFragment extends Fragment implements OnMapReadyCallback {

    LatLng lastValidTarget;

    public MapFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.filler_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_filler) {
            System.out.println("filler");
            return true;
        }

        return false;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setHasOptionsMenu(true);

        View fragmentView = inflater.inflate(R.layout.fragment_map, container, false);

        return fragmentView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        com.google.android.gms.maps.SupportMapFragment fragment = (com.google.android.gms.maps.SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        fragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(final GoogleMap googleMap) {
        LatLng target = new LatLng(52.051361, 4.522754);
        this.lastValidTarget = target;

        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(target)            // Sets the center of the map to Mountain View
                .zoom(18)                  // Sets the zoom
                .bearing(38)               // Sets the orientation of the camera to east
                .tilt(0)                   // Sets the tilt of the camera to 30 degrees
                .build();                  // Creates a CameraPosition from the builder
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        //googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
        googleMap.setMyLocationEnabled(true);

        LatLngBounds bounds = new LatLngBounds(
                new LatLng(52.046518, 4.529650),       // South west corner
                new LatLng(52.056125, 4.516046));      // North east corner

        GroundOverlay overlay = googleMap.addGroundOverlay(new GroundOverlayOptions()
                .image(BitmapDescriptorFactory.fromResource(R.drawable.tester))
                .positionFromBounds(bounds)
                .bearing(0)
                .transparency(0.3f));
    }
}

And the following (fragment_map.xml)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="xxx.xxx.MapFragment">

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

</FrameLayout>

is there something wrong in the code I'm using or am I using it wrong? Thanks in advance.

Upvotes: 1

Views: 813

Answers (1)

antonio
antonio

Reputation: 18262

The problem is that you are defining an incorrect LatLngBounds. As stated in the documentation, the constructor for LatLngBounds expects southwest, and northeast and you are exchanging the coordinates (southeast and northwest).

To solve it change yout LatLngBounds to be

LatLngBounds bounds = new LatLngBounds(new LatLng(52.046518, 4.516046), new LatLng(52.056125, 4.529650));

Or better, use a LatLngBounds.Builder to avoid errors:

LatLngBounds bounds = LatLngBounds.builder()
    .include(new LatLng(52.046518, 4.529650))
    .include(new LatLng(52.056125, 4.516046))
    .build();

Upvotes: 2

Related Questions