在我能够在我的应用程序中读取当前位置(国家,城市,地区等)之前,是否需要先打开Internet和关闭GPS?如果是,那么任何替代方式只能从GPS获取位置?因为互联网可用性是一个问题.
我正在尝试为我的新导航应用程序获取用户位置.我想经常检查用户位置,它必须准确.我使用示例中的以下代码来获取位置.
public class MainActivity extends Activity implements LocationListener {
private LocationManager locationManager;
private String provider;
private TextView a;
private TextView b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a = (TextView) findViewById(R.id.a);
b = (TextView) findViewById(R.id.b);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
a.setText("Location not available");
b.setText("Location not available");
}
} …Run Code Online (Sandbox Code Playgroud) 我没有使用模拟位置......事实上,上周代码工作正常.
我有一个应用程序收集GPS数据并使用应用程序本身生成的X,Y坐标吐出谷歌地图链接.我不是百分之百确定为什么它不按预期的方式工作但是当我要求应用程序根据手机提供的GPS位置制作谷歌地图链接时它告诉我我距离我的点5到6个街区起源(当时我实际上在哪里)不是我想要的
的已知,:
以下是收集GPS信息的代码:
Toast.makeText(context, "Gathering GPS Data...", Toast.LENGTH_SHORT).show();
gps = new gpsTracker(Settings.this);
if(gps.canGetLocation()){
try{
gps.getLocation();
lon = gps.getLongitude();
lat = gps.getLatitude();
Toast.makeText(getApplicationContext(), "Your location is - \nlat: " + lat + "\nlon: " + lon, Toast.LENGTH_LONG).show();
}
catch(Exception e){}
}
else{
gps.showSettingsAlert();
}
Run Code Online (Sandbox Code Playgroud)
所有上述课程都来自:
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
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.util.Log;
public class gpsTracker extends Service implements LocationListener {
private …Run Code Online (Sandbox Code Playgroud) 我想在android中获取当前的确切位置坐标.如果移动设备连接到Wifi互联网连接,我能够获得位置,但在其他情况下,当我去其他地方并且没有可用的互联网连接时,它不会给出当前的位置坐标.而是给出最后的位置坐标.
例如:
目前我在一个城市并通过wifi连接获取当前坐标并移动到城市B然后我无法获取当前坐标.而不是它给出最后的位置坐标.
谁能建议我如何在没有互联网连接的情况下获得当前位置.
public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
} …Run Code Online (Sandbox Code Playgroud)