简体   繁体   中英

How to get user location without gps or wifi?

how to get the user location without gps? I mean using the data services or network. Is it possible? Can anyone please give any sample code to look at on that?

Here is my stripped down Location Service I am using right now. GPS and Network are called Providers in this context.

I've changed the way my logic works, where it checks which provider is enabled and then returns the best one that's enabled (GPS is better then network), to this line: String providerToSend = providers.get(1);, in the GetProvider method. So instead of checking each provider, this way it chooses providers.get(1), which is always the string NETWORK. Feed that to your locationManager.getLastKnownLocation(TheStringWithTheCorrectProviderGoesHere), and it'll return your position.

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import java.util.List;


public class LocationService extends Service implements LocationListener{

    protected LocationManager locationManager;

    public String providerNow;
    private List<String> providers;

    public Location location;
    private final Context mContext;


    public LocationService(Context context) {
        Log.i("TestMap", "LocationService");


        this.mContext = context;

        locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,0,this);

        providerNow = (GetProvider());
    }



    public Location getLocation() {

        location = locationManager.getLastKnownLocation(providerNow);
        return location;
    }


    public String GetProvider (){

        Criteria criteria = new Criteria();
        criteria.setAccuracy(2);

        providers = locationManager.getProviders(true);
        String providerToSend = providers.get(1);

        return providerToSend;

    }

    public IBinder onBind(Intent intent) {
        Log.i("TestMap", "onBind");
        //TODO for communication return IBinder implementation
        return null;
    }


    public void onLocationChanged(Location location) {

        Location locationNew = location;
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {
        providerNow = (GetProvider());
    }

    @Override
    public void onProviderDisabled(String provider) {
        providerNow = (GetProvider());
    }
}

Android stores last location, u can get it http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String)

Many examples first read lastKnown then looks more data from providers how to get gps location android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM