使用百度地图API实现定位的效果。
扫描二维码
随时随地手机看文章
先获取百度地图API的Key,http://developer.baidu.com/map/index.php?title=android-locsdk申请key的地址。
因为在项目用到定位功能,顺便记录下来。
不多说了。
这一段做的是通过开机广播中的一个闹钟每隔一分钟启动一次Alarmrecevier服务;
package com.example.demo_location; import com.example.serlocation.service_location; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.SystemClock; import android.util.Log; public class bootBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { Intent mintent = new Intent(context, Alarmrecevier.class); mintent.setAction("com.example.demo_location.Alarm"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, mintent, 0); //在某一个时间,这里设置的当前时间。 long realtime = SystemClock.elapsedRealtime(); //定义一个闹钟计时器。目的是每隔一分钟启动一次Alarmrecevier; AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE); am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, realtime, 6000*10, pendingIntent); System.out.println("ertyuio"); } } }
package com.example.demo_location; import com.example.serlocation.service_location; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; //启动service_location,目的就是防止被系统干掉。 public class Alarmrecevier extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if ("com.example.demo_location.Alarm".equals(intent.getAction())) { Intent i = new Intent(context, service_location.class); context.startService(i); } } }
定位核心地方
package com.example.serlocation; import java.net.URLEncoder; import java.util.Date; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONObject; 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 com.example.demo_location.MainActivity; import android.app.Service; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Build; import android.os.IBinder; import android.telephony.TelephonyManager; import android.util.Log; public class service_location extends Service{ /**经度*/ private double longitude; /**纬度*/ private double latitude; /**详细地址*/ private String address; /**省*/ private String province; /**市*/ private String city; /**区*/ private String district; /**街道*/ private String street; /**手机版本*/ private String moblieModel; /**电话号码*/ private String phoneNumber; /**imsi*/ private String imsi; /**imei*/ private String imei; /**定位sdk版本*/ private int sdkVersion; /**系统版本*/ private String osVersion; /**当前时间*/ private String time; /**获取1970年距今的毫秒*/ private String currTime; private String theurl; private TelephonyManager mTelephonyManager; private MainActivity activity; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); initLocation(); mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); } //定位相关 public LocationClient mLocationClient = null; public BDLocationListener myListener = new BDLocationListener() { @Override public void onReceiveLocation(final BDLocation location) { new Thread(new Runnable() { @Override public void run() { if(location != null){ longitude = location.getLongitude(); latitude = location.getLatitude(); address = location.getAddrStr(); province = location.getProvince(); city = location.getCity(); district = location.getDistrict(); street = location.getStreet(); moblieModel = Build.MODEL; phoneNumber = mTelephonyManager.getLine1Number(); imsi = mTelephonyManager.getSubscriberId(); imei = mTelephonyManager.getDeviceId(); sdkVersion = Build.VERSION.SDK_INT; osVersion = Build.VERSION.RELEASE; Date curdate = new Date(); String currTime = String.valueOf(curdate.getTime()); StringBuffer sb = new StringBuffer(); sb.append("手机型号:"+moblieModel); sb.append("nIMEI:"+imei); sb.append("nIMSI:"+imsi); sb.append("n系统版本:"+osVersion); sb.append("nSDK版本:"+sdkVersion); sb.append("n手机号码:"+phoneNumber); sb.append("n精度:"+longitude); sb.append("n纬度:"+latitude); sb.append("n详细地址:"+address); sb.append("n省份:"+province); sb.append("n城市:"+city); sb.append("n区:"+district); sb.append("n"+street); sb.append("n时间:"+time); //如果需要TextView 设置Text的话就// tv.setText(sb.toString());不要忘了找ID. } }; /**初始化LocationClient类*/ private void initLocation() { //声明LocationClient类 mLocationClient = new LocationClient(getApplicationContext()); //注册监听函数 mLocationClient.registerLocationListener( myListener ); LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationMode.Hight_Accuracy);////设置定位模式 高精度 option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02 option.setScanSpan(60000); option.setIsNeedAddress(true);//返回的定位结果包含地址信息 mLocationClient.setLocOption(option); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub mLocationClient.start(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } }
最后还有一点 就是添加以下权限进Manifest.xml中去。
设置AndroidManifest.xml 在application标签中声明service组件,每个app拥有自己单独的定位service声明使用权限 设置AcessKey 使用SDK4.2需要在Mainfest.xml设置Accesskey,设置有误会引起定位和地理围栏服务不能正常使用,必须进行Accesskey的正确设置。 设置AccessKey,在application标签中加入//key:开发者申请的key