在 Android 上获取位置
Get location on Android
我是 运行 这段代码,但在 logcat 上看不到任何重要内容。
我错过了什么?
这是我的 activity class:
public class MainActivity extends Activity implements LocationListener, ConnectionCallbacks, OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnStart = (Button) findViewById(R.id.btnStart);
Button btnStop = (Button) findViewById(R.id.btnStop);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//SERVICE STARTED HERE
// Intent i = new Intent(getBaseContext(),MainService.class);
// getBaseContext().startService(i);
Log.d("SERVİCE", "START");
mGoogleApiClient.connect();
Toast.makeText(v.getContext(), "SERVICE START", Toast.LENGTH_SHORT).show();
}
});
btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//SERVICE STOP HERE
Intent i = new Intent(getBaseContext(),MainService.class);
getBaseContext().stopService(i);
Log.d("SERVİCE", "STOP");
mGoogleApiClient.disconnect();
Toast.makeText(v.getContext(), "SERVICE STOP", Toast.LENGTH_SHORT).show();
}
});
}
// @Override
// public void onLocationChanged(Location location) {
// // TODO Auto-generated method stub
// Log.d("KORDINAT-lat", String.valueOf(location.getLatitude()));
// Log.d("KORDINAT-long", String.valueOf(location.getLongitude()));
// }
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
Log.d("TAG", "GoogleApiClient connection has failed");
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setInterval(2000); // Update location every second
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
//
// Location location= LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
// Log.d("KORDINAT-lat", String.valueOf(location.getLatitude()));
// Log.d("KORDINAT-long", String.valueOf(location.getLongitude()));
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
Log.d("TAG", "GoogleApiClient connection has been suspend");
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d("Location received: " , location.toString());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
我还在 Android 文件上添加了所需的权限,如下所示:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
也加了meta-data
但是好像什么都没发生什么事。 Logcat 没有显示错误。
你期望输出什么?如果客户端连接成功,它只是请求位置更新。如果位置没有改变,它不会输出任何东西。
我是 运行 这段代码,但在 logcat 上看不到任何重要内容。
我错过了什么?
这是我的 activity class:
public class MainActivity extends Activity implements LocationListener, ConnectionCallbacks, OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnStart = (Button) findViewById(R.id.btnStart);
Button btnStop = (Button) findViewById(R.id.btnStop);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//SERVICE STARTED HERE
// Intent i = new Intent(getBaseContext(),MainService.class);
// getBaseContext().startService(i);
Log.d("SERVİCE", "START");
mGoogleApiClient.connect();
Toast.makeText(v.getContext(), "SERVICE START", Toast.LENGTH_SHORT).show();
}
});
btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//SERVICE STOP HERE
Intent i = new Intent(getBaseContext(),MainService.class);
getBaseContext().stopService(i);
Log.d("SERVİCE", "STOP");
mGoogleApiClient.disconnect();
Toast.makeText(v.getContext(), "SERVICE STOP", Toast.LENGTH_SHORT).show();
}
});
}
// @Override
// public void onLocationChanged(Location location) {
// // TODO Auto-generated method stub
// Log.d("KORDINAT-lat", String.valueOf(location.getLatitude()));
// Log.d("KORDINAT-long", String.valueOf(location.getLongitude()));
// }
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
Log.d("TAG", "GoogleApiClient connection has failed");
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setInterval(2000); // Update location every second
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
//
// Location location= LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
// Log.d("KORDINAT-lat", String.valueOf(location.getLatitude()));
// Log.d("KORDINAT-long", String.valueOf(location.getLongitude()));
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
Log.d("TAG", "GoogleApiClient connection has been suspend");
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d("Location received: " , location.toString());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
我还在 Android 文件上添加了所需的权限,如下所示:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
也加了meta-data
但是好像什么都没发生什么事。 Logcat 没有显示错误。
你期望输出什么?如果客户端连接成功,它只是请求位置更新。如果位置没有改变,它不会输出任何东西。