百度定位

来源:互联网 发布:python form import 编辑:程序博客网 时间:2024/04/27 20:00
package com.baidu.baidulocationdemo;import android.content.Context;import android.util.Log;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;/** * 获取当前的地址信息: 调用步骤:回调的方式,实现Locationable接口 */public class LocationUtility {private Context ct;public Locationable locationable;private LocationClient mLocationClient;public MyLocationListener mMyLocationListener;public LocationUtility(Context ct, Locationable locationable) {this.ct = ct;this.locationable = locationable;}// 设置位置public void setLocalMessage() {mLocationClient = new LocationClient(ct);mMyLocationListener = new MyLocationListener();mLocationClient.registerLocationListener(mMyLocationListener);initLocation();// 设置定位参数mLocationClient.start();// 开始}private void initLocation() {LocationClientOption option = new LocationClientOption();option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式option.setCoorType("gcj02");// 返回的定位结果是百度经纬度,默认值gcj02------>bd09lloption.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000msoption.setIsNeedAddress(true);// 返回的定位结果包含地址信息option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向mLocationClient.setLocOption(option);}/** * 实现实位回调监听 */public class MyLocationListener implements BDLocationListener {@Overridepublic void onReceiveLocation(BDLocation location) {// 结果信息,结果的信息都包含在BDLocation类中//http://developer.baidu.com/map/loc_refer/index.htmlStringBuffer sb = new StringBuffer(256);sb.append("time : ");sb.append(location.getTime());//时间sb.append("\nlatitude : ");sb.append(location.getLatitude());//纬度sb.append("\nlontitude : ");sb.append(location.getLongitude());//经度sb.append("\nradius : ");sb.append(location.getRadius());//定位精度if (location.getLocType() == BDLocation.TypeGpsLocation) {sb.append("\nspeed : ");sb.append(location.getSpeed());sb.append("\nsatellite : ");sb.append(location.getSatelliteNumber());sb.append("\ndirection : ");sb.append("\naddr : ");sb.append(location.getAddrStr());sb.append(location.getDirection());} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {sb.append("\naddr : ");sb.append(location.getAddrStr());}locationable.setLocationMessage(sb.toString());Log.i("BaiduLocationApiDem", sb.toString());// 停止客户端访问mLocationClient.stop();}}//回调函数public interface Locationable {void setLocationMessage(String str); }}
调用:<pre name="code" class="html">public class LocationActivity extends Activity implements Locationable {private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.location);tv = (TextView) findViewById(R.id.textView1);LocationUtility locationUtility = new LocationUtility(getApplicationContext(), this);locationUtility.setLocalMessage();}@Overridepublic void setLocationMessage(String str) {tv.setText(str);}}


0 0