基于百度地图的定位实现天气预报查询
2015-12-13 12:10
453 查看
(1)申请秘钥KEY,并下载百度定位SDK
(2)开发环境的配置:
a.在application标签中声明service组件,每个app拥有自己单独的定位service。<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"> </service>
b.配置置秘钥,SDK4.2及之后版本需要在Mainfest.xml设置Accesskey,设置有误会引起定位和地理围栏服务不能正常使用,必须进行Accesskey的正确
<meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="key" /> //key:开发者申请的key
c.权限设置
<!-- 这个权限用于进行网络定位--> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> <!-- 这个权限用于访问GPS定位--> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位--> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <!-- 获取运营商信息,用于支持提供运营商信息相关的接口--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位--> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> <!-- 用于读取手机当前的状态--> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数 4000 据--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <!-- 访问网络,网络定位需要上网--> <uses-permission android:name="android.permission.INTERNET" /> <!-- SD卡读取权限,用户写入离线定位数据--> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
(3)实现定位功能
a.初始化DBLocationClient类public LocationClient mLocationClient = null; public BDLocationListener myListener = new MyLocationListener(); public void onCreate() { mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类 mLocationClient.registerLocationListener( myListener ); //注册监听函数 }
b.通过LocationClientOpinion设置定位参数
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);
option.setCoorType(“bd09ll”);//可选,默认gcj02,
int span=1000;
option.setScanSpan(span);
option.setIsNeedAddress(true);
option.setOpenGps(true);
option.setIsNeedLocationDescribe(true);
option.setIsNeedLocationPoiList(true);
option.setIgnoreKillProcess(false);
option.SetIgnoreCacheException(false);
option.setEnableSimulateGps(false);
mLocationClient.setLocOption(option);
c.ationListener接口,并实现里面的OnReceiveLocation方法
private void initListener() { myListener=new BDLocationListener() { @Override public void onReceiveLocation(BDLocation location) { String citys=location.getCity(); cityName=citys.substring(0,citys.length()-1); if(cityName!=null) { QueryAsyncTask asyncTask = new QueryAsyncTask(); asyncTask.execute(); } else { Toast.makeText(MainActivity.this, "定位不到当前城市,无法查询天气", Toast.LENGTH_SHORT).show(); } } }; }
(4)根据定位获得城市名进行天气查询
我用新浪的api接口:“http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=2&city=“+cityName+”&dfc=3”
通过String getWeather(String cityName)获得返回的数据
public static String getWeather(String cityName)
{
String result=null;
String url=”http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=2&city=”+cityName+”&dfc=3”;
try{
HttpClient client=new DefaultHttpClient();
HttpGet mothod = new HttpGet(url);
HttpResponse httpResponse = client.execute(mothod);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
result = EntityUtils.toString(httpResponse.getEntity(),”gb2312”);
return result;
(5)异步通信AsynTask对天气进行查询.
protected void onPostExecute(Object result) { progressDialog.dismiss(); if(result!=null) { String weatherResult = (String)result; if(weatherResult.split(";").length>1){ String a = weatherResult.split(";")[1]; if(a.split("=").length>1){ String b = a.split("=")[1]; String c = b.substring(1,b.length()-1); String[] resultArr = c.split("\\}"); if(resultArr.length>0) { todayInfor(resultArr[0]); tomorrowInfor(resultArr[1]); afterTorrowInfor(resultArr[2]); td_city.setText(cityName); ws2_ll_yes.setVisibility(View.VISIBLE); } } } } } @Override protected Object doInBackground(Object... params) { //查询天气 return WeatherService.getWeather(cityName); } }
(6) 解析数据更新UI界面
相关文章推荐
- 设计模式 分类 总概括
- Web前端技能
- 滚动视图 解决弹出键盘遮住输入框的问题 实例解析
- unity开发相关环境(vs、MonoDevelop)windows平台编码问题
- 关于vs中scanf函数的一点说明
- How Classes are Found
- 第一次玩php程序
- JAVA 函数式接口与c#委托对应关系(一)
- 性格对咱的影响
- Oracle查询客户端编码集
- 单例模式(Singleton Pattern)
- Spring事务简要笔记
- PL/SQL建表时中文乱码
- 怎样认识语言和思维的关系
- Linux下安装Mongodb3.0
- 笔记本Lenovo Y470s硬件信息
- Thread
- FragmentDialog去掉背景
- hdu 1024 最大m子段和
- Python: 对象与参考