Monday, February 4, 2013

Google Maps V2 Animation with bearing

With the help of "android.location.Location.bearingTo(Location dest)", modify the last exercise "GoogleMap animation with zoom" to include bearing in Maps animation.


package com.example.androidmapsv2;

import java.util.ArrayList;
import java.util.List;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.CancelableCallback;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

import android.location.Location;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity 
 implements OnMapClickListener{

 private GoogleMap myMap;
 PolylineOptions polylineOptions;
 Polyline polyline;
 List<LatLng> listPoint;
 int currentPt;
 
 TextView info;
 SeekBar zoomBar;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        FragmentManager myFragmentManager = getFragmentManager();
        MapFragment myMapFragment = 
          (MapFragment)myFragmentManager.findFragmentById(R.id.map);
        myMap = myMapFragment.getMap();
        
        myMap.setOnMapClickListener(this);
        
        listPoint = new ArrayList<LatLng>();
        clearPolyline();
        
        zoomBar = (SeekBar)findViewById(R.id.zoombgar);
        Button btnClear = (Button)findViewById(R.id.clear);
        Button btnAnimate = (Button)findViewById(R.id.animate);
        Button btnStop = (Button)findViewById(R.id.stopanimate);
        info = (TextView)findViewById(R.id.info);
        
        btnClear.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    clearPolyline();
   }});

        btnAnimate.setOnClickListener(btnAnimateOnClickListener);
        
        btnStop.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    myMap.stopAnimation();
   }});
        
        myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        myMap.getUiSettings().setZoomControlsEnabled(true);
        myMap.getUiSettings().setCompassEnabled(true);
        myMap.getUiSettings().setAllGesturesEnabled(true);
    }
    
    OnClickListener btnAnimateOnClickListener =
      new OnClickListener(){

    @Override
    public void onClick(View v) {
     if(listPoint==null || listPoint.isEmpty()){
      Toast.makeText(getApplicationContext(), 
        "Not enought point!", 
        Toast.LENGTH_LONG).show();
     }else{

      float zoomValue = (float)(zoomBar.getProgress() + 2);
      
      myMap.animateCamera(
        CameraUpdateFactory.zoomTo(zoomValue), 
        5000,
        MyCancelableCallback);      
      
      currentPt = 0-1;
      
      info.setText("Zoom to: " + String.valueOf(zoomValue));

     }
    }
     
    };
    
    private void clearPolyline(){
     if(polyline != null){
      polyline.remove();
     }
     
     polylineOptions = new PolylineOptions();
     polylineOptions.color(Color.RED);
     
     listPoint.clear();
     
     myMap.clear();
    }


 @Override
 public void onMapClick(LatLng point) {
  myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));

  polylineOptions.add(point);
  if(polyline != null){
   polyline.remove();
  }
  
  polyline = myMap.addPolyline(polylineOptions);
  listPoint.add(point);
 }
 
 CancelableCallback MyCancelableCallback =
   new CancelableCallback(){

    @Override
    public void onCancel() {
     info.setText("onCancel()");
    }

    @Override
    public void onFinish() {

     if(++currentPt < listPoint.size()){
      
      //Get the current location
      Location startingLocation = new Location("starting point");
      startingLocation.setLatitude(myMap.getCameraPosition().target.latitude);
      startingLocation.setLongitude(myMap.getCameraPosition().target.longitude);
      
      //Get the target location
      Location endingLocation = new Location("ending point");
      endingLocation.setLatitude(listPoint.get(currentPt).latitude);
      endingLocation.setLongitude(listPoint.get(currentPt).longitude);
      
      //Find the Bearing from current location to next location
      float targetBearing = startingLocation.bearingTo(endingLocation);
      
      LatLng targetLatLng = listPoint.get(currentPt);
      float targetZoom = zoomBar.getProgress();
      
      //Create a new CameraPosition
      CameraPosition cameraPosition =
        new CameraPosition.Builder()
          .target(targetLatLng)
                         .bearing(targetBearing)
                         .zoom(targetZoom)
                         .build();
 
      
      myMap.animateCamera(
        CameraUpdateFactory.newCameraPosition(cameraPosition), 
        5000,
        MyCancelableCallback);
      info.setText("Animate to: " + listPoint.get(currentPt) + "\n" +
        "Bearing: " + targetBearing);
      
      
      
     }else{
      info.setText("onFinish()");
     }
     
    }
  
 };

}


download filesDownload the files.

The series:
A simple example using Google Maps Android API v2, step by step.

No comments: