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包还有相应的权限

你可能感兴趣的:(学习)