【问题标题】:mapview and cameraupdate in api v2api v2 中的 mapview 和 cameraupdate
【发布时间】:2023-03-24 06:26:01
【问题描述】:

为什么 CameraUpdateFactory 类在我的项目中不起作用? 如果执行以下命令,应用程序会崩溃: CameraUpdate pino= CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())); 如果我删除该行(当然还有下一行),代码会成功启动并显示地图。 我需要 mapView 并且需要使用新的 api v2。 我以这种方式在布局中声明mapView:

        <com.google.android.gms.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"

    android:id="@+id/mappa"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/buttonBar"

    map:uiZoomControls="false"
     />

然后在 mainActivity.java 我写了这个:

    public class MainActivity extends FragmentActivity    implements LocationListener, LocationSource { 
public static boolean locatingMe=true;
public GoogleMap mappa;
public MapView mapView;
private OnLocationChangedListener onLocationChangedListener;
private LocationManager locationManager;




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mapView = (MapView) findViewById(R.id.mappa);
    mapView.onCreate(savedInstanceState);


        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    //You may want to pass a different provider in as the first arg here
    //depending on the location accuracy that you desire
    //see LocationManager.getBestProvider()
    Criteria locationCriteria = new Criteria();
    locationCriteria.setAccuracy(Criteria.NO_REQUIREMENT);
    locationManager.requestLocationUpdates(locationManager.getBestProvider(locationCriteria, true), 1L, 2F, this);

    if (mappa == null) {
        mappa=mapView.getMap();
        //This is how you register the LocationSource
        mappa.setLocationSource(this);
        mappa.getUiSettings().setMyLocationButtonEnabled(false);
        mappa.setMyLocationEnabled(true);
    }


}
@Override
public void onPause()
{
    if(locationManager != null)
    {
        locationManager.removeUpdates(this);
    }
    mapView.onPause();
    super.onPause();
}

@Override
public void onResume()
{
      checkGooglePlayServicesAvailability();
      checkGps();
      mapView.onResume();
      super.onResume();
}
@Override
protected void onDestroy() {
    mapView.onDestroy();
    super.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

@Override
public void activate(OnLocationChangedListener listener) 
{
    onLocationChangedListener = listener;
}

@Override
public void deactivate() 
{
    onLocationChangedListener = null;
}

@Override
public void onLocationChanged(Location location) 
{
    if( onLocationChangedListener != null )
    {
        onLocationChangedListener.onLocationChanged( location );

        //Move the camera to the user's location once it's available!
        //only if locatingMe is true
        if (locatingMe) {
            if (mappa!=null){
                CameraUpdate pino= CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude()));
                        mappa.animateCamera(pino);

            }

        }

    }
}

@Override
public void onProviderDisabled(String provider) 
{
    // TODO Auto-generated method stub
    Toast.makeText(this, "provider disabled", Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderEnabled(String provider) 
{
    // TODO Auto-generated method stub
    Toast.makeText(this, "provider enabled", Toast.LENGTH_SHORT).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) 
{
    // TODO Auto-generated method stub
    Toast.makeText(this, "status changed", Toast.LENGTH_SHORT).show();
}

LogCat 中的错误如下:

 Caused by: java.lang.NullPointerException: CameraUpdateFactory is not initialized.

【问题讨论】:

    标签: android-mapview google-maps-api-2


    【解决方案1】:

    我通过在“onCreate”上添加以下几行来解决问题:

     try {
         MapsInitializer.initialize(this);
     } catch (GooglePlayServicesNotAvailableException e) {
         e.printStackTrace();
     }
    

    原因是 CameraUpdateFactory 需要被初始化(即使从文档看来使用 mapview 它应该被自动初始化)

    我什至将“public class MainActivity extends FragmentActivity”替换为“public class MainActivity extends Activity”。但我认为这最后一件事是不需要的..

    【讨论】:

    • 我也是!非常感谢!
    • if (MapsInitializer.initialize(ctx) != ConnectionResult.SUCCESS) { // 处理错误 }
    【解决方案2】:

    如果我使用:

    try {
     MapsInitializer.initialize(this);
    } catch (GooglePlayServicesNotAvailableException e) {
     e.printStackTrace();
    }
    

    异常给我这个错误:

    Unreachable catch block for GooglePlayServicesNotAvailableException.
    

    try 语句体永远不会抛出此异常。

    我只用过:

    MapsInitializer.initialize(this);
    

    :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      • 2015-02-19
      • 2012-11-21
      • 2015-09-11
      • 2013-02-14
      • 1970-01-01
      相关资源
      最近更新 更多