Tom*_*mDT 12 android location android-location google-api-client google-play-services
我只是想做一个简单的"教程"应用程序来获取我的手机的位置(以便在以后的其他应用程序中学习如何使用它),但我只是没有到达任何地方.
Android Developer的教程:首先,我按照Android开发者网站(developer.android.com/training/location/receive-location-updates.html)中的教程进行操作.
就像那里所示,我使用了Location,LocationClient和LocationRequest,在onCreate中初始化它们(并设置它们).LocationClient在onStart和onStop上连接和断开连接.
我在连接后请求位置更新(在onConnected中).我在进行此调用之前验证了GooglePlayServices是否可用,而且我仍然没有在"onLocationChanged"中获得任何更新.
GoogleApiClient:我注意到不推荐使用LocationClient,而首选的是LocationServices(developer.android.com/reference/com/google/android/gms/location/LocationClient.html).
如下所示:https://developer.android.com/reference/com/google/android/gms/location/FusedLocationProviderApi.html,我使用GoogleApiClient并将其设置为LocationServices(也是/sf/answers/1750969531/).所以我把它全部设置为使用它,但我仍然没有得到任何关于位置的更新.
onLocationChanged如果有响应则应该打印.我还放了一个打印位置或"没有"的按钮.
我检查了使用谷歌地图,看看我是否有一些东西,但它的工作正常.
的build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services:5.0.77'
compile "com.android.support:support-v4:20.0.+"
}
Run Code Online (Sandbox Code Playgroud)
表现
...
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
...
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
...
Run Code Online (Sandbox Code Playgroud)
码
public class MyActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private GoogleApiClient mLocationClient;
private Location mCurrentLocation;
LocationRequest mLocationRequest;
...
/* Constant fields - request interval */
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mLocationClient = new GoogleApiClient.Builder(getApplicationContext())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
}
@Override
protected void onStart() {
super.onStart();
mLocationClient.connect();
}
@Override
protected void onStop() {
super.onStop();
LocationServices.FusedLocationApi.removeLocationUpdates(mLocationClient, this);
mLocationClient.disconnect();
}
public void onClick(View view) {
if (mCurrentLocation != null) {
Log.i("Location", mCurrentLocation.toString());
} else {
Log.i("Location", "nothing");
}
}
@Override
public void onLocationChanged(Location location) {
Log.d("Location Update", "CHANGED");
mCurrentLocation = location;
}
@Override
public void onConnected(Bundle bundle) {
// Display the connection status
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
if(servicesConnected()) {
LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, this);
}
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(this, "Disconnected. Please re-connect.",
Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)
logcat的
8948-8948/example.android.com.test D/Location Updates? Google Play services is available.
8948-8948/example.android.com.test I/Location? nothing
Run Code Online (Sandbox Code Playgroud)
我觉得我可能会忘记一些非常简单的东西...但我只是看不到它而且我花了太多时间试图找到一个简单的位置......
我可能只是使用android.location API但是...这让我很困扰,这很难(据说更容易).
错误只是无线网络没有打开......而我没有意识到。
至于 GregM 所说的,如果我是正确的,那就是旧的 API(其中大部分现在已弃用),并且如开发人员网站中所述(https://developer.android.com/google/auth/api-client.html)你必须使用 GoogleApiClient 如我原来的问题所示(对于回调也是如此)。
| 归档时间: |
|
| 查看次数: |
24047 次 |
| 最近记录: |