Android 打开gps后如何获取我的位置
我是第一次使用谷歌地图,所以请帮我。我想在地图上显示用户的位置,当它打开应用程序。我这样做时,用户已经打开gpsAndroid 打开gps后如何获取我的位置,android,google-maps,location,Android,Google Maps,Location,我是第一次使用谷歌地图,所以请帮我。我想在地图上显示用户的位置,当它打开应用程序。我这样做时,用户已经打开gps LocationManager locationManagerCt = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE); locationCt = locationManagerCt .getLastKnownLocation(Location
LocationManager locationManagerCt = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
locationCt = locationManagerCt
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng latLng=new LatLng(locationCt.getLatitude(), locationCt.getLongitude());
MarkerOptions marker = new MarkerOptions().position(latLng).title("Current Location").snippet("Discription");
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
// Moving Camera to a Location with animation
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng).zoom(15).build();
HomeActivity.mGoogleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
HomeActivity.mGoogleMap.addMarker(marker);
当gps关闭时。我试着在打开Gps后做同样的事情。
我从这里打开gps:
用户打开gps后
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1000) {
if(resultCode == Activity.RESULT_OK){
//after user press ok button on GPS popup
/*I've apply same code above but unable to get location
its give me error on Location (nullpointer) */
} if (resultCode == Activity.RESULT_CANCELED) {
}
}
}
如何在几秒钟内打开GPS后获取位置。
我不想刷新活动
谢谢大家! 请使用此库,它非常有效
首先,切换到
FusedLocationApi
,而不是现在很旧的LocationManager。您需要使用FusedLocationAPI的requestLocationUpdates()
方法
浏览谷歌示例了解详细信息:
请像这样试试
private FusedLocationProviderClient mFusedLocationClient;
LocationRequest mLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
ButterKnife.bind(this);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}
public void requestLocationUpdates() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(120000); // two minute interval
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}
}
@Override
public void onConnected(Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
requestLocationUpdates();
mFusedLocationClient.getLastLocation()
.addOnCompleteListener(this, new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful() && task.getResult() != null) {
mLastLocation = task.getResult();
} else {
Log.w(TAG, "getLastLocation:exception", task.getException());
Toast.makeText(mContext, "No Location Detected.", Toast.LENGTH_LONG).show();
}
}
});
}
专用FusedLocationProviderClient mFusedLocationClient;
位置请求mLocationRequest;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
把(这个)绑起来;
mFusedLocationClient=LocationServices.getFusedLocationProviderClient(此);
}
public void requestLocationUpdates(){
mlLocationRequest=新位置请求();
mlLocationRequest.setInterval(120000);//两分钟间隔
mlLocationRequest.setFastTestInterval(1000);
mLocationRequest.setPriority(位置请求、优先级、平衡、功率、精度);
如果(ContextCompat.checkSelfPermission)(此,
清单.权限.访问(位置)
==PackageManager.权限(已授予){
mFusedLocationClient.RequestLocationUpdate(mlLocationRequest、mlLocationCallback、Looper.myLooper());
}
}
@凌驾
未连接的公共空间(捆绑包){
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
考虑到呼叫
//ActivityCompat#请求权限
//在此处请求缺少的权限,然后覆盖
//public void onRequestPermissionsResult(int-requestCode,字符串[]权限,
//int[]格兰特结果)
//处理用户授予权限的情况。请参阅文档
//对于ActivityCompat,请请求权限以获取更多详细信息。
返回;
}
requestLocationUpdates();
mFusedLocationClient.getLastLocation()文件
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()&&task.getResult()!=null){
mLastLocation=task.getResult();
}否则{
w(标记“getLastLocation:exception”,task.getException());
Toast.makeText(mContext,“未检测到位置”),Toast.LENGTH_LONG.show();
}
}
});
}