使用GPS获取当前地址

时间:2014-06-26 05:08:22

标签: android gps

我想使用GPS提供商获取当前地址。我使用GPS时我的零点值已经为零。我想要当前位置而不使用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" /> 

2 个答案:

答案 0 :(得分:1)

如果你的lat值为0,我认为你无法收到GPS响应。根据卫星的可见度,GPS可能需要花费大量时间才能获得第一次修复,有时长达15分钟。因此,当您只使用GPS时,请确保您在室外位置以获得更快的修复。

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

答案 1 :(得分:0)

<强> WRONG:

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

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

<强>正确:

如下所述在其上添加验证,因此只有在onLocationChanged(location)时才会调用either it has valid location or when it will receive new location using LocationManager

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