android 定位城市

首先你得有Button按钮的点击事件

public void dingwei(View v){
     MyLocationListenner myListener = new MyLocationListenner();
  locationClient = new LocationClient(MainActivity.this);
  locationClient.registerLocationListener(myListener);
  LocationClientOption option = new LocationClientOption();
  option.setOpenGps(true);
  option.setAddrType("all");
  option.setCoorType("bd09ll");
  option.setScanSpan(5000);
  option.disableCache(true);
  option.setPoiNumber(5);
  option.setPoiDistance(1000);
  option.setPoiExtraInfo(true);
  option.setPriority(LocationClientOption.GpsFirst);
  locationClient.setLocOption(option);
  locationClient.start();
    }

   private class MyLocationListenner implements BDLocationListener {
  @Override
  public void onReceiveLocation(BDLocation location) {

   if (location == null)
    return;
   StringBuffer sb = new StringBuffer(256);
   if (location.getLocType() == BDLocation.TypeGpsLocation) {
    // sb.append(location.getAddrStr());
    sb.append(location.getCity());
   } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
    sb.append(location.getCity());
   }
   if (sb.toString() != null && sb.toString().length() > 0) {
    lngCityName = sb.toString();
    tv_cityName.setText(lngCityName);
   }

  }

  public void onReceivePoi(BDLocation poiLocation) {

  }
 }
参数 private String lngCityName = "";// 存放返回的城市名

实现这两个方法你就可以定位出你的城市了

记得加相应的jar包还有相应的权限

要在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); } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值