接着上篇博客,如果没有看,点击http://blog.csdn.net/u012115730/article/details/78738159进行查看。这篇博客我们介绍下定位功能即地理位置的获取。废话不多说,直接撸起。
第一步:新建xml文件。这个xml只有一个按钮和一个listView。我这里不要加载百度地图,只获取地理位置。
<Button android:id="@+id/bt_dignshi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="定时获得经纬度" android:layout_gravity="center" /> <ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ListView>第二步:建立位置监听
// 注册位置监听器 locationClient.registerLocationListener(new BDLocationListener() { @Override public void onReceiveLocation(BDLocation location) { // TODO Auto-generated method stub if (location == null) { return; } StringBuffer sb = new StringBuffer(256); sb.append("时间:"); sb.append(location.getTime()); sb.append("纬度 : "); sb.append(location.getLatitude()); sb.append("经度 : "); sb.append(location.getLongitude()); sb.append("地址:"); sb.append(location.getAddrStr()); Log.e("经纬度",sb.toString()); Map<String, String> map1 = new HashMap<String, String>(); map1.put("经纬度",sb.toString()); data.add(map1); setListAdapter(new SimpleAdapter(DSJWDActivity.this,data,android.R.layout.simple_list_item_1, new String[]{"经纬度"}, //每行显示一个姓名 new int[]{android.R.id.text1})); //名字在text1上显示)); } }); locationClient.start(); } });第三步:建立定位条件:
// 设置定位条件 LocationClientOption option = new LocationClientOption(); option.setIsNeedAddress(true);// 中文地址 option.setOpenGps(true); // 是否打开GPS option.setCoorType("bd09ll"); // 设置返回值的坐标类型。 option.setPriority(LocationClientOption.NetWorkFirst); // 设置定位优先级 // option.setProdName("LocationDemo"); // option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);// 设置定位模式 option.setScanSpan(10000*5);//检查周期 小于1秒的按1秒 // 设置产品线名称。强烈建议您使用自定义的产品线名称,方便我们以后为您提供更高效准确的定位服务。 // option.setScanSpan(UPDATE_TIME);// 设置定时定位的时间间隔。单位毫秒 locationClient.setLocOption(option);
三步走完我们就可以出现我们想要的效果: