3

(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 =)

Jose Maria Landa
  • 415
  • 4
  • 15

2 Answers2

1

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.

Szamot
  • 366
  • 2
  • 12
0

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>
shawn_wx
  • 133
  • 1
  • 11
  • But that would prevent the map from filling the entire screen whenever the sliding layout is collapsed, would it not? I was thinking more of a programmatic solution. Anyway, I'll try out your solution and see if it works. Thank ya – Jose Maria Landa Mar 08 '16 at 02:26
  • Notice that I'm using `android:layout_weight` to measure the height of each views. So when you want to fill the entire screen with mapview, simply `setVisibility(View.GONE)` on your `otherView` will do the work. – shawn_wx Mar 08 '16 at 06:17
  • sorry but this doesnt work. its real choppy and touch stops working properly. – shawn_wx – Jose Maria Landa Mar 09 '16 at 05:06
  • could you provide your xml? – shawn_wx Mar 09 '16 at 18:33