android 定位天气预报,Android上实现一个简单的天气预报APP(十四) 使用百度API定位城市...

学习参考资源:https://www.gitbook.com/book/zhangqx/mini-weather/details

环境:Android Studio

外部API:使用百度API定位:http://lbsyun.baidu.com/index.php?title=android-locsdk

0818b9ca8b590ca3270a3433284dd417.png

1)前期准备

1.获取密钥

Android Studio获得数字签名的方法:

包名在Manifest中。

数字签名+包名获取AK密钥。

0818b9ca8b590ca3270a3433284dd417.png

2.导入库文件

在左侧边栏下方的“相关下载”,点击后进入下载界面,下载相应的包,存入app\libs

0818b9ca8b590ca3270a3433284dd417.png

右键BaiduXXX_XXX.jar——点击“Add as Library”

配置build\build.gradle

0818b9ca8b590ca3270a3433284dd417.png

添加了这条语句后,Android Studio会自动生成一个jniLibs文件夹,将这些库文件方进入去

0818b9ca8b590ca3270a3433284dd417.png

3.配置Manifest.xml

设置Access Key

android:name="com.baidu.lbsapi.API_KEY"

android:value="AK" /> //key:开发者申请的Key

声明权限

0818b9ca8b590ca3270a3433284dd417.png

2)编写我们自己的定位程序

百度定位包类参考:http://wiki.lbsyun.baidu.com/cms/androidloc/doc/v7.0/index.html

1.获取定位城市名称

package com.example.xchen.mweather;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import com.baidu.location.BDLocation;

import com.baidu.location.BDLocationListener;

import com.baidu.location.LocationClient;

import com.baidu.location.LocationClientOption;

/**

* Created by xchen on 16/12/23.

*/

public class Locate extends Activity{

private LocationClient mLocationClient;

private MyLocationListener myLocationListener;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.locate);

mLocationClient = new LocationClient(this);

myLocationListener = new MyLocationListener();

mLocationClient.registerLocationListener(myLocationListener);

initLocation();

mLocationClient.start();

}

void initLocation()

{

LocationClientOption option = new LocationClientOption();

option.setIsNeedAddress(true);

option.setOpenGps(true);

option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);

option.setCoorType("bd09ll");

option.setScanSpan(1000);

mLocationClient.setLocOption(option);

}

}

class MyLocationListener implements BDLocationListener{

@Override

public void onReceiveLocation(BDLocation bdLocation) {

String cityName = bdLocation.getCity();

Log.d("Locate",cityName);

}

}

启动这个Activity

0818b9ca8b590ca3270a3433284dd417.png

2.将获取的城市信息更新到界面上

在location布局中,设置了一个Button,这个Button的内容为城市名称

0818b9ca8b590ca3270a3433284dd417.png

在Location类中定义Button并绑定

public class Locate extends Activity{

private LocationClient mLocationClient;

private MyLocationListener myLocationListener;

Button locateBtn;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.location);

locateBtn = (Button)findViewById(R.id.bdmap_cityname);

mLocationClient = new LocationClient(this);

myLocationListener = new MyLocationListener(locateBtn);

mLocationClient.registerLocationListener(myLocationListener);

initLocation();

mLocationClient.start();

}

void initLocation()

{

LocationClientOption option = new LocationClientOption();

option.setIsNeedAddress(true);

option.setOpenGps(true);

option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);

option.setCoorType("bd09ll");

option.setScanSpan(1000);

mLocationClient.setLocOption(option);

}

}

在MyLocationListener中,一获取数据就将数据更新到Button上。

public class MyLocationListener implements BDLocationListener {

Button locBtn;

MyLocationListener(Button locBtn)

{

this.locBtn = locBtn;

}

String cityName;

@Override

public void onReceiveLocation(BDLocation bdLocation) {

cityName = bdLocation.getCity();

Log.d("Locate",cityName);

locBtn.setText(cityName);

}

}

0818b9ca8b590ca3270a3433284dd417.png

3.新增动作-根据获取的城市名称,根据数据库表找到对应的城市代码,并更新主界面天气数据

类似于我们前面,通过城市选择列表,点击Item获取城市代码,并更新天气主界面。

0818b9ca8b590ca3270a3433284dd417.png

运行一下!

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

你可能感兴趣的:(android,定位天气预报)