在我的 Android 中国可穿戴设备中获取 GPS 位置
Getting GPS location in my Android Wearable in China
我正在为 运行 在中国购买的智能手表开发 Android 应用程序。
我的目标是获取设备的 GPS 位置并跟踪其移动,但我似乎找不到如何让它工作。
目前,我正在为 API 21 开发,我正在使用 Google Play Services 的以下库,因为它是中国可穿戴设备使用的库:com.google.android.gms:play-services-wearable:7.8.87
我使用的代码如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_on_route);
locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(UPDATE_INTERVAL_MS)
.setFastestInterval(FASTEST_INTERVAL_MS);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addApi(Wearable.API) // used for data layer API
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
startTravelTime = System.currentTimeMillis();
if (!hasGps()) {
Log.d(TAG, "This hardware doesn't have GPS.");
}
}
private boolean hasGps() {
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
Log.i("Connected!!!", "WERE CONNECTED");
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
mGoogleApiClient.disconnect();
System.out.println("onstop");
}
@Override
protected void onResume() {
super.onResume();
if(!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
@Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, OnRouteActivity.this);
}
mGoogleApiClient.disconnect();
}
@Override
public void onConnected(Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.getStatus().isSuccess()) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Successfully requested location updates");
}
} else {
Log.e(TAG,
"Failed in requesting location updates, "
+ "status code: "
+ status.getStatusCode()
+ ", message: "
+ status.getStatusMessage());
}
}
});
}
@Override
public void onLocationChanged(Location location){
// Display the latitude and longitude in the UI
mTextView = (TextView) findViewById(R.id.location);
mTextView.setText("Latitude: " + String.valueOf( location.getLatitude()) + "\nLongitude: " + String.valueOf( location.getLongitude()));
Toast.makeText(getBaseContext(), "Latitude: " + String.valueOf( location.getLatitude()) + "\nLongitude: " + String.valueOf( location.getLongitude()), Toast.LENGTH_LONG).show();
}
我希望有人能帮助我:)
要在中国可穿戴设备上使用位置,您应该使用 Fused Location Provider API 并确保在您的 build.gradle 文件中包含以下行:
dependencies {
...
compile 'com.google.android.gms:play-services-location:10.2.0'
}
除了以上你还需要补充:
dependencies {
...
compile 'com.google.android.gms:play-services-wearable:10.2.0'
}
可以在 Google 的指南中找到更多详细信息 Creating Android Wear Apps for China
我正在为 运行 在中国购买的智能手表开发 Android 应用程序。
我的目标是获取设备的 GPS 位置并跟踪其移动,但我似乎找不到如何让它工作。
目前,我正在为 API 21 开发,我正在使用 Google Play Services 的以下库,因为它是中国可穿戴设备使用的库:com.google.android.gms:play-services-wearable:7.8.87
我使用的代码如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_on_route);
locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(UPDATE_INTERVAL_MS)
.setFastestInterval(FASTEST_INTERVAL_MS);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addApi(Wearable.API) // used for data layer API
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
startTravelTime = System.currentTimeMillis();
if (!hasGps()) {
Log.d(TAG, "This hardware doesn't have GPS.");
}
}
private boolean hasGps() {
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
Log.i("Connected!!!", "WERE CONNECTED");
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
mGoogleApiClient.disconnect();
System.out.println("onstop");
}
@Override
protected void onResume() {
super.onResume();
if(!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
@Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, OnRouteActivity.this);
}
mGoogleApiClient.disconnect();
}
@Override
public void onConnected(Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.getStatus().isSuccess()) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Successfully requested location updates");
}
} else {
Log.e(TAG,
"Failed in requesting location updates, "
+ "status code: "
+ status.getStatusCode()
+ ", message: "
+ status.getStatusMessage());
}
}
});
}
@Override
public void onLocationChanged(Location location){
// Display the latitude and longitude in the UI
mTextView = (TextView) findViewById(R.id.location);
mTextView.setText("Latitude: " + String.valueOf( location.getLatitude()) + "\nLongitude: " + String.valueOf( location.getLongitude()));
Toast.makeText(getBaseContext(), "Latitude: " + String.valueOf( location.getLatitude()) + "\nLongitude: " + String.valueOf( location.getLongitude()), Toast.LENGTH_LONG).show();
}
我希望有人能帮助我:)
要在中国可穿戴设备上使用位置,您应该使用 Fused Location Provider API 并确保在您的 build.gradle 文件中包含以下行:
dependencies {
...
compile 'com.google.android.gms:play-services-location:10.2.0'
}
除了以上你还需要补充:
dependencies {
...
compile 'com.google.android.gms:play-services-wearable:10.2.0'
}
可以在 Google 的指南中找到更多详细信息 Creating Android Wear Apps for China