基于百度地图的定位实现天气预报查询

(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>
<!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据-->
<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界面

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值