1、AndroidManifext.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.njupt.bmap_mylocate" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /> <application android:name="com.njupt.bmap_mylocate.Location" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/loc_sdk" android:theme="@style/AppTheme" > <activity android:name="com.njupt.bmap_mylocate.mainActivity" android:configChanges="orientation|keyboardHidden" android:label="@string/loc_sdk" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <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>
2、activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".GeofenceActivity" > <Button android:id="@+id/add" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="添加围栏" /> <Button android:id="@+id/remove" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/add" android:text="删除围栏" /> </RelativeLayout>
3、main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/btn_favorite_tabheader" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@android:color/black" > <TextView android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@android:color/white"> </TextView> <Button android:id="@+id/StartBtn" android:layout_width="100dip" android:layout_height="50dip" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:text="开始" > </Button> <Button android:id="@+id/locBtn" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_alignBottom="@+id/StartBtn" android:layout_alignTop="@+id/StartBtn" android:layout_toRightOf="@+id/StartBtn" android:text="定位" > </Button> <Button android:id="@+id/setBtn" android:layout_width="100dip" android:layout_height="50dip" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="设置" > </Button> <EditText android:id="@+id/addrEdit" android:layout_width="100dip" android:layout_height="40dip" android:layout_above="@+id/setBtn" android:layout_alignLeft="@+id/setBtn" android:layout_alignRight="@+id/setBtn" android:text="detail" > </EditText> <EditText android:id="@+id/edit" android:layout_width="100dip" android:layout_height="40dip" android:layout_above="@+id/setBtn" android:layout_alignLeft="@+id/addrEdit" android:layout_alignRight="@+id/addrEdit" android:text="3000" > </EditText> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/edit" android:layout_alignTop="@+id/edit" android:layout_toLeftOf="@+id/edit" android:text="扫描时间间隔 :" android:textColor="@android:color/white"> </TextView> <EditText android:id="@+id/coorEdit" android:layout_width="100dip" android:layout_height="40dip" android:layout_above="@+id/edit" android:layout_alignLeft="@+id/edit" android:layout_alignRight="@+id/edit" android:text="bd09ll" > </EditText> <CheckBox android:id="@+id/gpsCheck" android:layout_width="wrap_content" android:layout_height="40dip" android:layout_above="@+id/coorEdit" android:layout_alignRight="@+id/coorEdit" android:text="打开GPS" android:textColor="@android:color/white" > </CheckBox> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/coorEdit" android:layout_alignTop="@+id/coorEdit" android:layout_toLeftOf="@+id/coorEdit" android:text="坐标系类型 :" android:textColor="@android:color/white"> </TextView> <Button android:id="@+id/PoiReq" android:layout_width="100dip" android:layout_height="50dip" android:layout_alignBaseline="@+id/textView2" android:layout_alignBottom="@+id/textView2" android:layout_alignParentLeft="true" android:text="PoiRequest" /> <CheckBox android:id="@+id/priorityCheck" android:layout_width="100dip" android:layout_height="50dip" android:layout_alignTop="@+id/gpsCheck" android:layout_toLeftOf="@+id/locBtn" android:text="网络优先级" android:textColor="@android:color/white"/> <Button android:id="@+id/offLineBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/gpsCheck" android:text="离线请求" /> <CheckBox android:id="@+id/isAddrInfocb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/offLineBtn" android:layout_marginBottom="14dp" android:layout_toRightOf="@+id/locBtn" android:text="是否要地址信息" android:textColor="@android:color/white"/> </RelativeLayout>
4、strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Geofence4.0</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="loc_sdk">定位SDK</string> <string name="geofence">地理围栏</string> </resources>
5、导定位SDK的jar包、armeabi目录到lib目录下
6、Location
package com.njupt.bmap_mylocate; import android.app.Application; import android.os.Process; import android.os.Vibrator; import android.util.Log; import android.widget.TextView; import android.widget.Toast; 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;//LocationClient,定位SDK的核心类 private String mData; public MyLocationListenner myListener = new MyLocationListenner(); public TextView mTv; public NotifyLister mNotifyer=null; public Vibrator mVibrator01; @Override public void onCreate() { mLocationClient = new LocationClient( this ); /**—————————————————————————————————————————————————————————————————— * 这里的AK和应用签名包名绑定,如果使用在自己的工程中需要替换为自己申请的Key * —————————————————————————————————————————————————————————————————— */ mLocationClient.setAK("1d6228b653bfe637080e8c6a56f953a9");//设置已申请AccessKey mLocationClient.registerLocationListener( myListener );//注册监听函数: //位置提醒相关代码 mNotifyer = new NotifyLister(); mNotifyer.SetNotifyLocation(32.128925,118.931046,3000,"gps");//用于设定位置提醒的点的具体信息,4个参数依次为:纬度,经度,半径,坐标类型.4个参数代表要位置提醒的点的坐标,具体含义依次为:纬度,经度,距离范围,坐标系类型(gcj02,gps,bd09,bd09ll) mLocationClient.registerNotify(mNotifyer);//注册位置提醒监听事件: super.onCreate(); } /** * 显示请求字符串 * @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 { /** * 封装了定位SDK的定位结果,在BDLocationListener的onReceive方法中获取。 * 通过该类用户可以获取error code,位置的坐标,精度半径等信息 * * onReceiveLocation():获取定位结果的接口。用户自己实现这个接口后,监听定位结果。 */ @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());//获取error code.161:网络定位结果 , 61:GPS定位结果 , 66:离线定位结果 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());//获取定位SDK的版本号 sb.append("\nisCellChangeFlag : "); sb.append(location.isCellChangeFlag()); logMsg(sb.toString()); System.out.println(sb.toString()); } /** * onReceivePoi(...):获取POI信息的接口。用户自己实现这个接口后,监听poi的相关结果。 */ 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()); } } /** * BDNotifyListener:位置提醒接口类,用于设定位置提醒点 * @author Administrator * */ public class NotifyLister extends BDNotifyListener{ /** * onNotify(...): 位置提醒监听事件具体实现 * mlocation表示当前位置, * distance是当前坐标中心点与设定位置提醒的坐标点之间的距离值。 */ public void onNotify(BDLocation mlocation, float distance){ mVibrator01.vibrate(1000); Toast.makeText(getApplicationContext(), "您好,您已经到达一个对你来说有意义的位置...", 1).show(); } } }
7、mainActivity
package com.njupt.bmap_mylocate; import java.util.regex.Pattern; import android.app.Activity; import android.app.Service; import android.content.Intent; import android.os.Bundle; import android.os.Process; import android.os.Vibrator; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; public class mainActivity extends Activity { private TextView mTv = null; private EditText mSpanEdit; private EditText mCoorEdit; private CheckBox mGpsCheck; private CheckBox mPriorityCheck; private Button mStartBtn; private Button mSetBtn; private Button mLocBtn; private Button mPoiBtn; private Button mOfflineBtn; private CheckBox mIsAddrInfoCheck; private boolean mIsStart; private static int count = 1; private Vibrator mVibrator01 =null; private LocationClient mLocClient; private Button geofenceBtn; public static String TAG = "LocTestDemo"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTv = (TextView)findViewById(R.id.textview); mSpanEdit = (EditText)findViewById(R.id.edit); mCoorEdit = (EditText)findViewById(R.id.coorEdit); mGpsCheck = (CheckBox)findViewById(R.id.gpsCheck); mPriorityCheck = (CheckBox)findViewById(R.id.priorityCheck); mStartBtn = (Button)findViewById(R.id.StartBtn); mLocBtn = (Button)findViewById(R.id.locBtn); mSetBtn = (Button)findViewById(R.id.setBtn); mPoiBtn = (Button)findViewById(R.id.PoiReq); mOfflineBtn = (Button)findViewById(R.id.offLineBtn); mIsAddrInfoCheck = (CheckBox)findViewById(R.id.isAddrInfocb); mIsStart = false; mLocClient = ((Location)getApplication()).mLocationClient; ((Location)getApplication()).mTv = mTv; mVibrator01 =(Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE); ((Location)getApplication()).mVibrator01 = mVibrator01; //开始/停止按钮 mStartBtn.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { if (!mIsStart) { setLocationOption(); mLocClient.start();//启动定位SDK mStartBtn.setText("停止"); mIsStart = true; } else { mLocClient.stop();//关闭定位SDK mIsStart = false; mStartBtn.setText("开始"); } Log.d(TAG, "... mStartBtn onClick... pid="+Process.myPid()+" count="+count++); } }); //定位按钮 mLocBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mLocClient != null && mLocClient.isStarted()){ /** * 0: 正常发起了定位。 1: 服务没有启动。 2: 没有监听函数。 6: 请求间隔过短。 * 前后两次请求定位时间间隔不能小于1000ms。 */ setLocationOption(); mLocClient.requestLocation();//发起定位,异步获取当前位置.定位结果在ReceiveListener的方法OnReceive方法的参数中返回。 } else Log.d(TAG, "locClient is null or not started"); Log.d(TAG, "... mlocBtn onClick... pid="+Process.myPid()+" count="+count++); Log.d(TAG,"version:"+mLocClient.getVersion()); } }); //设置按钮 mSetBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setLocationOption(); } }); mPoiBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mLocClient.requestPoi(); } }); //离线基站定位按钮 mOfflineBtn.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { /** * 0: 正常发起了定位。 * 1: 服务没有启动。 * 2: 没有监听函数。 */ int req = mLocClient.requestOfflineLocation();//发起离线定位,异步获取当前位置 Log.d("req","req:"+req); } }); } @Override public void onDestroy() { mLocClient.stop(); ((Location)getApplication()).mTv = null; super.onDestroy(); } //设置相关参数 private void setLocationOption(){ /** * setProdName ( String ): 设置产品线名称。强烈建议您使用自定义的产品线名称,方便我们以后为您提供更高效准确的定位服务。 * setPoiDistance( float ): 设置查询范围,默认值为500,即以当前定位位置为中心的半径大小。 */ LocationClientOption option = new LocationClientOption(); option.setOpenGps(mGpsCheck.isChecked()); //设置是否打开gps, option.setCoorType(mCoorEdit.getText().toString()); //设置坐标类型 option.setServiceName("com.baidu.location.service_v2.9"); option.setPoiExtraInfo(mIsAddrInfoCheck.isChecked()); //设置是否需要POI的电话地址等详细信息。默认值为false,即不返回POI的电话和地址信息。 if(mIsAddrInfoCheck.isChecked()) { option.setAddrType("all");//设置是否需要地址信息.String 值为 all时,表示返回地址信息。其他值都表示不返回地址信息。 } if(null!=mSpanEdit.getText().toString()) { boolean b = isNumeric(mSpanEdit.getText().toString()); if(b) { option.setScanSpan(Integer.parseInt(mSpanEdit.getText().toString())); //设置定位模式,小于1秒则一次定位;大于等于1秒则定时定位 } } // option.setScanSpan(3000); if(mPriorityCheck.isChecked()) { option.setPriority(LocationClientOption.NetWorkFirst); //设置网络优先 } else { option.setPriority(LocationClientOption.GpsFirst); //不设置,默认是gps优先 } option.setPoiNumber(10);//设置POI个数,默认值为3。由于POI查询比较耗费流量,设置最多返回的POI个数,以便节省流量。 option.disableCache(true);//true表示禁用缓存定位,false表示启用缓存定位。 mLocClient.setLocOption(option);//设置参数 } protected boolean isNumeric(String str) { Pattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); } }
----------------------------------------------------总结起来,其实就是以下三步-------------------------
1、拷AndroidManifest.xml、layout、menu、strings.xml
2、导jar包
3、拷源码
4、做一些必要的修改