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

(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的正确

"com.baidu.lbsapi.API_KEY"
       android:value="key" />       //key:开发者申请的key

c.权限设置


<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>

(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界面

你可能感兴趣的:(android)