Android 带GoogleapClient的GPS跟踪器
在谷歌服务的上一次更新中,谷歌取消了LocationClient api,现在说使用GoogleAppClient 现在需要创建应用程序与GPS报告任何30秒到我的网络服务器,但没有找到或不了解如何工作这个新的api 如果您有使用GoogleAppClient的示例,请通过链接查看或下载 如果有GoogleAppClient的服务,请通过链接 感谢您的帮助。如果您已经安装了android sdk,那么只需签出以下目录\extras\google\google\u play\u services\samples\maps\src\com\example\mapdemo\ 它有一个在GoogleMap中显示当前位置的示例,它使用GoogleAppClient以5秒的周期间隔检索当前位置,如以下代码所述。您可以根据自己的要求进行修改 MyLocationDemoActivity.javaAndroid 带GoogleapClient的GPS跟踪器,android,gps,location,google-play-services,Android,Gps,Location,Google Play Services,在谷歌服务的上一次更新中,谷歌取消了LocationClient api,现在说使用GoogleAppClient 现在需要创建应用程序与GPS报告任何30秒到我的网络服务器,但没有找到或不了解如何工作这个新的api 如果您有使用GoogleAppClient的示例,请通过链接查看或下载 如果有GoogleAppClient的服务,请通过链接 感谢您的帮助。如果您已经安装了android sdk,那么只需签出以下目录\extras\google\google\u play\u services\
我也有同样的问题。您需要明确使用GoogleMap.setLocationSource
下面是一个示例:我发现GoogleMap使用自己的位置提供程序,而不是FusedLocationProvider,因此您需要明确调用GoogleMap.setLocationSource来替换它。以下是一个例子:
public class MyLocationDemoActivity extends FragmentActivity
implements
ConnectionCallbacks,
OnConnectionFailedListener,
LocationListener,
OnMyLocationButtonClickListener {
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private TextView mMessageView;
// These settings are the same as the settings for the map. They will in fact give you updates
// at the maximal rates currently possible.
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5000) // 5 seconds
.setFastestInterval(16) // 16ms = 60fps
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_location_demo);
mMessageView = (TextView) findViewById(R.id.message_text);
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
setUpGoogleApiClientIfNeeded();
mGoogleApiClient.connect();
}
@Override
public void onPause() {
super.onPause();
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationButtonClickListener(this);
}
}
}
private void setUpGoogleApiClientIfNeeded() {
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
}
/**
* Button to get current Location. This demonstrates how to get the current Location as required
* without needing to register a LocationListener.
*/
public void showMyLocation(View view) {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
String msg = "Location = "
+ LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
}
/**
* Implementation of {@link LocationListener}.
*/
@Override
public void onLocationChanged(Location location) {
mMessageView.setText("Location = " + location);
}
/**
* Callback called when connected to GCore. Implementation of {@link ConnectionCallbacks}.
*/
@Override
public void onConnected(Bundle connectionHint) {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient,
REQUEST,
this); // LocationListener
}
/**
* Callback called when disconnected from GCore. Implementation of {@link ConnectionCallbacks}.
*/
@Override
public void onConnectionSuspended(int cause) {
// Do nothing
}
/**
* Implementation of {@link OnConnectionFailedListener}.
*/
@Override
public void onConnectionFailed(ConnectionResult result) {
// Do nothing
}
@Override
public boolean onMyLocationButtonClick() {
Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
// Return false so that we don't consume the event and the default behavior still occurs
// (the camera animates to the user's current position).
return false;
}
}