GPS位置仅来自网络

时间:2014-01-02 07:51:34

标签: android networking gps

我从网络获取位置而不是gps.i需要gps位置而不是网络提供商..但是我在我的日志文件中看到..我只是获得不准确的网络位置。所以我需要有gps位置。 。请帮我解决我做错的地方。提前谢谢..

if(isNetworkEnabled)
{
    try
    {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0,
        ll = new LocationListener() {

            public void onLocationChanged(Location loc) 
            {

                try {
                    location = loc;
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                    logFile log=new logFile();

                    dt = location.getTime();
                    log.createFile("Location Changed NETWORK:"+latitude+""+longitude+"time:"+dt);
                                        setdateAndtime(dt);
                } catch (Exception e) {
                    String errMsg= e.getClass().getName() + " " + e.getMessage();
                    logFile log=new logFile();
                    log.createFile("Error in GPS TRACKER When Location Changed"+errMsg+"\n");
                }

            }

            public void onProviderDisabled(String provider) {}

            public void onProviderEnabled(String provider) {}

            public void onStatusChanged(String provider, int status,
                                        Bundle extras) {}
        });

        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            dt = location.getTime();
            setdateAndtime(dt);

            logFile log=new logFile();
            log.createFile("Location From NETWORK:" + latitude + " " + longitude +
                     "time:" + dt + ":" + location.getProvider() + location);
        }

    }
    catch(Exception e)
    {
        System.out.println(e+"Error MSg");
    }
}
if(isGPSEnabled)
{
    try
    {
        if (location != null) {

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0,
            ll = new LocationListener() {
                public void onLocationChanged(Location loc) 
                {
                    try {
                        location = loc;
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        logFile log=new logFile();

                        dt = location.getTime();
                        log.createFile("Location Changed GPS:"+latitude+""+longitude+"time:"+dt);
                        setdateAndtime(dt);
                    } catch (Exception e) {
                        String errMsg= e.getClass().getName() + " " + e.getMessage();
                        logFile log=new logFile();
                        log.createFile("Error in GPS TRACKER When Location Changed"+errMsg+"\n");
                    }

                }

                public void onProviderDisabled(String provider) {}

                public void onProviderEnabled(String provider) {}

                public void onStatusChanged(String provider, int status,
                                        Bundle extras) {}
            });

            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
                dt = location.getTime();

                setdateAndtime(dt);

                logFile log=new logFile();
                log.createFile("Location From GPS:" + latitude + " " + longitude + 
                            "time:" + dt + ":" + location.getProvider() + location);
            }

4 个答案:

答案 0 :(得分:1)

更改订单和条件。

您的代码是..

if (enable_network)
{
  // get location from network provider
}

if (enable_gps)
{
  // get location from gps provider
}

您的代码始终从网络提供商处接收位置(如果已启用)。

因此,您应首先检查gps提供商条件,然后检查网络提供商。

更改此类代码

if (enable_gps)
{
  // get location from gps provider
}
else (enable_network)
{
  // get location from network provider
}
else
{
  // can not receive location data
}

答案 1 :(得分:1)

使用GpsProvider类检查GPS状态,启动GPS,如果GPS已启用则获取位置

public class GpsProvider extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

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

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);
        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // if GPS Enabled get lat/long using GPS Services
        if (isGPSEnabled) {
            this.canGetLocation = true;
            if (location == null) {
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("GPS Enabled", "GPS Enabled");
                if (locationManager != null) {
                    Log.d("GPS Enabled", "Managernot null");
                    location = locationManager
                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (location != null) {
                        Log.d("GPS Enabled", "location not null");
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
        }

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

    return location;
}

/**
 * Stop using GPS listener Calling this function will stop using GPS in your
 * app
 * */
public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GpsProvider.this);
    }
}

/**
 * Function to get latitude
 * */
public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude() {
    if (location != null) {
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}

/**
 * Function to check GPS/wifi enabled
 * 
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}


@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

}

在此之后,您可以通过以下代码检查任何活动中的GPS。

GpsProvider gpsProvider = new GpsProvider(this.getParent());

    // check if GPS enabled
    if (gpsProvider.canGetLocation()) {

        double latitude = gpsProvider.getLatitude();
        double longitude = gpsProvider.getLongitude();

        // \n is for new line
        Toast.makeText(
                getApplicationContext(),
                "Your Location is - \nLat: " + latitude + "\nLong: "
                        + longitude, Toast.LENGTH_LONG).show();
    } else {
        // can't get location
        // GPS is not enabled
        // Ask user to enable GPS in settings
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                this.getParent());

        // Setting Dialog Message
        alertDialog
                .setMessage("Your GPS seems to be disabled, do you want to enable it?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                        Intent intent = new Intent(
                                Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(intent);
                    }
                });

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                        finish();
                    }
                });
        // Setting Gravity to Center
        AlertDialog dialog = alertDialog.show();
        dialog.setCanceledOnTouchOutside(false);
        TextView messageText = (TextView) dialog
                .findViewById(android.R.id.message);
        messageText.setGravity(Gravity.CENTER);
        // Showing Alert Message
        dialog.show();

    }

快乐编码

答案 2 :(得分:0)

我在室内测试您的应用程序的事情,因为GPS在室内不起作用,以及它为什么与您的网络一起工作...尝试通过移动到外面检查应用程序...

答案 3 :(得分:0)

Avinash我相信你来自印度。为什么这很重要?因为在印度甚至在外面我无法通过LocationManager 从GPS获取位置我已经从LocationManager对GPS进行了如此多的测试。它似乎很少从GPS提供商返回位置。

解决方案
使用LocationClient(Google Play服务)检索当前位置。 Google Play服务非常准确,可以从网络,GPS和Wifi中检索位置(如果有)。 你有更多的机会可以LocationFollow this guide