0

Im trying to change this code to do reverse geocoding as well. Coordinates work fine. Im gettin the error: location cannot be resolved to a variable. This error is at this line of code:

List<Address> addresses = geocoder.getFromLocation(location.getLatitude,
    location.getLongitude, 1); 

My full code is:

package gps.attempt;

   import java.io.IOException;
   import java.util.List;
   import java.util.Locale;

   import android.app.Activity;
   import android.content.Intent;
   import android.location.Address;
   import android.location.Location;
   import android.location.Geocoder;
   import android.location.LocationListener;
   import android.location.LocationManager;
   import android.location.LocationProvider;
   import android.os.Bundle;
   import android.util.Log;
   import android.widget.TextView;
   import android.widget.Toast;

   public class GpsAttemptActivity extends Activity implements LocationListener {

/* this class implements LocationListener, which listens for both
 * changes in the location of the device and changes in the status
 * of the GPS system.
 * */

static final String tag = "Main"; // for Log

TextView txtInfo;
LocationManager lm;
StringBuilder sb;
int noOfFixes = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /* get TextView to display the GPS data */
    txtInfo = (TextView) findViewById(R.id.textView1);
    TextView myAddress = (TextView)findViewById(R.id.myaddress);

    /* the location manager is the most vital part it allows access
     * to location and GPS status services */
    lm = (LocationManager) getSystemService(LOCATION_SERVICE);

       Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);

       try {
  List<Address> addresses = geocoder.getFromLocation(location.getLatitude, location.getLongitude, 1);

  if(addresses != null) {
   Address returnedAddress = addresses.get(0);
   StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
   for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
    strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
   }
   myAddress.setText(strReturnedAddress.toString());
  }
  else{
   myAddress.setText("No Address returned!");
  }
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  myAddress.setText("Cannot get Address!");
 }

}




@Override
protected void onResume() {
    /*
     * onResume is is always called after onStart, even if the app hasn't been
     * paused
     *
     * add location listener and request updates every 1000ms or 10m
     */
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
    super.onResume();
}

@Override
protected void onPause() {
    /* GPS, as it turns out, consumes battery like crazy */
    lm.removeUpdates(this);
    super.onPause();
}

@Override
public void onLocationChanged(Location location) {
    Log.v(tag, "Location Changed");

    sb = new StringBuilder(512);

    noOfFixes++;

    /* display some of the data in the TextView */

    sb.append("No. of Fixes: ");
    sb.append(noOfFixes);
    sb.append('\n');
    sb.append('\n');

    sb.append("Longitude: ");
    sb.append(location.getLongitude());
    sb.append('\n');

    sb.append("Latitude: ");
    sb.append(location.getLatitude());
    sb.append('\n');

    sb.append("Accuracy: ");
    sb.append(location.getAccuracy());
    sb.append('\n');


    txtInfo.setText(sb.toString());
}

@Override
public void onProviderDisabled(String provider) {
    /* this is called if/when the GPS is disabled in settings */
    Log.v(tag, "Disabled");

    /* bring up the GPS settings */
    Intent intent = new Intent(
            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(intent);
}

@Override
public void onProviderEnabled(String provider) {
    Log.v(tag, "Enabled");
    Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    /* This is called when the GPS status alters */
    switch (status) {
    case LocationProvider.OUT_OF_SERVICE:
        Log.v(tag, "Status Changed: Out of Service");
        Toast.makeText(this, "Status Changed: Out of Service",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.TEMPORARILY_UNAVAILABLE:
        Log.v(tag, "Status Changed: Temporarily Unavailable");
        Toast.makeText(this, "Status Changed: Temporarily Unavailable",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.AVAILABLE:
        Log.v(tag, "Status Changed: Available");
        Toast.makeText(this, "Status Changed: Available",
                Toast.LENGTH_SHORT).show();
        break;
    }
}

@Override
protected void onStop() {
    /* may as well just finish since saving the state is not important for this toy app */
    finish();
    super.onStop();
}
  }

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gps.attempt"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".GpsAttemptActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />

I would be grateful if someone could help me with this

  • 1
    longitude cannot be resolved to a variable, latitude cannot be resolved to a variable, location cannot be resolved to a variable – user1234167 Mar 09 '12 at 11:51

1 Answers1

1

Have a look at my answer here It's a similar question but using a different sensor.

You are making the (understandable) mistake of assuming that you can just get your location instantly on the phone. You can't unfortunately. You basically have a to start up the service and then wait for it to come back to you when it has a location fix.

If you want an instant location you can see if the call to getLastKnowLocation() is non-null (but it may be inaccurate) and use that. Otherwise you need to put your Geocoding logic in the onLocationChanged() method and wait for it to be called.

Community
  • 1
  • 1
Tim
  • 5,767
  • 9
  • 45
  • 60
  • Thank you i put it in location changed and the errors are gone. However, now that it is running on the emulator the cannot get address is coming up (and i have inputted co-ordinates) – user1234167 Mar 09 '12 at 12:38
  • so you've used the emulator control in Eclipse to send a location to the emulator? Have you tried manually using the geocoder.. say using the coordinates of Wall street or something? Then put in the coordinates of the location you are currently in and see if it also works – Tim Mar 09 '12 at 14:08
  • ive tried with puttin the coordinates of the location im currently in and its coming up no address found. Done that using the emulator control. Any ideas? Im goin to try it on my phone later – user1234167 Mar 10 '12 at 16:36
  • try putting in a latitude and longitude of a well known location (for example 38.89874, -77.03652 is the lat/long for the White House so it should give you back the address of 1600 Pennsylvania Ave). If that doesn't work there is something more serious wrong (network connection or something?). Are you in a location which has google maps street data? – Tim Mar 10 '12 at 17:59
  • ive not used google maps as an API didnt think i needed to? I'll give a well known location a shot as well although i dont need the address just the city. Take it I would have to change the code for that as well? – user1234167 Mar 10 '12 at 18:49
  • this worked for me `Geocoder gc = new Geocoder(getApplicationContext()); try { List
    l = gc.getFromLocation( 38.89874, -77.03652, 1); Log.i(LOG_TAG, ""+ l.get(0).toString()); } catch (IOException e) { e.printStackTrace(); }` Sorry about the formatting but these are the comments.
    – Tim Mar 10 '12 at 22:08