此处将为大家介绍关于android–如何使用FusedLocationProviderApi和GoogleClient检查位置是否可用?的详细内容,并且为您解答有关android获取位置的相关问题,此
此处将为大家介绍关于android – 如何使用FusedLocationProviderApi和GoogleClient检查位置是否可用?的详细内容,并且为您解答有关android 获取位置的相关问题,此外,我们还将为您介绍关于addOnFailureListener 如何在 FusedLocationProviderClient 中工作以及为什么 addOnSuccessListener 不起作用?、Android Google Maps LocationClient、Android Google Map如何检查gps位置是否在圆圈内、Android Location LocationListener始终调用onProviderDisabled的有用信息。
本文目录一览:- android – 如何使用FusedLocationProviderApi和GoogleClient检查位置是否可用?(android 获取位置)
- addOnFailureListener 如何在 FusedLocationProviderClient 中工作以及为什么 addOnSuccessListener 不起作用?
- Android Google Maps LocationClient
- Android Google Map如何检查gps位置是否在圆圈内
- Android Location LocationListener始终调用onProviderDisabled
android – 如何使用FusedLocationProviderApi和GoogleClient检查位置是否可用?(android 获取位置)
我正在尝试使用新api的位置,即fusedLocationProviderApi并尝试检查是否可以检索位置.
怎么检查?
解决方法
LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
通过将FusedLocationApi与Googleapiclient和LocationRequest一起使用,您可以使用LocationListener接口请求位置更新,该接口将在更改位置时调用回调方法. onLocationChanged(位置位置).因此,您可以知道是否可以在onLocationChanged方法中授予位置.
所以流程示例可以是;检查位置提供商是否已启用,然后尝试连接到Googleapiclient以查看用户是否具有可以获取用户位置的GooglePlayServices等.
addOnFailureListener 如何在 FusedLocationProviderClient 中工作以及为什么 addOnSuccessListener 不起作用?
如何解决addOnFailureListener 如何在 FusedLocationProviderClient 中工作以及为什么 addOnSuccessListener 不起作用??
我需要获取上次定位电话,但我不明白为什么它不起作用。这是我的代码,你能告诉我我做错了什么吗。
private lateinit var fusedLocationClient: FusedLocationProviderClient
var lastLocation: Location? = null
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
if (ActivityCompat.checkSelfPermission(
context,Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
context,Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
fusedLocationClient.flushLocations()
fusedLocationClient.lastLocation.addOnSuccessListener {
listener -> lastLocation = listener
}.addOnFailureListener{
}
解决方法
如果条件允许,将代码移出
fusedLocationClient.flushLocations()
fusedLocationClient.lastLocation.addOnSuccessListener {
listener -> lastLocation = listener
}.addOnFailureListener{
}
- 通过设备或模拟器中的应用信息检查位置权限
- 检查设备中的启用位置
- 打开谷歌地图保存位置
Android Google Maps LocationClient
我在Android应用程序中使用Google Maps Android API v2 LocationClient:
http://developer.android.com/reference/com/google/android/gms/location/LocationClient.html
我自己打电话给connect()和断开连接().
我还为GooglePlayServicesClient.ConnectionCallbacks注册了一个侦听器:
http://developer.android.com/reference/com/google/android/gms/common/GooglePlayServicesClient.ConnectionCallbacks.html
我观察到在LocationClient上调用connect()之后,在侦听器上调用了回调onConnected().
但是,似乎在LocationClient上调用disconnect()之后,从未在侦听器上调用过ondisconnected().
这是正常行为吗?
解决方法:
我想有点晚了,但我也想知道,所以我会回复.
这是正常的行为. api文档尚不清楚,但是在官方教程http://developer.android.com/training/location/retrieve-current.html中,它说明了ondisconnected:
Called by Location Services if the connection to the location client
drops because of an error.
这意味着如果您自己调用disconnect(),则不应调用它.
Android Google Map如何检查gps位置是否在圆圈内
public class MapaEscola extends FragmentActivity { private GoogleMap googleMap; private Serializable escolas; private ProgressDialog dialog; private Circle mCircle; private Marker mMarker; @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getActionBar().setdisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); setContentView(R.layout.maps); // Loading map initilizeMap(); // Changing map type googleMap.setMapType(GoogleMap.MAP_TYPE_norMAL); // Showing / hiding your current location googleMap.setMyLocationEnabled(true); // Enable / disable zooming controls googleMap.getUiSettings().setZoomControlsEnabled(true); // Enable / disable my location button googleMap.getUiSettings().setMyLocationButtonEnabled(true); // Enable / disable Compass icon googleMap.getUiSettings().setCompassEnabled(true); // Enable / disable Rotate gesture googleMap.getUiSettings().setRotateGesturesEnabled(true); // Enable / disable zooming functionality googleMap.getUiSettings().setZoomGesturesEnabled(true); Bundle extra = getIntent().getBundleExtra("extra"); ArrayList<Escolas> objects = (ArrayList<Escolas>) extra.getSerializable("array"); try { for(int i = 0; i < objects.size(); i ++) { System.out.println(" escolas " + objects.get(i).getLatitude() + " " + objects.get(i).getLongitude()); float latitude = objects.get(i).getLatitude(); float longitude = objects.get(i).getLongitude(); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-23.316281,-51.155528),15)); MarkerOptions options = new MarkerOptions(); // Setting the position of the marker options.position(new LatLng(latitude,longitude)); googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); LatLng latLng = new LatLng(latitude,longitude); drawMarkerWithCircle(latLng); googleMap.setonMyLocationchangelistener(new GoogleMap.OnMyLocationchangelistener() { @Override public void onMyLocationChange(Location location) { float[] distance = new float[2]; Location.distanceBetween( mMarker.getPosition().latitude,mMarker.getPosition().longitude,mCircle.getCenter().latitude,mCircle.getCenter().longitude,distance); if( distance[0] > (mCircle.geTradius() / 2) ){ Toast.makeText(getBaseContext(),"Outside",Toast.LENGTH_LONG).show(); } else { Toast.makeText(getBaseContext(),"Inside",Toast.LENGTH_LONG).show(); } } }); } } catch (Exception e) { e.printstacktrace(); } } private void drawMarkerWithCircle(LatLng position){ double radiusInMeters = 500.0; int strokeColor = 0xffff0000; //red outline int shadeColor = 0x44ff0000; //opaque red fill CircleOptions circleOptions = new CircleOptions().center(position).radius(radiusInMeters).fillColor(shadeColor).strokeColor(strokeColor).strokeWidth(8); mCircle = googleMap.addCircle(circleOptions); MarkerOptions markerOptions = new MarkerOptions().position(position); mMarker = googleMap.addMarker(markerOptions); } private void initilizeMap() { if (googleMap == null) { googleMap = ((MapFragment) getFragmentManager().findFragmentById( R.id.map)).getMap(); // check if map is created successfully or not if (googleMap == null) { Toast.makeText(getApplicationContext(),"Não foi possível carregar o mapa",Toast.LENGTH_SHORT) .show(); } } } @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public void onBackpressed() { super.onBackpressed(); finish(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Todo Auto-generated method stub MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_main,menu); return super.onCreateOptionsMenu(menu); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) public boolean onoptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: super.onBackpressed(); finish(); return true; } return true; } @Override protected void onResume() { super.onResume(); initilizeMap(); } }
解决方法
您应该使用传入onMyLocationChange()回调的位置,以便它使用您当前的位置来判断设备是否在圆圈内:
googleMap.setonMyLocationchangelistener(new GoogleMap.OnMyLocationchangelistener() { @Override public void onMyLocationChange(Location location) { float[] distance = new float[2]; /* Location.distanceBetween( mMarker.getPosition().latitude,distance); */ Location.distanceBetween( location.getLatitude(),location.getLongitude(),distance); if( distance[0] > mCircle.geTradius() ){ Toast.makeText(getBaseContext(),"Outside,distance from center: " + distance[0] + " radius: " + mCircle.geTradius(),Toast.LENGTH_LONG).show(); } else { Toast.makeText(getBaseContext(),"Inside,Toast.LENGTH_LONG).show(); } } });
以下是我运行的全部工作示例,它是您的原始代码的缩减版本:
public class MainActivity extends ActionBaractivity { private GoogleMap googleMap; private Serializable escolas; private ProgressDialog dialog; private Circle mCircle; private Marker mMarker; @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().setdisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); setContentView(R.layout.activity_main); // Loading map initilizeMap(); // Changing map type googleMap.setMapType(GoogleMap.MAP_TYPE_norMAL); // Showing / hiding your current location googleMap.setMyLocationEnabled(true); // Enable / disable zooming controls googleMap.getUiSettings().setZoomControlsEnabled(true); // Enable / disable my location button googleMap.getUiSettings().setMyLocationButtonEnabled(true); // Enable / disable Compass icon googleMap.getUiSettings().setCompassEnabled(true); // Enable / disable Rotate gesture googleMap.getUiSettings().setRotateGesturesEnabled(true); // Enable / disable zooming functionality googleMap.getUiSettings().setZoomGesturesEnabled(true); // Bundle extra = getIntent().getBundleExtra("extra"); //ArrayList<Escolas> objects = (ArrayList<Escolas>) extra.getSerializable("array"); try { //test outside double mLatitude = 37.77657; double mLongitude = -122.417506; //test inside //double mLatitude = 37.7795516; //double mLongitude = -122.39292; googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLatitude,mLongitude),15)); MarkerOptions options = new MarkerOptions(); // Setting the position of the marker options.position(new LatLng(mLatitude,mLongitude)); //googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); LatLng latLng = new LatLng(mLatitude,mLongitude); drawMarkerWithCircle(latLng); googleMap.setonMyLocationchangelistener(new GoogleMap.OnMyLocationchangelistener() { @Override public void onMyLocationChange(Location location) { float[] distance = new float[2]; /* Location.distanceBetween( mMarker.getPosition().latitude,distance); */ Location.distanceBetween( location.getLatitude(),distance); if( distance[0] > mCircle.geTradius() ){ Toast.makeText(getBaseContext(),Toast.LENGTH_LONG).show(); } } }); } catch (Exception e) { e.printstacktrace(); } } private void drawMarkerWithCircle(LatLng position){ double radiusInMeters = 500.0; int strokeColor = 0xffff0000; //red outline int shadeColor = 0x44ff0000; //opaque red fill CircleOptions circleOptions = new CircleOptions().center(position).radius(radiusInMeters).fillColor(shadeColor).strokeColor(strokeColor).strokeWidth(8); mCircle = googleMap.addCircle(circleOptions); MarkerOptions markerOptions = new MarkerOptions().position(position); mMarker = googleMap.addMarker(markerOptions); } private void initilizeMap() { if (googleMap == null) { googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById( R.id.map)).getMap(); // check if map is created successfully or not if (googleMap == null) { Toast.makeText(getApplicationContext(),menu); return super.onCreateOptionsMenu(menu); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) public boolean onoptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: super.onBackpressed(); finish(); return true; } return true; } @Override protected void onResume() { super.onResume(); initilizeMap(); } }
圈内结果:
圆圈外的结果:
Android Location LocationListener始终调用onProviderDisabled
compileSdkVersion 24 buildToolsversion "24.0.1" minSdkVersion 21 targetSdkVersion 24
从不调用LocationListener onLocationChanged().它总是调用onProviderdisabled().
这是MainActivity.java
TextView mTextView; LocationManager mLocationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.tv); if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } mLocationManager = (LocationManager) this .getSystemService(Context.LOCATION_SERVICE); mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1 * 1000,0.00001F,locationListener); } private void updateUIToNewLocation(Location location) { Log.d("hello","updateUIToNewLocation = "); if (location != null) { mTextView.setText("Latitude:" + location.getLatitude() + "\nLongitude:" + location.getLongitude()); } else { mTextView.setText("error"); } } LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { Log.d("hello","onLocationChanged"); System.out.println("onLocationChanged"); System.out.println("Latitude:" + location.getLatitude() + "\nLongitude" + location.getLongitude()); updateUIToNewLocation(location); } public void onStatusChanged(String provider,int status,Bundle extras) { Log.d("hello","onStatusChanged"); System.out.println("onStatusChanged"); System.out.println("privider:" + provider); System.out.println("status:" + status); System.out.println("extras:" + extras); } public void onProviderEnabled(String provider) { Log.d("hello","onProviderEnabled"); System.out.println("onProviderEnabled"); System.out.println("privider:" + provider); } public void onProviderdisabled(String provider) { Log.d("hello","onProviderdisabled"); System.out.println("onProviderdisabled"); System.out.println("privider:" + provider); } };
这是manifest xml中的权限和Activity标签.
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <!-- Needed only if your app targets Android 5.0 (API level 21) or higher. --> <uses-feature android:name="android.hardware.location.network"/> <uses-feature android:name="android.hardware.location.gps" /> <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="24"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application>
此应用程序可以在Android 5.1上运行,但在Android 6.0.1中不起作用.希望有人可以帮助我.我不明白这个问题.
解决方法
今天的关于android – 如何使用FusedLocationProviderApi和GoogleClient检查位置是否可用?和android 获取位置的分享已经结束,谢谢您的关注,如果想了解更多关于addOnFailureListener 如何在 FusedLocationProviderClient 中工作以及为什么 addOnSuccessListener 不起作用?、Android Google Maps LocationClient、Android Google Map如何检查gps位置是否在圆圈内、Android Location LocationListener始终调用onProviderDisabled的相关知识,请在本站进行查询。
本文标签: