【问题标题】:Getting Current Address using GPS使用 GPS 获取当前地址
【发布时间】:2014-06-26 14:02:10
【问题描述】:

我想使用 GPS 提供程序获取当前地址。我在使用 GPS 时获得零纬度 lng 值。我想要当前位置而不使用 Wifi。当我使用 GeoCoder 转换为 alt lng 值以寻址时出现异常( “无法解析来自服务器的响应”)我无法做到这一点,请帮助我

我的代码

public class MainActivity extends Activity implements LocationListener, OnClickListener{

LocationManager locationManager ;
String provider;
Button btn;
private String strAdd;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button)findViewById(R.id.button1);

    btn.setOnClickListener(this);
    // Getting LocationManager object
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    // Creating an empty criteria object
    Criteria criteria = new Criteria();

    // Getting the name of the provider that meets the criteria
    provider = locationManager.getBestProvider(criteria, false);

    if(provider!=null && !provider.equals("")){

        // Get the location from the given provider
        Location location = locationManager.getLastKnownLocation(provider);

        locationManager.requestLocationUpdates(provider, 10, 30, this);

        if(location!=null)
            onLocationChanged(location);
        else
            Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();

    }else{
        Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
    }
}



@Override
public void onLocationChanged(Location location) {
    // Getting reference to TextView tv_longitude
    TextView tvLongitude = (TextView)findViewById(R.id.textView1);
    TextView tvAdd  = (TextView)findViewById(R.id.textView2);

    // Getting reference to TextView tv_latitude
    //TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude);

    // Setting Current Longitude
    double lat = location.getLatitude();
    double lng = location.getLongitude();

    Log.e("LATLNG", ""+lat+lng);
   Geocoder geoCoder = new Geocoder(MainActivity.this, Locale.getDefault());
    try{
         List<Address> addresses = geoCoder.getFromLocation(lat,lng, 1);
          if (addresses != null) {
              Address returnedAddress = addresses.get(0);
              StringBuilder strReturnedAddress = new StringBuilder("");

              for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                  strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
              }
                strAdd = strReturnedAddress.toString();

          }
    }
          catch (Exception e) {
            // TODO: handle exception
           e.printStackTrace();
          }
    tvLongitude.setText("LAT LNG:" + lat+lng);

    tvAdd.setText("ADDRESS:" + strAdd);

    // Setting Current Latitude
   // tvLatitude.setText("Latitude:" + location.getLatitude() );
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
}

权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

【问题讨论】:

  • 你在哪里测试你的代码?在模拟器中还是在真实设备中?
  • 真机测试

标签: android gps


【解决方案1】:

如果您在 lat long 中获得 0 值,我认为您无法收到 GPS 响应。 GPS 可能需要很长时间才能获得第一次定位,有时长达 15 分钟,具体取决于卫星的能见度。因此,当您只使用 GPS 时,请确保您在室外位置以获得更快的定位。

此外,您还需要包含 &lt;uses-permission android:name="android.permission.INTERNET" /&gt;,因为 Geocoder 需要将 lat lng 发送到在线服务器以获取地址。

【讨论】:

  • 不上网可以获取地址吗?
  • 如果您有整个 lat long 数据库来解决您的 android 设备中的转换问题,您可能会这样做。但这是不可能的。
  • @ShivamVerma stackoverflow.com/questions/24408421/… 如果可以的话,请帮助我。
  • @nobalG 对不起,你想说什么?
  • @Durga 如果它适合你,请接受答案:-)
【解决方案2】:

错误:

即使最后一个已知位置包含 (0,0) 作为 lat-long,它也会简单地调用 onLocationChanged(location)

    if(location!=null)
        onLocationChanged(location);
    else
        Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();

正确:

如下所述添加验证,以便仅在 either it has valid location or when it will receive new location using LocationManager 时调用 onLocationChanged(location)

    if(location!=null){
        if(location.location.getLatitude()>0 || location.getLongitude()>0) 
        {
           onLocationChanged(location);
        }
    }else{
        Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show(); 
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多