Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android当前位置_Android_Api - Fatal编程技术网

Android当前位置

Android当前位置,android,api,Android,Api,我的问题如下。我想创建一个API来使用Android应用程序获取用户的当前位置。关于这一点,我无法找到解决方案。在应用程序代码中我必须做什么?或者在android参考中有没有预定义的东西来获取我开发的应用程序的位置?谢谢你的回答,但我应该更详细地回答我的问题。我的问题是,如果我想获得特定用户的位置,因为应用程序将由多个设备安装,该怎么办? public class GPSTracker extends Service implements LocationListener { private

我的问题如下。我想创建一个API来使用Android应用程序获取用户的当前位置。关于这一点,我无法找到解决方案。在应用程序代码中我必须做什么?或者在android参考中有没有预定义的东西来获取我开发的应用程序的位置?

谢谢你的回答,但我应该更详细地回答我的问题。我的问题是,如果我想获得特定用户的位置,因为应用程序将由多个设备安装,该怎么办?
public class GPSTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = 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 = 2; // 2 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 5000 * 1 ; // 5seconds 

// Declaring a Location Manager
protected LocationManager locationManager;

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

private Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled)  //&& !isNetworkEnabled) {
            Log.e("GPS", "no network provider is enabled");            
        else 
        {
            this.canGetLocation = true;
            // First get location from Network Provider
            setupRequestLocationUpdates();            

        }

    } 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(GPSTracker.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;
}
}

// To consume it in activity:
gps = new GPSTracker(this);
@Override
protected void onResume() {
    super.onResume();
    gps.setupRequestLocationUpdates();
}

@Override
protected void onPause() {
    super.onPause();
    gps.stopUsingGPS();
}