Mega Code Archive

 
Categories / Android / Core Class
 

Street view map pinch zoom

package app.test; import android.os.Bundle; import android.view.View; import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.util.FloatMath; import android.util.Log; import android.view.MotionEvent; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapView; import com.google.android.maps.Overlay;  class ClickReceiver extends Overlay {   private static final float ZOOMJUMP = 75f;   private Context mContext;   private boolean inZoomMode = false;   private boolean ignoreLastFinger = false;   private float mOrigSeparation;   public ClickReceiver(Context context) {         mContext = context;   }   @Override   public boolean onTap(GeoPoint p, MapView mapView) {     if(mapView.isStreetView()) {             Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse             ("google.streetview:cbll=" +             (float)p.getLatitudeE6() / 1000000f +             "," + (float)p.getLongitudeE6() / 1000000f               +"&cbp=1,180,,0,1.0"                 ));         mContext.startActivity(myIntent);         return true;     }       return false;   }      public boolean onTouchEvent(MotionEvent e, MapView mapView) {       int action = e.getAction() & MotionEvent.ACTION_MASK;       if(e.getPointerCount() == 2) {           inZoomMode = true;         }         else {           inZoomMode = false;         }                  if(inZoomMode) {           switch(action) {           case MotionEvent.ACTION_POINTER_DOWN:             mOrigSeparation = calculateSeparation(e);             break;           case MotionEvent.ACTION_POINTER_UP:             ignoreLastFinger  = true;             break;           case MotionEvent.ACTION_MOVE:             float newSeparation = calculateSeparation(e);             if(newSeparation - mOrigSeparation > ZOOMJUMP) {               mapView.getController().zoomIn();               mOrigSeparation = newSeparation;             }             else if (mOrigSeparation - newSeparation > ZOOMJUMP) {               mapView.getController().zoomOut();               mOrigSeparation = newSeparation;             }             break;           }           return true;         }         if(ignoreLastFinger) {             if(action == MotionEvent.ACTION_UP)               ignoreLastFinger = false;           return true;         }     return super.onTouchEvent(e, mapView);   }   private float calculateSeparation(MotionEvent e) {     float x = e.getX(0) - e.getX(1);     float y = e.getY(0) - e.getY(1);     return FloatMath.sqrt(x * x + y * y);   } } public class MainActivity extends MapActivity {   private MapView mapView;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         mapView = (MapView)findViewById(R.id.mapview);         ClickReceiver clickRecvr = new ClickReceiver(this);         mapView.getOverlays().add(clickRecvr);         mapView.invalidate();     }     public void myClickHandler(View target) {         switch(target.getId()) {         case R.id.zoomin:           mapView.getController().zoomIn();           break;         case R.id.zoomout:           mapView.getController().zoomOut();           break;         case R.id.sat:           mapView.setSatellite(true);           break;         case R.id.street:           mapView.setStreetView(true);           break;         case R.id.traffic:           mapView.setTraffic(true);           break;         case R.id.normal:           mapView.setSatellite(false);           mapView.setStreetView(false);           mapView.setTraffic(false);           break;         }         mapView.postInvalidateDelayed(2000);     }     @Override     protected boolean isLocationDisplayed() {         return false;     }     @Override     protected boolean isRouteDisplayed() {         return false;     } } <?xml version="1.0" encoding="utf-8"?> <!-- This file is /res/layout/mapview.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/zoomin" android:layout_width="wrap_content"   android:layout_height="wrap_content" android:text="+"   android:onClick="myClickHandler" android:padding="12px" /> <Button android:id="@+id/zoomout" android:layout_width="wrap_content"   android:layout_height="wrap_content" android:text="-"   android:onClick="myClickHandler" android:padding="12px" /> <Button android:id="@+id/sat" android:layout_width="wrap_content"   android:layout_height="wrap_content" android:text="Satellite"   android:onClick="myClickHandler" android:padding="8px" /> <Button android:id="@+id/street" android:layout_width="wrap_content"   android:layout_height="wrap_content" android:text="Street"   android:onClick="myClickHandler" android:padding="8px" /> <Button android:id="@+id/traffic" android:layout_width="wrap_content"   android:layout_height="wrap_content" android:text="Traffic"   android:onClick="myClickHandler" android:padding="8px" /> <Button android:id="@+id/normal" android:layout_width="wrap_content"   android:layout_height="wrap_content" android:text="Normal"   android:onClick="myClickHandler" android:padding="8px" /> </LinearLayout> <com.google.android.maps.MapView android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:clickable="true" android:apiKey="YourKey" /> </LinearLayout>