在android

时间:2015-07-02 08:13:29

标签: android location

我想从网络获取位置并将其保存在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" />

2 个答案:

答案 0 :(得分:0)

我确信你必须等待通过

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

而在收到位置更新之前,您要求变量,因此它将始终为null。此外,您应该在获取之前检查loc是否有值

答案 1 :(得分:0)

我已完成任务使用此API:

http://ip-api.com/json

这将通过您的IP地址为您提供当前位置,只需调用此JSON即可获得所需的任何内容。 谢谢