在 android 中从网络获取空位置
getting null location from network in android
我想从网络获取位置并将其保存在 sqlite 中。不幸的是,我总是将经度和纬度的值设为 0.0。尽管我能够连接到 wifi,但我无法找出问题所在。
这是包含调用定位方法的按钮的方法 class:
public void ButtonClick(View V) {
gps = new GPSTracker(this);
if (usersqlite_obj.RecvDate(DateOnly,shift)) {
Common.ShowDialogue(this, "Warning", "Your data has already been saved");
} else
{
latitude = gps.getLatitude();
longitude = gps.getLongitude();
usersqlite_obj.open();
usersqlite_obj.insertDAILYATTENDANCE(Recvdate, ffcode, terrcode,
drcode, drname, addr, Recvdate, ffmgr, shift,
Double.toString(latitude), Double.toString(longitude),
droid_ref_no, SyncStatus,unique_droid_ref_no,DateOnly);
usersqlite_obj.close();
Intent i = null;
i = new Intent(getApplicationContext(), Menu.class);
startActivity(i);
finish();
}
}
}
这是我用来获取位置的 GPSTracker class。
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0;
private static final long MIN_TIME_BW_UPDATES = 0;
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} 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);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
我在清单文件中设置了这些权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
我确定您必须等待
的更新
public void onLocationChanged(Location loc)
{
// then get the gps coordinated from
curlat = if(loc.hasLatitude())loc.getLatitude();
curlong = if(loc.hasLongitude())loc.getLongitude();
}
而您在收到位置更新之前要求变量,因此它将始终为空。另外你应该在获取它之前检查 loc 是否有值
我已经完成了任务 api:
这将根据您的 IP 地址为您提供当前位置,只需简单地调用此 JSON 即可获得所需的任何内容。
谢谢
我想从网络获取位置并将其保存在 sqlite 中。不幸的是,我总是将经度和纬度的值设为 0.0。尽管我能够连接到 wifi,但我无法找出问题所在。 这是包含调用定位方法的按钮的方法 class:
public void ButtonClick(View V) {
gps = new GPSTracker(this);
if (usersqlite_obj.RecvDate(DateOnly,shift)) {
Common.ShowDialogue(this, "Warning", "Your data has already been saved");
} else
{
latitude = gps.getLatitude();
longitude = gps.getLongitude();
usersqlite_obj.open();
usersqlite_obj.insertDAILYATTENDANCE(Recvdate, ffcode, terrcode,
drcode, drname, addr, Recvdate, ffmgr, shift,
Double.toString(latitude), Double.toString(longitude),
droid_ref_no, SyncStatus,unique_droid_ref_no,DateOnly);
usersqlite_obj.close();
Intent i = null;
i = new Intent(getApplicationContext(), Menu.class);
startActivity(i);
finish();
}
}
}
这是我用来获取位置的 GPSTracker class。
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0;
private static final long MIN_TIME_BW_UPDATES = 0;
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} 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);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
我在清单文件中设置了这些权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
我确定您必须等待
的更新public void onLocationChanged(Location loc)
{
// then get the gps coordinated from
curlat = if(loc.hasLatitude())loc.getLatitude();
curlong = if(loc.hasLongitude())loc.getLongitude();
}
而您在收到位置更新之前要求变量,因此它将始终为空。另外你应该在获取它之前检查 loc 是否有值
我已经完成了任务 api:
这将根据您的 IP 地址为您提供当前位置,只需简单地调用此 JSON 即可获得所需的任何内容。 谢谢