gpt4 book ai didi

android - getLastKnownLocation 在 Android 中返回 0,0

转载 作者:行者123 更新时间:2023-11-29 01:14:47 32 4
gpt4 key购买 nike

我正在尝试使用 GPS 简单地返回我当前位置的纬度/经度。无论我做什么,网络和 GPS 的返回都是 0,0。我试过从窗口走过去。

这是我的 GPS 代码:

import android.app.Service;

//
import android.Manifest;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;


import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;


public class TrackGPS extends Service implements LocationListener {

private final Context mContext;


boolean checkGPS = false;


boolean checkNetwork = false;

boolean canGetLocation = false;

Location loc;
double latitude;
double longitude;


private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;


private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
protected LocationManager locationManager;

public TrackGPS(Context mContext) {
this.mContext = mContext;
getLocation();
}

private Location getLocation() {

try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);

// getting GPS status
checkGPS = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);

// getting network status
checkNetwork = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!checkGPS && !checkNetwork) {
Toast.makeText(mContext, "No Service Provider Available", Toast.LENGTH_SHORT).show();
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (checkNetwork) {
Toast.makeText(mContext, "Network", Toast.LENGTH_SHORT).show();

try {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
0,
0, this);
Log.d("Network", "Network");
if (locationManager != null) {
loc = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

}

if (loc != null) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
}
} catch (SecurityException e) {

}
}
}
// if GPS Enabled get lat/long using GPS Services
if (checkGPS) {
Toast.makeText(mContext, "GPS", Toast.LENGTH_SHORT).show();
if (loc == null) {
try {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
loc = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc != null) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
}
}
} catch (SecurityException e) {

}
}
}

} catch (Exception e) {
e.printStackTrace();
}

return loc;
}

public double getLongitude() {
if (loc != null) {
longitude = loc.getLongitude();
}
return longitude;
}

public double getLatitude() {
if (loc != null) {
latitude = loc.getLatitude();
}
return latitude;
}

public boolean canGetLocation() {
return this.canGetLocation;
}

public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);


alertDialog.setTitle("GPS Not Enabled");

alertDialog.setMessage("Do you wants to turn On GPS");


alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});


alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});


alertDialog.show();
}


public void stopUsingGPS() {
if (locationManager != null) {

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;
}
locationManager.removeUpdates(TrackGPS.this);
}
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onLocationChanged(Location location) {
// this.location = location;
getLatitude();
getLongitude();
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
}

这是我的主要 Activity :

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private Button b_get;
private TrackGPS gps;
double longitude;
double latitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b_get = (Button)findViewById(R.id.get);



b_get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

gps = new TrackGPS(MainActivity.this);


if(gps.canGetLocation()){


longitude = gps.getLongitude();
latitude = gps .getLatitude();

Toast.makeText(getApplicationContext(),"Longitude:"+Double.toString(longitude)+"\nLatitude:"+Double.toString(latitude),Toast.LENGTH_SHORT).show();
}
else
{

gps.showSettingsAlert();
}

}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
gps.stopUsingGPS();
}
}

这是我的 Android list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.quantums3.getcurrentlocation">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

</manifest>

提前致谢。

最佳答案

getLastKnownLocation 在 Android 中返回 0,0

实际上,上述方法在您的情况下返回 null,并且由于您的 TrackGPS 结构,您看到的默认值是 double latitude; double longitude;

关于android - getLastKnownLocation 在 Android 中返回 0,0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40555299/

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