Android 获取地理位置获取经纬度

下载jar https://pan.baidu.com/s/1ioZ5_24WvsmMYxIcgoLyvA

复制到libs下

附代码

static BDLocation lastLocation = null;
private LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner();
    public class MyLocationListenner implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null) {
                return;
            }
            Log.d("map", "On location change received:" + location);
            Log.d("map", "addr:" + location.getAddrStr());

            if (lastLocation != null) {
                if (lastLocation.getLatitude() == location.getLatitude() && lastLocation.getLongitude() == location.getLongitude()) {
                    Log.d("map", "same location, skip refresh");
                    // mMapView.refresh(); //need this refresh?
                    return;
                }
            }
            String addrlg; //定位结果
            lastLocation = location;
            if (!TextUtils.isEmpty(lastLocation.getLocationDescribe())){
                addrlg = lastLocation.getLocationDescribe();
            }else if (lastLocation.hasAddr()) {
                addrlg = lastLocation.getAddrStr();
            }else {
                addrlg = "定位失败...";
                return;
            }
            String city = lastLocation.getCity();
            double lat = lastLocation.getLatitude();
            double lot = lastLocation.getLongitude();
            ShareUtil.sharedPstring("nowla",String.valueOf(lat));
            ShareUtil.sharedPstring("nowlo",String.valueOf(lot));
            Log.i("lgq","............"+addrlg+"........"+lat+"......."+lot);
//            tv_bottom_bar_me.setText(addrlg);
//            mBaiduMap.animateMapStatus(u);
        }
    }
private void showMapWithLocationClient() {

    mLocClient = new LocationClient(this);
    mLocClient.registerLocationListener(myListener);

    LocationClientOption option = new LocationClientOption();
    option.setOpenGps(true);// open gps
    // option.setCoorType("bd09ll");
    // Johnson change to use gcj02 coordination. chinese national standard
    // so need to conver to bd09 everytime when draw on baidu map
    option.setCoorType("gcj02");
    option.setScanSpan(30000);
    option.setAddrType("all");
    mLocClient.setLocOption(option);

    mLocClient.start();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showMapWithLocationClient();

}



谷歌API也可以实现啦:https://blog.csdn.net/meixi_android/article/details/84955589

你可能感兴趣的:(移动开发)