Android - 使用GPS跟踪,不推荐使用getMap

时间:2017-04-07 20:00:25

标签: android google-maps gps

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,而且我的手机上有一个屏幕。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

不推荐使用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">