gpt4 book ai didi

java - onLocationChange() 未被调用...使用 FuseLocationApi ...检查了所有解决方案并尝试了所有...但仍然没有任何效果

转载 作者:行者123 更新时间:2023-12-02 02:20:54 25 4
gpt4 key购买 nike

出于检查目的,我在代码中的每个方法中插入了 log 。 onConnected() 方法被调用,但它不会进一步执行 OnLocationChanged()

DriverMapActivity

public class DriverMapActivity extends FragmentActivity implements OnMapReadyCallback, View.OnClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {

private GoogleMap mMap;
private Button save;
GoogleApiClient mgooogleapiclient;
Location mlastlocation;
LocationRequest mlocationRequest;
Marker marker;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver_map);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
save = (Button) findViewById(R.id.save);
save.setOnClickListener(this);
}


@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Add a marker in Sydney and move the camera
// LatLng sydney = new LatLng(-34, 151);
// mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
// mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
Log.d("conn","api got");

}
else
{
checkLocationPermission();
Log.d("conn","api gotout");
}
Log.d("conn","api gotout");



}

protected synchronized void buildGoogleApiClient() {
mgooogleapiclient=new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mgooogleapiclient.connect();
Log.d("buildapi","api co");
}

@Override
public void onClick(View v) {


}

@Override
public void onConnected(@Nullable Bundle bundle) {

mlocationRequest = new LocationRequest();
mlocationRequest.setInterval(1000);
mlocationRequest.setFastestInterval(1000);
mlocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

LocationServices.FusedLocationApi.requestLocationUpdates(mgooogleapiclient, mlocationRequest, this);
}
Log.d("conn","in on vconnected");

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void onLocationChanged(Location location) {
mlastlocation=location;
LatLng latLng=new LatLng(location.getLatitude(),location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
Log.d("see location","lati: "+location.getLatitude()+" longi:"+location.getLongitude());
MarkerOptions markerOptions=new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
if(marker!=null)
{
marker.remove();
}
marker=mMap.addMarker(markerOptions);

DatabaseReference ref=FirebaseDatabase.getInstance().getReference("DriversAvailable");
GeoFire geoFire=new GeoFire(ref);
geoFire.setLocation("DriverId",new GeoLocation(location.getLatitude(),location.getLongitude()));
}

@Override
protected void onStop() {
super.onStop();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference("DriversAvailable");
GeoFire geoFire=new GeoFire(ref);
geoFire.removeLocation("DriverId");
}
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
private void checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)) {

// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
new AlertDialog.Builder(this)
.setTitle("Location Permission Needed")
.setMessage("This app needs the Location permission, please accept to use location functionality")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(DriverMapActivity.this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION );
Log.d("conn","in dialog box");
}
})
.create()
.show();


} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION );
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {


if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {

if (mgooogleapiclient == null) {
buildGoogleApiClient();
}
mMap.setMyLocationEnabled(true);
LocationServices.FusedLocationApi.requestLocationUpdates(mgooogleapiclient, mlocationRequest, this);
}

} else {


Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
}
return;
}


}
}
}

Android list 文件

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />

<activity
android:name=".DriverMapActivity"
android:label="@string/title_activity_driver_map">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

构建 Gradle 文件

apply plugin: 'com.android.application

android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.user.vypodriver"
minSdkVersion 17
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.google.android.gms:play-services-maps:11.0.4'
implementation 'com.google.firebase:firebase-database:11.0.4'
testImplementation 'junit:junit:4.12'
implementation 'com.google.android.gms:play-services-location:11.0.4'
compile 'com.firebase:geofire-android:2.2.0'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
apply plugin: 'com.google.gms.google-services'

**这里我试图每秒获取驱动程序位置并希望将其更新到 firebase 但 onLocationChange 不会被调用,任何操作都会完成。尝试在运行 Noghut 的 Android 手机上运行它 **

最佳答案

可能由于您没有位置权限,因此未调用 requestLocationUpdates 方法。

在这里添加一个else:

if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED 
&& ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mgooogleapiclient, mlocationRequest, this);
} else {
checkLocationPermission();
}

关于java - onLocationChange() 未被调用...使用 FuseLocationApi ...检查了所有解决方案并尝试了所有...但仍然没有任何效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48522678/

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