Code example for Projection

Methods: fromScreenLocation, toScreenLocation

0
/* 
 * Copyright (C) 2007 The Android Open Source Project 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */ 
 
package com.gdelight.activity; 
 
import com.gdelight.R; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.Projection; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter; 
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener; 
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener; 
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.LatLngBounds; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 
 
import android.annotation.SuppressLint; 
import android.graphics.Color; 
import android.graphics.Point; 
import android.os.Build; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.SystemClock; 
import android.support.v4.app.FragmentActivity; 
import android.text.SpannableString; 
import android.text.style.ForegroundColorSpan; 
import android.view.View; 
import android.view.ViewTreeObserver.OnGlobalLayoutListener; 
import android.view.animation.BounceInterpolator; 
import android.view.animation.Interpolator; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 
 
/** 
 * This class provides a basic demonstration of how to write an Android 
 * activity. Inside of its window, it places a single view: an EditText that 
 * displays and edits some internal text. 
 */ 
public class MapActivity extends FragmentActivity implements OnMarkerClickListener, OnInfoWindowClickListener, OnMarkerDragListener {
     
    private static final LatLng BRISBANE = new LatLng(-27.47093, 153.0235);
    private static final LatLng MELBOURNE = new LatLng(-37.81319, 144.96298);
    private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
    private static final LatLng ADELAIDE = new LatLng(-34.92873, 138.59995);
    private static final LatLng PERTH = new LatLng(-31.952854, 115.857342);
    private GoogleMap mMap;
    private Marker mPerth;
    private Marker mSydney;
    private Marker mBrisbane;
    private Marker mAdelaide;
    private Marker mMelbourne;
    private TextView mTopText;
 
	 
    public MapActivity() { 
    } 
 
    /** Called with the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map); 
 
        mTopText = (TextView) findViewById(R.id.top_text);
 
        setUpMapIfNeeded(); 
    } 
 
     
    /** 
     * Called when the activity is about to start interacting with the user. 
     */ 
    @Override 
    protected void onResume() { 
        super.onResume(); 
        setUpMapIfNeeded(); 
    } 
 
    private void setUpMapIfNeeded() { 
        // Do a null check to confirm that we have not already instantiated the map. 
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment. 
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map. 
            if (mMap != null) {
                setUpMap(); 
            } 
        } 
    } 
 
    private void setUpMap() { 
        // Hide the zoom controls as the button panel will cover it. 
        mMap.getUiSettings().setZoomControlsEnabled(false);
 
        // Add lots of markers to the map. 
        addMarkersToMap(); 
 
        // Setting an info window adapter allows us to change the both the contents and look of the 
        // info window. 
        mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
 
        // Set listeners for marker events.  See the bottom of this class for their behavior. 
        mMap.setOnMarkerClickListener(this);
        mMap.setOnInfoWindowClickListener(this);
        mMap.setOnMarkerDragListener(this);
 
        // Pan to see all markers in view. 
        // Cannot zoom to bounds until the map has a size. 
        final View mapView = getSupportFragmentManager().findFragmentById(R.id.map).getView();
        if (mapView.getViewTreeObserver().isAlive()) {
            mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @SuppressLint("NewApi") // We check which build version we are using. 
                @Override 
                public void onGlobalLayout() { 
                    LatLngBounds bounds = new LatLngBounds.Builder()
                            .include(PERTH)
                            .include(SYDNEY)
                            .include(ADELAIDE)
                            .include(BRISBANE)
                            .include(MELBOURNE)
                            .build();
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                      mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    } else { 
                      mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    } 
                    mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
                } 
            }); 
        } 
    } 
 
    private void addMarkersToMap() { 
        // Uses a colored icon. 
        mBrisbane = mMap.addMarker(new MarkerOptions()
                .position(BRISBANE)
                .title("Brisbane")
                .snippet("Population: 2,074,200")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
 
        // Uses a custom icon. 
        mSydney = mMap.addMarker(new MarkerOptions()
                .position(SYDNEY)
                .title("Sydney")
                .snippet("Population: 4,627,300")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));
 
        // Creates a draggable marker. Long press to drag. 
        mMelbourne = mMap.addMarker(new MarkerOptions()
                .position(MELBOURNE)
                .title("Melbourne")
                .snippet("Population: 4,137,400")
                .draggable(true));
 
        // A few more markers for good measure. 
        mPerth = mMap.addMarker(new MarkerOptions()
                .position(PERTH)
                .title("Perth")
                .snippet("Population: 1,738,800"));
        mAdelaide = mMap.addMarker(new MarkerOptions()
                .position(ADELAIDE)
                .title("Adelaide")
                .snippet("Population: 1,213,000"));
 
        // Creates a marker rainbow demonstrating how to create default marker icons of different 
        // hues (colors). 
        int numMarkersInRainbow = 12;
        for (int i = 0; i < numMarkersInRainbow; i++) {
            mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(
                            -30 + 10 * Math.sin(i * Math.PI / (numMarkersInRainbow - 1)),
                            135 - 10 * Math.cos(i * Math.PI / (numMarkersInRainbow - 1))))
                    .title("Marker " + i)
                    .icon(BitmapDescriptorFactory.defaultMarker(i * 360 / numMarkersInRainbow)));
        } 
    } 
 
    private boolean checkReady() { 
        if (mMap == null) {
            Toast.makeText(this, R.string.map_not_ready, Toast.LENGTH_SHORT).show();
            return false; 
        } 
        return true; 
    } 
 
    /** Called when the Clear button is clicked. */ 
    public void onClearMap(View view) {
        if (!checkReady()) { 
            return; 
        } 
        mMap.clear();
    } 
 
    /** Called when the Reset button is clicked. */ 
    public void onResetMap(View view) {
        if (!checkReady()) { 
            return; 
        } 
        // Clear the map because we don't want duplicates of the markers. 
        mMap.clear();
        addMarkersToMap(); 
    } 
 
    // 
    // Marker related listeners. 
    // 
 
    @Override 
    public boolean onMarkerClick(final Marker marker) {
        // This causes the marker at Perth to bounce into position when it is clicked. 
        if (marker.equals(mPerth)) {
            final Handler handler = new Handler();
            final long start = SystemClock.uptimeMillis();
            Projection proj = mMap.getProjection();
            Point startPoint = proj.toScreenLocation(PERTH);
            startPoint.offset(0, -100);
            final LatLng startLatLng = proj.fromScreenLocation(startPoint);
            final long duration = 1500;
 
            final Interpolator interpolator = new BounceInterpolator();
 
            handler.post(new Runnable() {