注册百度开发者帐号,下载相关sdk
添加权限:
添加百度注册访问应用(AK)码
添加源代码文件到libs文件:
代码如下:
package com.lixu.baidu_gps; import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption; import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast; public class MainActivity extends Activity {
private LocationClient mLocationClient = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); MyLocationListenner myListenner = new MyLocationListenner(); mLocationClient = new LocationClient(this);
LocationClientOption option = new LocationClientOption(); option.setIsNeedAddress(true);
option.setAddrType("all");
mLocationClient.setLocOption(option);
mLocationClient.registerLocationListener(myListenner);
mLocationClient.start(); } private class MyLocationListenner implements BDLocationListener { @Override
public void onReceiveLocation(BDLocation location) {
// 获取省
String p = location.getProvince();
// 获取城市
String city = location.getCity();
// 获取道路名
String street = location.getStreet();
// 获取街道号
String n = location.getStreetNumber();
Toast.makeText(getApplicationContext(), "您现在的位置在:" + p + " " + city + " " + street + " " + n, 1).show();
} } // 程序退出时关闭
@Override
protected void onDestroy() {
super.onDestroy();
mLocationClient.stop();
} }
在真机上运行后 会弹出你所在的详细地址。。。