Android位置客户端无法解析
public class MainActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
private LocationClient locationClient;
private LocationRequest locationRequest;
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationClient = new LocationClient(this, this, this);
locationRequest = LocationRequest.create()
.setInterval(5000)
.setFastestInterval(500)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = supportMapFragment.getMap();
googleMap.setMyLocationEnabled(true);
}
@Override
public void onLocationChanged(Location location) {
HaritadaKonumGosterAsyncTask task = new HaritadaKonumGosterAsyncTask();
task.execute(new Location[] {location});
}
@Override
public void onConnected(Bundle connectionHint) {
locationClient.requestLocationUpdates(locationRequest, this);
}
@Override
public void onDisconnected() {}
@Override
public void onConnectionFailed(ConnectionResult result) {}
@Override
protected void onResume() {
super.onResume();
locationClient.connect();
}
@Override
public void onPause() {
super.onPause();
if(locationClient.isConnected())
locationClient.removeLocationUpdates(this);
locationClient.disconnect();
}
private class HaritadaKonumGosterAsyncTask extends AsyncTask<Location, Void, LatLng> {
@Override
protected LatLng doInBackground(Location... params) {
Location konum = params[0];
return new LatLng(konum.getLatitude(), konum.getLongitude());
}
@Override
protected void onPostExecute(LatLng konum) {
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(konum, 15));
}
}
}
答案 0 :(得分:1)
在 build.gradle 中添加以下代码
compile 'com.google.android.gms:play-services:8.4.0'