问题转换为GMaps v2 for Android

时间:2013-05-02 14:26:46

标签: java android google-maps android-location

我正在尝试将此代码的旧版本从GMaps v1.1转换为GMaps v2。我在转换以下MapController代码时遇到问题:

private MapController mMapController;

public void setController(Object controller)
{
  /*if( controller instanceof org.osmdroid.views.MapView )
  {
     mOpenStreetMapViewControllerSource = (org.osmdroid.views.MapView) controller;
     mMapController = null;
  } else */if( controller instanceof MapController )
  {
     mMapController = (MapController) controller;
     mOpenStreetMapViewControllerSource = null;
  }
}

public void setZoom( int i )
{
  if( mMapController != null )
  {
     mMapController.setZoom( i );
  }
  /*else if( mOpenStreetMapViewControllerSource != null )
  {
     mOpenStreetMapViewControllerSource.getController().setZoom( i );
     mPostponedSetZoom = i;
  }*/
  else 
  {
     throw new IllegalStateException( "No working controller available" );
  }
}

public void animateTo( LatLng point )
{
  if( point.latitude*1000000 != 0 && point.longitude*1000000 != 0 )
  {
     if( mMapController != null )
     {
        mMapController.animateTo( point );
     }
     /*else if( mOpenStreetMapViewControllerSource != null )
     {
        mOpenStreetMapViewControllerSource.getController().animateTo( new org.osmdroid.util.GeoPoint( point.getLatitudeE6(), point.getLongitudeE6() ) );
        mPostponedSetCenterPoint = point;
     }*/
     else 
     {
        throw new IllegalStateException( "No working controller available" );
     }
  }
}

public void setCenter( LatLng point )
{
  if( point.latitude*1000000 != 0 && point.longitude*1000000 != 0 )
  {
     if( mMapController != null )
     {
        mMapController.setCenter( point );
     }
     /*else if( mOpenStreetMapViewControllerSource != null )
     {
        mOpenStreetMapViewControllerSource.getController().setCenter( new org.osmdroid.util.GeoPoint( point.getLatitudeE6(), point.getLongitudeE6() ) );
        mPostponedSetCenterPoint = point;
     }*/
  }
}


public boolean zoomIn()
{
  if( mMapController != null )
  {
     return mMapController.zoomIn();  //USE CameraUpdateFactory for zoomIn() - https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/CameraUpdateFactory
  }
  /*if( mOpenStreetMapViewControllerSource != null )
  {
     return mOpenStreetMapViewControllerSource.getController().zoomIn();
  }*/
  return false;
}

public boolean zoomOut()
{
  if( mMapController != null )
  {
     return mMapController.zoomOut();  //USE CameraUpdateFactory for zoomOut() - https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/CameraUpdateFactory
  }
  /*else if( mOpenStreetMapViewControllerSource != null )
  {
     return mOpenStreetMapViewControllerSource.getController().zoomOut();
  }*/
  return false;
}

以下是我要转换的整个文件:

import android.util.Log;

import com.google.android.gms.maps.model.LatLng;
//import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;

public class MapControllerProxy
{

private static final String TAG = "OGT.MapControllerProxy";
private MapController mMapController;
private org.osmdroid.views.MapView mOpenStreetMapViewControllerSource;
private LatLng mPostponedSetCenterPoint = null;
private int mPostponedSetZoom = -1;

public MapControllerProxy()
{
}


public void setController(Object controller)
{
  /*if( controller instanceof org.osmdroid.views.MapView )
  {
     mOpenStreetMapViewControllerSource = (org.osmdroid.views.MapView) controller;
     mMapController = null;
  } else */if( controller instanceof MapController )
  {
     mMapController = (MapController) controller;
     mOpenStreetMapViewControllerSource = null;
  }
}

public void setZoom( int i )
{
  if( mMapController != null )
  {
     mMapController.setZoom( i );
  }
  /*else if( mOpenStreetMapViewControllerSource != null )
  {
     mOpenStreetMapViewControllerSource.getController().setZoom( i );
     mPostponedSetZoom = i;
  }*/
  else 
  {
     throw new IllegalStateException( "No working controller available" );
  }
}

public void animateTo( LatLng point )
{
  if( point.latitude*1000000 != 0 && point.longitude*1000000 != 0 )
  {
     if( mMapController != null )
     {
        mMapController.animateTo( point );
     }
     /*else if( mOpenStreetMapViewControllerSource != null )
     {
        mOpenStreetMapViewControllerSource.getController().animateTo( new org.osmdroid.util.GeoPoint( point.getLatitudeE6(), point.getLongitudeE6() ) );
        mPostponedSetCenterPoint = point;
     }*/
     else 
     {
        throw new IllegalStateException( "No working controller available" );
     }
  }
}

public void setCenter( LatLng point )
{
  if( point.latitude*1000000 != 0 && point.longitude*1000000 != 0 )
  {
     if( mMapController != null )
     {
        mMapController.setCenter( point );
     }
     /*else if( mOpenStreetMapViewControllerSource != null )
     {
        mOpenStreetMapViewControllerSource.getController().setCenter( new org.osmdroid.util.GeoPoint( point.getLatitudeE6(), point.getLongitudeE6() ) );
        mPostponedSetCenterPoint = point;
     }*/
  }
}


public boolean zoomIn()
{
  if( mMapController != null )
  {
     return mMapController.zoomIn();  //USE CameraUpdateFactory for zoomIn() - https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/CameraUpdateFactory
  }
  /*if( mOpenStreetMapViewControllerSource != null )
  {
     return mOpenStreetMapViewControllerSource.getController().zoomIn();
  }*/
  return false;
}

public boolean zoomOut()
{
  if( mMapController != null )
  {
     return mMapController.zoomOut();  //USE CameraUpdateFactory for zoomOut() - https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/CameraUpdateFactory
  }
  /*else if( mOpenStreetMapViewControllerSource != null )
  {
     return mOpenStreetMapViewControllerSource.getController().zoomOut();
  }*/
  return false;
}

public void executePostponedActions()
{
  if( mPostponedSetCenterPoint != null )
  {
     Log.w( TAG, "mPostponedSetCenterPoint"+ mPostponedSetCenterPoint);
     setCenter( mPostponedSetCenterPoint );
     mPostponedSetCenterPoint = null;
  }
  if( mPostponedSetZoom >= 0 )
  {
     Log.w( TAG, "mPostponedSetZoom"+ mPostponedSetCenterPoint);
     setZoom( mPostponedSetZoom );
     mPostponedSetZoom = -1;
  }
}

}

在转换为v2时,我可以使用哪些代码替换此MapController功能?任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:1)

这样做会将地图置于一个点上

CameraPosition position = new CameraPosition.Builder()
            .target(new LatLng(Lat,Lon))
            .zoom(zoom).build();

         map.animateCamera(CameraUpdateFactory.newCameraPosition(position));