How to centering google map on Balloon center in Android

Published by Igor Khrupin on

If you click (tap) the balloon on Android Google Maps in your application, and want to centering map on center of balloon. You need to look this post.

Below you can download the source code of sample Android project.

The secret in Utils class. Below you can see the source of this class

package com.hrupin.lazymarkers;

import android.content.Context;
import android.util.DisplayMetrics;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

public class Utils {
    private static final int BALLOON_OFFSET_IN_DP = 150;
    private static final double EQUATRE_LENGTH_IN_KM = 40075.7;
    private static final double EARTH_RADIUS_IN_KM = 6356.8;
    private static DisplayMetrics metrics = null;
    private static float density;

    public static GeoPoint getBalloonCenterCoords(GeoPoint markerGeoPoint, MapView mapView) {
        int zoom = mapView.getZoomLevel();
        int balloonOffset = getOffsetInDegreesE6(zoom, dpToPxConverter(mapView.getContext(), BALLOON_OFFSET_IN_DP));
        int balloonCenterLatitude = markerGeoPoint.getLatitudeE6() + balloonOffset;
        return new GeoPoint(balloonCenterLatitude, markerGeoPoint.getLongitudeE6());
    }

    private static int getOffsetInDegreesE6(int zoom, int offsetInPx) {
        // 256 - equator length on map if zoom level = 0
        double equatreLengthOnMapInPx = (double) 256 * Math.pow(2, zoom);
        double earthRadiusOnMapInPx = (equatreLengthOnMapInPx * EARTH_RADIUS_IN_KM) / EQUATRE_LENGTH_IN_KM;
        double tangensValue = (double) offsetInPx / earthRadiusOnMapInPx;
        double offsetInDegrees = Math.toDegrees(Math.atan(tangensValue));
        return (int) (offsetInDegrees * 1E6);
    }

    private static int dpToPxConverter(Context context, int dp) {
        if (metrics == null) {
            metrics = context.getResources().getDisplayMetrics();
            density = metrics.density;
        }
        return (int) (density * dp + 0.5f);
    }
}

Please tell me if you know other solution.

Hope it help you!

Here you can download the source code of sample Android project.

Download it from github

2 Comments

Anand · 26 June, 2012 at 10:49

Hi

This is the solution that I exactly need.
Can you let me know the logic behind the Utils class ?
I would be interested

Igor · 26 June, 2012 at 15:05

Hi, Anand
You can download the source code from post…

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.