Android - Google位置服务 - 构造函数LocationServices.GetAddressTask未定义

时间:2014-02-01 20:54:07

标签: java android geolocation

我正在尝试将融合的经度/经度坐标转换为街道地址,根据教程here. 一切都很好,直到这一行(线在最后):
(new GetAddressTask(this))。execute(mLocation);
它说的是什么 "The constructor LocationServices.GetAddressTask(LocationServices.GetAddressTask) is undefined."

我一般都是Java编程/ Android的新手,所以如果你能帮助我用非常棒的语言来帮助我。 这是代码:

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;

public class LocationServices extends FragmentActivity implements
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener {

private LocationClient mLocationClient;
Location mCurrentLocation;
private TextView mAddress;
private ProgressBar mActivityIndicator;

/*
 * Called by Location Services when the request to connect the
 * client finishes successfully. At this point, you can
 * request the current location or start periodic updates
 */
@Override
public void onConnected(Bundle dataBundle) {
    // Display the connection status
    mCurrentLocation = mLocationClient.getLastLocation();
    Toast.makeText(this, "Your location is " + mCurrentLocation, Toast.LENGTH_LONG).show();

}

/*
 * Called by Location Services if the connection to the
 * location client drops because of an error.
 */
@Override
public void onDisconnected() {
    // Display the connection status
    Toast.makeText(this, "Disconnected. Please re-connect.",
            Toast.LENGTH_SHORT).show();

}

@Override
public void onConnectionFailed(ConnectionResult arg0) {
    // TODO Auto-generated method stub

}

 @Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_location_services);
        /*
         * Create a new location client, using the enclosing class to
         * handle callbacks.
         */
        mLocationClient = new LocationClient(this, this, this);
        mAddress = (TextView) findViewById(R.id.address);
        mActivityIndicator = (ProgressBar) findViewById(R.id.address_progress);

    }

    /*
     * Called when the Activity becomes visible.
     */
    @Override
    protected void onStart() {
        super.onStart();
        // Connect the client.
        mLocationClient.connect();
    }

    /*
     * Called when the Activity is no longer visible.
     */
    @Override
    protected void onStop() {
        // Disconnecting the client invalidates it.
        mLocationClient.disconnect();
        super.onStop();
    }

    //////////////////////////////////////////////////////////////////////////////////////////////



    /**
     * A subclass of AsyncTask that calls getFromLocation() in the
     * background. The class definition has these generic types:
     * Location - A Location object containing
     * the current location.
     * Void     - indicates that progress units are not used
     * String   - An address passed to onPostExecute()
     */
     private class GetAddressTask extends
             AsyncTask<Location, Void, String> {
         Context mContext;
         public GetAddressTask(Context context) {
             super();
             mContext = context;
         }

         /**
          * Get a Geocoder instance, get the latitude and longitude
          * look up the address, and return it
          *
          * @params params One or more Location objects
          * @return A string containing the address of the current
          * location, or an empty string if no address can be found,
          * or an error message
          */
         @Override
         protected String doInBackground(Location...params) {
             Geocoder geocoder =
                     new Geocoder(mContext, Locale.getDefault());
             // Get the current location from the input parameter list
             Location loc = params[0];
             // Create a list to contain the result address
             List<Address> addresses = null;
             try {
                 /*
                  * Return 1 address.
                  */
                 addresses = geocoder.getFromLocation(loc.getLatitude(),
                         loc.getLongitude(), 1);
             } catch (IOException e1) {
             Log.e("LocationSampleActivity",
                     "IO Exception in getFromLocation()");
             e1.printStackTrace();
             return ("IO Exception trying to get address");
             } catch (IllegalArgumentException e2) {
             // Error message to post in the log
             String errorString = "Illegal arguments " +
                     Double.toString(loc.getLatitude()) +
                     " , " +
                     Double.toString(loc.getLongitude()) +
                     " passed to address service";
             Log.e("LocationSampleActivity", errorString);
             e2.printStackTrace();
             return errorString;
             }
             // If the reverse geocode returned an address
             if (addresses != null && addresses.size() > 0) {
                 // Get the first address
                 Address address = addresses.get(0);
                 /*
                  * Format the first line of address (if available),
                  * city, and country name.
                  */
                 String addressText = String.format(
                         "%s, %s, %s",
                         // If there's a street address, add it
                         address.getMaxAddressLineIndex() > 0 ?
                                 address.getAddressLine(0) : "",
                         // Locality is usually a city
                         address.getLocality(),
                         // The country of the address
                         address.getCountryName());
                 // Return the text
                 return addressText;
             } else {
                 return "No address found";
             }


             }
         /**
          * A method that's called once doInBackground() completes. Turn
          * off the indeterminate activity indicator and set
          * the text of the UI element that shows the address. If the
          * lookup failed, display the error message.
          */


         protected void onPostExecute (String address) {
             // Set activity indicator visibility to "gone"
             mActivityIndicator.setVisibility(View.GONE);
             // Display the results of the lookup.
             mAddress.setText(address);
         }

         public void getAddress(View v) {
             // Ensure that a Geocoder services is available

                 mActivityIndicator.setVisibility(View.VISIBLE);
                 (new GetAddressTask(this)).execute(mLocation);

         }
     }
}

1 个答案:

答案 0 :(得分:0)

因为this指向GetAddressTask对象而非Activity对象。 使用LocationServices.this之类的:

(new GetAddressTask(LocationServices.this)).execute(mLocation);