使用android中的地理编码器从纬度经度中查找城市名称和国家/地区名称

时间:2012-05-15 06:54:36

标签: android

我从当前位置获得经度和经度 使用地理编码器从纬度经度显示城市国家和州名称 在这里我把我的logacat有错误

  

Log.error

05-15 12:13:40.584: E/Running(309): Mehod getBestProvider
05-15 12:13:40.674: E/Running(309): Mehod getBestProvider
05-15 12:13:57.083: E/Running(309): C Latitude27.422006
05-15 12:13:57.083: E/Running(309): C Longitude22.084095
05-15 12:13:57.093: E/Running(309): try called
05-15 12:13:57.093: E/Running(309): Exception called
05-15 12:13:57.093: W/System.err(309): java.io.IOException: Service not Available
05-15 12:13:57.113: W/System.err(309):at
     ndroid.location.Geocoder.getFromLocation(Geocoder.java:117)
05-15 12:13:57.113: W/System.err(309):  at 
  

Activityclass

 public class CitycountrymapActivity extends Activity implements LocationListener {
 private LocationManager locationManager;
 //private GeoPoint currentPoint;
 private Location currentLocation;
 double currentlatitude;
 double currentlongitude;
 String provider;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getLocation();

}


 public void getLocation()
    {
        provider=getBestProvider();

        currentLocation=locationManager.getLastKnownLocation(provider);

        if(currentLocation!=null)
        {
            setCurrentLocation(currentLocation);

        }
        else
        {
            Toast.makeText(this, "Location not yet acquired", Toast.LENGTH_LONG).show();
        }

    }


    public String getBestProvider()
    {
        locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
        criteria.setAccuracy(Criteria.NO_REQUIREMENT);
        String bestprovider=locationManager.getBestProvider(criteria, true);
        Log.e("Running","Mehod getBestProvider");
        return bestprovider;
    }

    public void setCurrentLocation(Location location)
    {


        //currentlatitude=(double)(location.getLatitude());
        //currentlongitude=(double)(location.getLongitude());

        currentlatitude =27.422006;
        currentlongitude=22.084095;

        currentLocation = new Location("");

        //currentPoint = new GeoPoint((int)(72.454565* 1E6),(int)(23.4572448 * 1E6)); 
        //currentPoint  = new GeoPoint((int)(72.454565),(int)(23.4572448));

         Global.clat=currentlatitude;
         Global.clon=currentlongitude;

        Log.e("Running","C Latitude"+Global.clat);
        Log.e("Running","C Longitude"+Global.clon);

        try
        {


        Log.e("Running","try called");  

        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        List<Address> addresses=geocoder.getFromLocation(currentlatitude, currentlongitude, 1);  

        Log.e("Running","geocoder set");
        Log.e("Running","addresses");

        String address = addresses.get(0).getAddressLine(0); 
        String city = addresses.get(0).getAddressLine(1); 
        String country = addresses.get(0).getAddressLine(2);

        Log.e("Running","Latitude"+currentlatitude);
        Log.e("Running","Longitude"+currentlongitude);
        Log.e("Running","Address"+address);
        Log.e("Running","City-->"+city);
        Log.e("Running","Country->"+country);

        }
        catch(Exception e)
        {
            Log.e("Running","Exception called");
            e.printStackTrace();
        }
    }

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        Toast.makeText(this, "Loction Changed", Toast.LENGTH_SHORT).show();
        setCurrentLocation(location);
    }

    protected void onResume()
    {
        super.onResume();
        locationManager.requestLocationUpdates(getBestProvider(), 1000, 1, this);
    }

    protected void onPause()
    {
        super.onPause();
        locationManager.removeUpdates(this);

    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Provider Disabled", Toast.LENGTH_SHORT).show();
    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Provider Enabled", Toast.LENGTH_SHORT).show();
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Staus Changed", Toast.LENGTH_SHORT).show();
    }

   }

2 个答案:

答案 0 :(得分:1)

请参阅@ddewaele发布的this answer。它将为您解决问题,也许解决方法也可以帮助您启动和运行应用程序。

答案 1 :(得分:0)

        locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0,
                    this);
            curLocation = locMan
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            Log.v("Gps", "on");
        } else if (locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                    100, 0, this);
            curLocation = locMan
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            Log.v("network", "on");

        } else {
            Log.v("setting", "on");
            final Intent poke = new Intent();
            poke.setClassName("com.android.settings",
                    "com.android.settings.widget.SettingsAppWidgetProvider");
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
            poke.setData(Uri.parse("3"));
            sendBroadcast(poke);

            locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0,
                    this);
            curLocation = locMan
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }

        if (curLocation != null) {
            clat = curLocation.getLatitude();
            clng = curLocation.getLongitude();
            Log.v("TAG  ", clat + "  " + clng);
        //  GeoPoint srcgeopoint = new GeoPoint((int) (clat * 1E6),
        //          (int) (clng * 1E6));


        } else {
            Toast.makeText(new_map.this, "No Location Find", Toast.LENGTH_SHORT);
        }


  //  srcLat = geoPoint.getLatitudeE6() / 1E6;
   //       srcLong = geoPoint.getLongitudeE6() / 1E6;

            Geocoder gCoder = new Geocoder(this);
            List<Address> addresses;
            try {
                addresses = gCoder.getFromLocation(clat, clng, 5);
                Log.e("get adderess", addresses + "");
                if (addresses != null && addresses.size() > 0) {
                    addres = addresses.get(0).getLocality();
                    System.out.print(addresses.get(0).getLocality());
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

在城市名称集中添加代码并获取一个......