在安卓中从网络获取空位置信息

3

我希望能够从网络中获取位置信息并保存在sqlite数据库中。但是不幸的是,无论怎样我都只能得到经度和纬度值为0.0的结果。尽管我可以连接到wifi,但我仍然无法找出问题所在。下面是包含调用位置信息方法类的按钮的方法:

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类,用于获取位置信息。
 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" />

你已经在设置中启用了GPS吗? - Lucifer
我不需要 GPS,看代码就可以了,我只使用网络。 - Andrain
你在哪里测试/部署这个应用程序?在真实设备上还是在模拟器上? - Lucifer
好的,您的设备连接Wifi正常吗? - Lucifer
是的,模拟器和设备已连接到WiFi。 - Andrain
显示剩余2条评论
2个回答

0

我已经使用这个API完成了任务:

http://ip-api.com/json

这将非常简单地从您的IP地址获取您当前的位置,只需调用此JSON即可获得所需的任何内容。谢谢


这听起来很有趣,我在安卓方面完全是新手,你可以给我解释一下 JSON 是什么吗?或者你能给我一个示例代码以便更好地理解吗? - Andrain
尝试这段代码。我希望它对你有所帮助。http://javapapers.com/android/android-location-using-gps-network-provider/ - Vishal Butani

0

我相信你一定需要等待更新

public void onLocationChanged(Location loc)
{ 
  // then get the gps coordinated from 
     curlat  = if(loc.hasLatitude())loc.getLatitude();
     curlong = if(loc.hasLongitude())loc.getLongitude();
}

由于您在接收位置更新之前请求变量,因此它将始终为空值。另外,在获取 loc 变量之前应该先检查其是否有值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接