【问题标题】:Android - Tracking using GPS, getMap deprecated [duplicate]Android - 使用 GPS 进行跟踪,不推荐使用 getMap [重复]
【发布时间】:2017-04-07 20:00:25
【问题描述】:
public class LocationActivity extends FragmentActivity implements
        LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    private static final String TAG = "LocationActivity";
    private static final long INTERVAL = 1000 * 60 * 1; //1 minute
    private static final long FASTEST_INTERVAL = 1000 * 60 * 1; // 1 minute
    Button btnFusedLocation;
    TextView tvLocation;
    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    Location mCurrentLocation;
    String mLastUpdateTime;
    GoogleMap googleMap;

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isGooglePlayServicesAvailable()) {
            finish();
        }
        createLocationRequest();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        setContentView(R.layout.activity_location_google_map);
        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        googleMap = fm.getMap();
        googleMap.getUiSettings().setZoomControlsEnabled(true);
    }
}

我正在尝试使用 gps 进行跟踪并在地图上绘制位置。我尝试了上面的代码,但 getMap 已被弃用,我的手机上出现了一个屏幕。有人可以帮我解决这个问题吗?

【问题讨论】:

标签: android google-maps gps


【解决方案1】:

GetMap() 已弃用。您需要在您的班级中实现OnMapReadyCallback

public class LocationActivity extends FragmentActivity implements
    LocationListener,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener, OnMapReadyCallback  {

并覆盖:onMapReady() 函数

@Override
public void onMapReady(GoogleMap map) {
    googleMap = map;
    googleMap.getUiSettings().setZoomControlsEnabled(true);
    //add code here
}

也在你的代码中添加:

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
fm.getMapAsync(this); 

在布局中:

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

【讨论】:

  • 我试过这个,但它说“尝试在空对象引用上调用 getMapAsync(..)。
  • 你打电话给fm.getMapAsync(this);了吗?
  • 是的,MapFragment mapFragment = (MapFragment) getFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this);
  • 使用:SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); fm.getMapAsync(this);
猜你喜欢
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
  • 2011-08-18
  • 2012-04-19
相关资源
最近更新 更多