你的位置:首页 > 操作系统

[操作系统]利用百度地图开源sdk获取地址信息。



注册百度开发者帐号,下载相关sdk

添加权限:

添加百度注册访问应用(AK)码

添加源代码文件到libs文件:

代码如下:

 1 package com.lixu.baidu_gps; 2  3 import com.baidu.location.BDLocation; 4 import com.baidu.location.BDLocationListener; 5 import com.baidu.location.LocationClient; 6 import com.baidu.location.LocationClientOption; 7  8 import android.app.Activity; 9 import android.os.Bundle;10 import android.widget.Toast;11 12 public class MainActivity extends Activity {13   private LocationClient mLocationClient = null;14 15   @Override16   protected void onCreate(Bundle savedInstanceState) {17     super.onCreate(savedInstanceState);18     setContentView(R.layout.activity_main);19 20     MyLocationListenner myListenner = new MyLocationListenner();21 22     mLocationClient = new LocationClient(this);23     LocationClientOption option = new LocationClientOption();24 25     option.setIsNeedAddress(true);26     option.setAddrType("all");27     mLocationClient.setLocOption(option);28     mLocationClient.registerLocationListener(myListenner);29     mLocationClient.start();30 31   }32 33   private class MyLocationListenner implements BDLocationListener {34 35     @Override36     public void onReceiveLocation(BDLocation location) {37       // 获取省38       String p = location.getProvince();39       // 获取城市40       String city = location.getCity();41       // 获取道路名42       String street = location.getStreet();43       // 获取街道号44       String n = location.getStreetNumber();45       Toast.makeText(getApplicationContext(), "您现在的位置在:" + p + " " + city + " " + street + " " + n, 1).show();46     }47 48   }49 50   // 程序退出时关闭51   @Override52   protected void onDestroy() {53     super.onDestroy();54     mLocationClient.stop();55   }56 57 }

在真机上运行后  会弹出你所在的详细地址。。。