【问题标题】:gps app reverse geocoding errorgps应用程序反向地理编码错误
【发布时间】:2012-03-08 23:29:28
【问题描述】:

我正在尝试更改此代码以进行反向地理编码。坐标工作正常。我遇到了错误:位置无法解析为变量。这个错误在这行代码:

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

如果有人可以帮助我,我将不胜感激

【问题讨论】:

  • 经度无法解析为变量,纬度无法解析为变量,位置无法解析为变量

标签: android geocoding reverse-geocoding


【解决方案1】:

看看我的回答here 这是一个类似的问题,但使用了不同的传感器。

您犯了一个(可以理解的)错误,即假设您可以通过手机立即获取您的位置。不幸的是,你不能。您基本上需要启动服务,然后等待它在定位修复后返回给您。

如果您想要一个即时位置,您可以查看对getLastKnowLocation() 的调用是否为非空(但它可能不准确)并使用它。否则,您需要将您的地理编码逻辑放在 onLocationChanged() 方法中并等待它被调用。

【讨论】:

  • 谢谢你,我把它放在位置改变了,错误就消失了。但是,现在它正在模拟器上运行,无法获取地址即将出现(并且我已经输入了坐标)
  • 那么您已经使用 Eclipse 中的模拟器控件向模拟器发送位置了吗?您是否尝试过手动使用地理编码器..说使用华尔街的坐标或其他东西?然后输入你当前所在位置的坐标,看看是否也有效
  • 我尝试输入我当前所在位置的坐标,但未找到地址。使用模拟器控件完成此操作。有任何想法吗?我稍后会在我的手机上尝试一下
  • 尝试输入一个知名位置的纬度和经度(例如 38.89874,-77.03652 是白宫的纬度/经度,因此它应该返回 1600 Pennsylvania Ave 的地址)。如果这不起作用,则存在更严重的错误(网络连接或其他什么?)。您所在的位置有谷歌地图街道数据吗?
  • 我没有使用谷歌地图作为 API 不认为我需要吗?我也会给一个众所周知的位置一个镜头,虽然我不需要这个城市的地址。接受它,我也必须为此更改代码?
猜你喜欢
  • 1970-01-01
  • 2011-04-26
  • 2013-04-20
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
相关资源
最近更新 更多