gpt4 book ai didi

android - LocationServices.FusedLocationApi.requestLocationUpdates 永远不会被调用

转载 作者:行者123 更新时间:2023-11-30 00:31:56 26 4
gpt4 key购买 nike

我没有使用 LocationManager,而是使用 LocationServices 方法来获取当前的 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 方法。

最佳答案

我在这里开发了融合位置 api 演示应用程序和实用程序包。

General Utilities

如果对您有用,请尝试一下。要使用融合位置 api 获取位置,您只需编写以下代码 fragment ...

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();
}
}
}

希望对你有所帮助。

关于android - LocationServices.FusedLocationApi.requestLocationUpdates 永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44173000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com