如何获取我的当前位置的城市名称?

27

我正在使用Android Studio,希望在弹出对话框中让用户获取他们的位置,但我所知道的只是获取我的纬度和经度。

这是代码

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener {
    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager locationManager;
    private String provider;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        latituteField = (TextView) findViewById(R.id.TextView02);
        longitudeField = (TextView) findViewById(R.id.TextView04);


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

        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);


        if (location != null) {
            System.out.println("Provider " + provider + " has been selected.");
            onLocationChanged(location);
        } else {
            latituteField.setText("Location not available");
            longitudeField.setText("Location not available");
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
    }


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

    @Override
    public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {


    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();
    }
} 

在MainActivity中。你能帮我吗?

我已经在清单文件中添加了这个。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

但它仍然显示“位置不可用”。



你所说的“name”是指地址吗? - gsb
是的,我指的是城市:例如佛罗伦萨。 - Rick
3个回答

34

您需要使用GeoCoder类来从给定的经/纬度获取地址。请尝试以下方法:

Geocoder geoCoder = new Geocoder(this, Locale.getDefault()); //it is Geocoder
StringBuilder builder = new StringBuilder();
try {
    List<Address> address = geoCoder.getFromLocation(latitude, longitude, 1);
    int maxLines = address.get(0).getMaxAddressLineIndex();
    for (int i=0; i<maxLines; i++) {
    String addressStr = address.get(0).getAddressLine(i);
    builder.append(addressStr);
    builder.append(" ");
    }

String fnialAddress = builder.toString(); //This is the complete address.
} catch (IOException e) {}
  catch (NullPointerException e) {}

以下代码应该适用于您:(请检查关于您代码的内联注释)

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private TextView addressField; //Add a new TextView to your activity_main to display the address
private LocationManager locationManager;
private String provider;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    latituteField = (TextView) findViewById(R.id.TextView02);
    longitudeField = (TextView) findViewById(R.id.TextView04);
    addressField = (TextView) findViewById(R.id.TextView05); //Make sure you add this to activity_main


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

    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);


    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        onLocationChanged(location);
    } else {
        latituteField.setText("Location not available");
        longitudeField.setText("Location not available");
    }
}


@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider, 400, 1, this);
}


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

@Override
public void onLocationChanged(Location location) {
    //You had this as int. It is advised to have Lat/Loing as double.
    double lat = location.getLatitude();
    double lng = location.getLongitude();

    Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
    StringBuilder builder = new StringBuilder();
    try {
        List<Address> address = geoCoder.getFromLocation(lat, lng, 1);
        int maxLines = address.get(0).getMaxAddressLineIndex();
        for (int i=0; i<maxLines; i++) {
            String addressStr = address.get(0).getAddressLine(i);
            builder.append(addressStr);
            builder.append(" ");
        }

        String fnialAddress = builder.toString(); //This is the complete address.

        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
        addressField.setText(fnialAddress); //This will display the final address.

    } catch (IOException e) {
        // Handle IOException
    } catch (NullPointerException e) {
        // Handle NullPointerException
    }
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {


}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
}
}

我不知道为什么,但它不起作用!当我在手机上启动应用程序时,它会显示“很抱歉,我的应用程序已停止”。你的情况正常吗? - Rick
我已经解决了,它可以工作,但在TextView02、TextView04和TextView05中显示“位置不可用”。 - Rick
你是在设备上使用还是模拟器上? - gsb
我正在尝试在我的手机上。 - Rick
System.err: java.io.IOException: 服务不可用 - Iman Marashi

15

您需要在 AsyncTask 中(或者在与UI线程不同的线程组中的线程中)执行地理编码器!


public void getCityName(final Location location, final OnGeocoderFinishedListener listener) {
   new AsyncTask<Void, Integer, List<Address>>() {
      @Override
      protected List<Address> doInBackground(Void... arg0) {
         Geocoder coder = new Geocoder(getContext(), Locale.ENGLISH);
         List<Address> results = null;
         try {
            results = coder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
         } catch (IOException e) {
            // nothing
         }
         return results;
      }
        
      @Override
      protected void onPostExecute(List<Address> results) {
         if (results != null && listener != null) {
            listener.onFinished(results);
         }
      }
   }.execute();
}

通过这个抽象 Listener

public abstract class OnGeocoderFinishedListener {
   public abstract void onFinished(List<Address> results);
}

现在可以这样调用该方法:

getCityName(location, new OnGeocoderFinishedListener() {
   @Override
   public void onFinished(List<Address> results) {
      // do something with the result
   }
});

希望这可以帮助到你们中的一些人!


这是一份获取Address对象中城市名称的方法列表。通常getLocality可以使用,但有时它会返回null,那么您需要使用其他方法,如getSubAdminAreagetSubLocality等。 - Donald Duck

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接