api v2中的mapview和cameraupdate [英] mapview and cameraupdate in api v2

查看:88
本文介绍了api v2中的mapview和cameraupdate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Why the CameraUpdateFactory class is not working in my project? The app crashes if it executes the following command: CameraUpdate pino= CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())); If i remove that line (and of course the next one), the code successfully starts and shows the map. I need the mapView and i need to use the new api v2. I declare the mapView in layout in this way:

        <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中,我这样写:

then in mainActivity.java i wrote this:

    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中的错误如下:

The error in the LogCat is the following:

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

推荐答案

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

I solved the problem by adding on the "onCreate" the following lines:

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

原因是需要对CameraUpdateFactory进行初始化(即使从文档看来,应该使用mapview对其进行自动初始化)

the reason is that the CameraUpdateFactory needs to be initilized (even if from documentation it seems that using mapview it shoud be automatically initialized)

我什至用公共类MainActivity扩展Activity"替换了公共类MainActivity扩展FragmentActivity".但是我认为这最后一件事是不需要的.

I even replaced "public class MainActivity extends FragmentActivity" with "public class MainActivity extends Activity". But i think that this last thing it was not needed..

这篇关于api v2中的mapview和cameraupdate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆