Jose Maria Landa
Jose Maria Landa

Reputation: 425

Center map on top portion of screen. google maps api android

(I'm using a sliding layout like in the umano app) How can I center the map on the top portion of the screen. I want to center the map and marker on the top portion of the screen. Right now it looks like this: enter image description here

And I want it to look like this: enter image description here

Any idea on how I can achieve this? Thanks =)

Upvotes: 4

Views: 1445

Answers (2)

Szamot
Szamot

Reputation: 366

I've found some roundabout way to do this without change map height.

  1. Using second answer from this thread calc your meters per pixel factor:

    double metersPerPx = 156543.03392 * Math.cos(latitude * Math.PI / 180) / Math.pow(2, zoom);

    latitude is your current point latitude, if you want just center on current point center then:

    double latitude = googleMap.getCameraPosition().target.latitude;

    zoom is your current map zoom:

    double zoom = googleMap.getCameraPosition().zoom;

  2. Get your screen height like that:

    private int getScreenHeight() { DisplayMetrics displayMetrics = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); return displayMetrics.heightPixels; }

  3. Calc where is your center on the top portion of screen and how many pixels you have to move to the top. You need to know height of your bottom overlay:

    int pixels = bottomOverlayHeight/2;

    or if you know it's eg 60%:

    int pixels = 0.6/2 * screenHeight;

  4. Calc meters to move: double meters = metersPerPx * pixels;

  5. Convert meters to latitude value. Meridian is about 20 000 km long. Divide 180 degrees by 20000 km result 0.009 degree/km = 0.000009 degree/m. Finally:

    double mLatitude = latitude - meters * 0.000009;

    then just:

    CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(mLatitude, longitude)).zoom(zoom).build(); googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

To be honest: that's theoretically correct, but does not work for me. But if I divide 0.000009 by 10 = 0.0000009 then it works like a charm. I guessing that in point 1 could be other unit - 10 meters per pixel.

Upvotes: 1

shawn_wx
shawn_wx

Reputation: 133

Could you provide your XML layout? I guess you have your map matching the parent view, and the rest of view is overlapping on top of the map. A simple way to solve is, you could arrange the map view and other view into a vertical LinearLayout. Something like this below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical" >

    <CustomMapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:id="@+id/mapView" />

    <otherView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </otherView>

</LinearLayout>

Upvotes: 0

Related Questions