百度定位
百度定位DEMO
Location.java类
package com.example.baidudemo; import android.app.Application; import android.os.Process; import android.os.Vibrator; import android.util.Log; import android.widget.TextView; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.BDNotifyListener; import com.baidu.location.GeofenceClient; import com.baidu.location.LocationClient; public class Location extends Application { public LocationClient mLocationClient = null; public GeofenceClient mGeofenceClient; private String mData; public MyLocationListenner myListener = new MyLocationListenner(); public TextView mTv; public NotifyLister mNotifyer=null; public Vibrator mVibrator01; public static String TAG = "LocTestDemo"; @Override public void onCreate() { mLocationClient = new LocationClient( this ); /**—————————————————————————————————————————————————————————————————— * 这里的AK和应用签名包名绑定,如果使用在自己的工程中需要替换为自己申请的Key * —————————————————————————————————————————————————————————————————— */ mLocationClient.setAK("0Fa49070e3d9a18fb1df084293c5a335"); mLocationClient.registerLocationListener( myListener ); mGeofenceClient = new GeofenceClient(this); //位置提醒相关代码 // mNotifyer = new NotifyLister(); // mNotifyer.SetNotifyLocation(40.047883,116.312564,3000,"gps");//4个参数代表要位置提醒的点的坐标,具体含义依次为:纬度,经度,距离范围,坐标系类型(gcj02,gps,bd09,bd09ll) // mLocationClient.registerNotify(mNotifyer); super.onCreate(); Log.d(TAG, "... Application onCreate... pid=" + Process.myPid()); } /** * 显示请求字符串 * @param str */ public void logMsg(String str) { try { mData = str; if ( mTv != null ) mTv.setText(mData); } catch (Exception e) { e.printStackTrace(); } } /** * 监听函数,有更新位置的时候,格式化成字符串,输出到屏幕中 */ public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { if (location == null) return ; StringBuffer sb = new StringBuffer(256); sb.append("time : "); sb.append(location.getTime()); sb.append("\nerror code : "); sb.append(location.getLocType()); 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()); // } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){ /** * 格式化显示地址信息 */ sb.append("\n省:"); sb.append(location.getProvince()); sb.append("\n市:"); sb.append(location.getCity()); sb.append("\n区/县:"); sb.append(location.getDistrict()); sb.append("\naddr : "); sb.append(location.getAddrStr()); // } sb.append("\nsdk version : "); sb.append(mLocationClient.getVersion()); sb.append("\nisCellChangeFlag : "); sb.append(location.isCellChangeFlag()); logMsg(sb.toString()); Log.i(TAG, sb.toString()); } public void onReceivePoi(BDLocation poiLocation) { if (poiLocation == null){ return ; } StringBuffer sb = new StringBuffer(256); sb.append("Poi time : "); sb.append(poiLocation.getTime()); sb.append("\nerror code : "); sb.append(poiLocation.getLocType()); sb.append("\nlatitude : "); sb.append(poiLocation.getLatitude()); sb.append("\nlontitude : "); sb.append(poiLocation.getLongitude()); sb.append("\nradius : "); sb.append(poiLocation.getRadius()); if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){ sb.append("\naddr : "); sb.append(poiLocation.getAddrStr()); } if(poiLocation.hasPoi()){ sb.append("\nPoi:"); sb.append(poiLocation.getPoi()); }else{ sb.append("noPoi information"); } logMsg(sb.toString()); } } public class NotifyLister extends BDNotifyListener{ public void onNotify(BDLocation mlocation, float distance){ mVibrator01.vibrate(1000); } } }
MainActivity 测试类
package com.example.baidudemo; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { TextView textView1; private LocationClient mLocClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLocClient = ((Location)getApplication()).mLocationClient; textView1 = (TextView)findViewById(R.id.textView2); ((Location)getApplication()).mTv = textView1; setLocationOption(); mLocClient.start(); mLocClient.requestLocation(); } // ������ز��� private void setLocationOption() { LocationClientOption option = new LocationClientOption(); // option.setOpenGps(true); //gps option.setCoorType("bd009ll"); option.setServiceName("com.baidu.location.service_v2.9");//设置使用的service, 注意 该service需要在AndroidMenifest.xml中配置 option.setPoiExtraInfo(true); option.setAddrType("all");//设置获取的地址类型 option.setScanSpan(3000);//设置扫描间隔 option.setPriority(LocationClientOption.NetWorkFirst); // 设置定位优先级;网络定位优先 option.setPoiNumber(10); option.disableCache(true); mLocClient.setLocOption(option); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
AndroidManifests.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.baidudemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:name="com.example.baidudemo.Location" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.baidudemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
<!-- 这个service的定义很重要 --> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" > <intent-filter> <action android:name="com.baidu.location.service_v2.2" > </action> </intent-filter> </service> </application> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > </uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" > </uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE" > </uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > </uses-permission> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" > </uses-permission> <uses-permission android:name="android.permission.READ_LOGS" > </uses-permission> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> </manifest>
在做上面的工作之前需要导入百度定位的jar和相应的so文件。
具体可以参考http://i.cnblogs.com/Files.aspx里面的android_baidudingweiDEMO.rar