首先你得有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();
}
@Override
public void onReceiveLocation(BDLocation location) {
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);
}
}
实现这两个方法你就可以定位出你的城市了
记得加相应的jar包还有相应的权限