通过百度AK定位详细地址

 需要去百度开发者平台申请AK

  import com.baidu.location.BDLocation;
        import com.baidu.location.BDLocationListener;
        import com.baidu.location.LocationClient;
        import com.baidu.location.LocationClientOption;
        import com.baidu.location.LocationClientOption.LocationMode;


        import android.os.Bundle;
        import android.app.Activity;
        import android.view.Menu;
        import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView textView;
    private LocationClient locationClient = null;// 定位SDK的核心类
    private String tempcoor = "gcj02";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.showText);

        locationClient = new LocationClient(getApplicationContext());// 初始化定位对象
        locationClient.registerLocationListener(bdLocationListener);// 注册定位监听器

        // 定位前的准备工作
        initLocation();

        locationClient.start();// 开始定位
        locationClient.requestLocation();
    }
    @Override
    protected void onDestroy() {
// TODO Auto-generated method stub
        super.onDestroy();
        if (locationClient != null) {
            locationClient.unRegisterLocationListener(bdLocationListener);
            locationClient = null;
        }
    }
    public void initLocation() {

        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationMode.Hight_Accuracy);// 高精度定位模式
        option.setCoorType(tempcoor);// 国测局加密经纬度坐标
        option.setScanSpan(0);
        option.setIsNeedAddress(true);
        option.setOpenGps(true);
        option.setLocationNotify(true);// 可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIgnoreKillProcess(true);
        locationClient.setLocOption(option);

    }

    BDLocationListener bdLocationListener = new BDLocationListener() {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // TODO Auto-generated method stub
            if (location == null) {
                return;
            }
            textView.setText("address:" + location.getAddrStr());
        }
    };

}




亲测有效!

转载:

发布了19 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/xyzahaha/article/details/82782657