(1)在AndroidManifest.xml文件中添加定位权限:
(2)在代码中获取定位管理器实例:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
(3)注册定位监听器并获取设备位置信息:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// 获取位置信息
}
});
(1)在AndroidManifest.xml文件中添加Google Play服务和定位权限:
(2)在代码中获取定位客户端实例:
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
(3)连接定位客户端并获取设备位置信息:
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
(1)获取Google Maps API密钥并在AndroidManifest.xml