[英]How to fetch current longitude and latitude in android programaticaly at every secon?
[英]Not able to fetch current latitude and longitude in android
我是android的新手,我正在尝试获取当前位置的纬度和经度,为此,我使用的是“ com.google.android.gms:play-services:8.4.0 ”,并且出现此错误
java.lang.IllegalArgumentException:必须提供GoogleApiClient参数 。 我提到了以前的stckoverflow答案,但它没有帮助我。请帮助我,预先感谢
这是Mapsactivity.java的代码
public class MapsActivity extends FragmentActivity{
private GoogleMap mMap;
AppLocationService appLocationService;
GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
displayLocation();
}
}
}
private void displayLocation() {
mLastLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
Log.d("result", latitude + ", " + longitude);
} else {
Log.d("result2","(Couldn't get the location. Make sure location is
enabled on the device)");
}
}
}
这是我的清单文件的代码
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.malli.myapplication" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"
/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
您尚未初始化mGoogleApiClient
变量。 请先初始化它。
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
mLastLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
注意:这只是初始化GoogleApiClient的示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.