在单独的课程中使用Google Play位置API?

时间:2015-03-09 22:57:12

标签: android google-maps gps google-play-services

在Android开发者指南中,Google展示了如何在活动类中使用Google Play服务API。将API调用卸载到单独的类中的最佳方法是什么?

public class GPSresource implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {


private Location location; // location
private double latitude; // latitude
private double longitude; // longitude
private GoogleApiClient mGAC;
private Context mContext;
public static final String TAG = "GPSresource";

public GPSresource(Context c)
{
    mContext = c;
    try {
        buildGoogleApiClient();
        mGAC.connect();
    }
    catch(Exception e)
    {
        Log.d(TAG,e.toString());
    }
}

protected synchronized void buildGoogleApiClient() {
    mGAC = new GoogleApiClient.Builder(mContext)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}


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;
}
@Override
public void onConnected(Bundle bundle) {
    location = LocationServices.FusedLocationApi.getLastLocation(mGAC);
}

@Override
public void onConnectionSuspended(int i) {

}


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

    }
}

我上面写的代码不起作用,因为从不调用onConnected,因为这不是一个活动。有没有更好的方法将GPS服务与主要活动分开,或者这是唯一的选择(如果有,是否有原因?)或许在主要活动中使一个主题在后台运行?

谢谢!

2 个答案:

答案 0 :(得分:1)

调用

onConnected,我在阅读日志时犯了一个错误!

答案 1 :(得分:0)

以下是一个例子:

public class LocationManager implements LocationListener {

private LocationManager locationManager;

public LocationManager()
   locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER))
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    if (locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER))
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

    Location  mobileLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (mobileLocation != null)
    {
        onLocationChanged(mobileLocation);
    }
    Location  netLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (netLocation != null)
    {
        onLocationChanged(netLocation);
    }
}


@Override
public void onLocationChanged(Location loc) {

    String longitude = "Longitude: " + loc.getLongitude();
    //Log.v(TAG, longitude);
    String latitude = "Latitude: " + loc.getLatitude();
   // Log.v(TAG, latitude);

    /*------- To get city name from coordinates -------- */
    String cityName = null;
    Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
    List<Address> addresses;
    try {
        addresses = gcd.getFromLocation(loc.getLatitude(),
                loc.getLongitude(), 1);
   // Log.e(TAG, "addr : " + addresses.toString());
        if (addresses.size() > 0)
        cityName = addresses.get(0).getLocality();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    String s = longitude + "\n" + latitude + "\n\nMy Current City is: "  + cityName;

   Log.e(TAG, "location : " + s);
}

@Override
public void onProviderDisabled(String provider) {
    Log.d("Latitude", "disable");
}

@Override
public void onProviderEnabled(String provider) {
    Log.d("Latitude","enable");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("Latitude","status");
}


public void pause() {
    if (locationManager != null)
    {
        locationManager.removeUpdates(this);
        locationManager = null;
    }
}

public void destroy() {
    if (locationManager != null)
    {
        locationManager.removeUpdates(this);
        locationManager = null;
    }
}

}