android定位并获取城市
时间: 2023-06-14 21:04:39 浏览: 66
要在Android设备上获取用户的当前位置并获取城市,可以使用Android系统提供的LocationManager类和Geocoder类。
首先,需要在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
```
然后,在代码中使用LocationManager类获取设备的当前位置:
```java
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
```
这里使用NETWORK_PROVIDER来获取网络位置,也可以使用GPS_PROVIDER来获取GPS位置。获取到位置后,就可以使用Geocoder类将位置转换成城市:
```java
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
String city = addresses.get(0).getLocality();
```
这里需要注意的是,getFromLocation方法需要在异步线程中执行,以避免阻塞UI线程。完整的代码示例如下:
```java
public class MainActivity extends AppCompatActivity {
private TextView cityTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cityTextView = findViewById(R.id.city_text_view);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
new GetCityTask().execute(location);
}
}
private class GetCityTask extends AsyncTask<Location, Void, String> {
@Override
protected String doInBackground(Location... locations) {
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(locations[0].getLatitude(), locations[0].getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses != null && addresses.size() > 0) {
return addresses.get(0).getLocality();
} else {
return null;
}
}
@Override
protected void onPostExecute(String city) {
if (city != null) {
cityTextView.setText(city);
}
}
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)