2012-03-08 82 views
0

我試圖更改此代碼以進行反向地理編碼。座標工作正常。我得到錯誤:位置不能解析爲變量。這個錯誤是在這行代碼:gps應用程序反向地理編碼錯誤

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

我完整的代碼:

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(); 
} 
    } 

清單:

<?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" /> 

我將不勝感激,如果有人可以幫助我與此

+0

什麼是錯誤? – Tim 2012-03-09 01:55:31

+1

經度不能解析爲變量,緯度無法解析爲變量,位置無法解析爲變量 – user1234167 2012-03-09 11:51:03

回答

1

Hav看看我的回答here 這是一個類似的問題,但使用不同的傳感器。

您正在作出(理解)錯誤,認爲您可以立即在手機上獲取您的位置。你不能遺憾。你基本上有一個啓動服務,然後等待它有一個位置修復時回來給你。

如果你想要一個即時的位置,你可以看到getLastKnowLocation()的調用是否爲非null(但可能不準確)並使用它。否則,您需要將您的地理編碼邏輯放在onLocationChanged()方法中並等待它被調用。

+0

謝謝我把它放在位置改變了,錯誤消失了。但是,現在它在仿真器上運行,無法獲取地址(並且我輸入了座標) – user1234167 2012-03-09 12:38:45

+0

因此您已經使用Eclipse中的仿真器控件向仿真器發送位置了?你有沒有嘗試手動使用地理編碼器..說使用華爾街的座標什麼的?然後把你現在的位置的座標,看看它是否也可以工作 – Tim 2012-03-09 14:08:58

+0

我試着用puttin當前位置的座標,並且沒有找到地址。完成使用模擬器控件。有任何想法嗎?即將登陸我的手機後試用 – user1234167 2012-03-10 16:36:08