LocationServices.FusedLocationApi.requestLocationUpdates 永远不会被调用
Posted
技术标签:
【中文标题】LocationServices.FusedLocationApi.requestLocationUpdates 永远不会被调用【英文标题】:LocationServices.FusedLocationApi.requestLocationUpdates is never getting called 【发布时间】:2017-05-25 05:34:42 【问题描述】:我使用 LocationServices 方法代替 LocationManager 来获取当前 gps 位置和位置更新。
这是代码:
public void onConnected(@Nullable Bundle bundle)
mLocationRequest = new LocationRequest();//PRIORITY_BALANCED_POWER_ACCURACY
mLocationRequest.setInterval(1000*30*1);
mLocationRequest.setFastestInterval(1000*30*1);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED)
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest,this);
public void onLocationChanged(Location location) mLastLocation = location;
if (mCurrLocationMarker != null)
mCurrLocationMarker.remove();
//Place current location marker
latLng = new LatLng(location.getLatitude(), location.getLongitude());
lat= location.getLatitude();
log=location.getLongitude();
trim_check_location_change(lat,log);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(""+s);
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mCurrLocationMarker = mMap.addMarker(markerOptions);
//move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
CurrentLocation = latLng;
当我从一个地方移动到另一个地方时,蓝色标记会相应移动。 但是 onlocationchanged 方法永远不会被调用。
【问题讨论】:
【参考方案1】:我在这里开发了融合位置 api 演示应用程序和实用程序包。
General Utilities
如果对您有用,请尝试。要使用融合位置 api 获取位置,您只需编写以下 sn-p...
new LocationHandler(this)
.setLocationListener(new LocationListener()
@Override
public void onLocationChanged(Location location)
// Get the best known location
).start();
如果您想自定义它,只需在此处查找文档...
https://github.com/abhishek-tm/general-utilities-android/wiki/Location-Handler
更新
我已经根据你的需要写了一个示例代码,试试这个...
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import in.teramatrix.utilities.service.LocationHandler;
import in.teramatrix.utilities.util.MapUtils;
/**
* Lets see how to use utilities module by implementing location listener.
*
* @author Khan
*/
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, LocationListener
private GoogleMap map;
private Marker marker;
private LocationHandler locationHandler;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Obtaining an instance of map
FragmentManager manager = getSupportFragmentManager();
SupportMapFragment mapFragment = (SupportMapFragment) manager.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
this.locationHandler = new LocationHandler(this)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(5000)
.setFastestInterval(10000)
.setLocationListener(this);
@Override
public void onMapReady(GoogleMap map)
this.map = map;
this.locationHandler.start();
@Override
public void onLocationChanged(Location location)
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
if (marker == null)
marker = MapUtils.addMarker(map, latLng, R.drawable.ic_current_location);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14), 500, null);
else
marker.setPosition(latLng);
@Override
protected void onDestroy()
super.onDestroy();
if (locationHandler != null)
locationHandler.stop();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == LocationHandler.REQUEST_LOCATION)
locationHandler.start();
希望对你有帮助。
【讨论】:
我检查了你的 github 代码。我将距离更改为 1 米,并移动了几米。我使用敬酒消息来指示位置更改。但 onLocatioChanged 仍然无法正常工作。 @cgeek GPS 无法识别此距离,该距离仅几米。出去看看。它应至少为 10-20 米。 包含你的 github 代码。完美运行.. 谢谢。以上是关于LocationServices.FusedLocationApi.requestLocationUpdates 永远不会被调用的主要内容,如果未能解决你的问题,请参考以下文章