1.申请百度AK

申请地址:http://lbsyun.baidu.com/apiconsole/key

在应用中的manifest申明



2.注册服务以及导入jar包和so文件

在lib/文件夹下导入BaiduLBS_Android.jar

在lib/armeabi文件夹下导入liblocSDK4d.so

注册百度服务:

        

申明权限:

                                                                                                                

3.定位

package com.location.model;import android.content.Context;import android.util.Log;import com.baidu.location.BDLocation;import com.baidu.location.BDLocationListener;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import com.baidu.location.LocationClientOption.LocationMode;import com.physical.training.utils.Utils;public class LocationApi {private Context mContext;private static volatile LocationApi sApi;public LocationClient mLocationClient = null;public BDLocationListener myListener = new MyLocationListener();private double mPreLongitude = Integer.MIN_VALUE;private double mPreLatitude = Integer.MIN_VALUE;private OnLocationListener mLocationListener;private double mDistance = 0;private LocationApi(Context context) {mContext = context;}public static LocationApi getInstance(Context context) {if (sApi == null) {synchronized (LocationApi.class) {if (sApi == null) {sApi = new LocationApi(context.getApplicationContext());}}}return sApi;}public void setOnLocationListener(OnLocationListener l) {mLocationListener = l;}public void startLocation() {mLocationClient = new LocationClient(mContext); // 声明LocationClient类mLocationClient.registerLocationListener(myListener); // 注册监听函数LocationClientOption option = new LocationClientOption();option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02option.setScanSpan(3000);// 设置发起定位请求的间隔时间option.setIsNeedAddress(true);// 返回的定位结果包含地址信息option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向option.setOpenGps(true);mLocationClient.setLocOption(option);mLocationClient.start();//开始定位mLocationClient.requestLocation();mPreLongitude = Integer.MIN_VALUE;mPreLatitude = Integer.MIN_VALUE;mDistance = 0;}public void stopLocation() {if(mLocationClient != null) {mLocationClient.stop();mLocationClient.unRegisterLocationListener(myListener);mLocationClient = null;mPreLongitude = Integer.MIN_VALUE;mPreLatitude = Integer.MIN_VALUE;mDistance = 0;}}public class MyLocationListener implements BDLocationListener {@Overridepublic void onReceiveLocation(BDLocation location) {if (location == null)return;double longitude = location.getLongitude();double latitude = location.getLatitude();Log.e("LocationApi", longitude + "  " + latitude);Log.e("LocationApi", "mDistance++  = " + mDistance);if (mPreLongitude != Integer.MIN_VALUE) {mDistance += Math.abs(Utils.getDistance(mPreLongitude, mPreLatitude, longitude, latitude)) + 100;}Log.e("LocationApi", "mDistance--  = " + mDistance);if (mLocationListener != null) {mLocationListener.onLocationChange();}mPreLongitude = longitude;mPreLatitude = latitude;}}public int getDistance() {return (int) mDistance;}public interface OnLocationListener {public void onLocationChange();}}

4.天气

WeatherApi.java定位到经纬度

package com.weather.model;import com.baidu.location.BDLocation;import com.baidu.location.BDLocationListener;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import com.baidu.location.LocationClientOption.LocationMode;import com.common.data.HttpEventHandler;import com.physical.training.R;import android.app.Notification;import android.app.NotificationManager;import android.content.Context;import android.util.Log;public class WeatherApi {private Context mContext;private static volatile WeatherApi sApi;private WeatherFactory mWeatherFactory;public LocationClient mLocationClient = null;public BDLocationListener myListener = new MyLocationListener();OnWeatherUpdateListener mListener;WeatherInfo mInfo;private WeatherApi(Context context) {mContext = context;mLocationClient = new LocationClient(mContext); // 声明LocationClient类mLocationClient.registerLocationListener(myListener); // 注册监听函数mWeatherFactory = new WeatherFactory();mWeatherFactory.setHttpEventHandler(mEventHandler);}public void setOnWeatherUpdateListener(OnWeatherUpdateListener l) {mListener = l;}public void startLocation() {LocationClientOption option = new LocationClientOption();option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02option.setScanSpan(60 * 60 * 1000);// 设置发起定位请求的间隔时间option.setIsNeedAddress(true);// 返回的定位结果包含地址信息option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向mLocationClient.setLocOption(option);mLocationClient.start();}public void stopLocation() {mLocationClient.stop();}public static WeatherApi getInstance(Context context) {if (sApi == null) {synchronized (WeatherApi.class) {if (sApi == null) {sApi = new WeatherApi(context.getApplicationContext());}}}return sApi;}public class MyLocationListener implements BDLocationListener {@Overridepublic void onReceiveLocation(BDLocation location) {if (location == null)return;Log.d("LocSDK5", location.getCity() + "  " + location.getCityCode());mWeatherFactory.downloaDatas(location.getLongitude() + "," + location.getLatitude());}}public WeatherInfo getWeatherInfo() {return mInfo;}public void destory() {mLocationClient.unRegisterLocationListener(myListener);}private HttpEventHandler mEventHandler = new HttpEventHandler() {@Overridepublic void HttpSucessHandler(WeatherInfo arg0) {mInfo = arg0;if (mListener != null) {mListener.onWeatherUpdate();}if (mInfo != null) {NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);Notification notification = new Notification(R.drawable.ic_launcher, "weather",System.currentTimeMillis());notification.setLatestEventInfo(mContext,mContext.getResources().getString(R.string.notification_weather_title, mInfo.cityName),mInfo.weather + "," + mInfo.temperature + "," + mInfo.wind, null);notificationManager.notify(Integer.MAX_VALUE, notification);}}@Overridepublic void HttpFailHandler() {}};public interface OnWeatherUpdateListener {public void onWeatherUpdate();}}

WeatherFactory.java创建访问天气的uri,获取天气的json数据,http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=T2AHbrsEn5FaSg4Iir8YfP1U,location表示经纬度,用分号隔开

package com.weather.model;import java.io.IOException;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.util.Log;import com.common.data.HttpJsonFactoryBase;public class WeatherFactory extends HttpJsonFactoryBase {@Overrideprotected WeatherInfo analysisData(JSONObject json) throws IOException {WeatherInfo info = new WeatherInfo();Log.e("WeatherFactory", json.toString());try {JSONArray results = json.getJSONArray("results");JSONObject result = results.getJSONObject(0);info.cityName = result.getString("currentCity");info.pm25 = result.getString("pm25");JSONArray weatherDatas = result.getJSONArray("weather_data");JSONObject weatherData = weatherDatas.getJSONObject(0);info.weather = weatherData.getString("weather");info.wind = weatherData.getString("wind");info.temperature = weatherData.getString("temperature");Log.e("WeatherFactory", info.toString());return info;} catch (JSONException e) {e.printStackTrace();}return null;}@Overrideprotected String createUri(Object... arg0) {return String.format("http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=T2AHbrsEn5FaSg4Iir8YfP1U",arg0[0]);}}




更多相关文章

  1. android使用百度地图SDK获取定位信息示例
  2. Tools属性Tools Attributes
  3. 【转】备份:Android(安卓)常用 mimeType 表
  4. 备份:Android(安卓)常用 mimeType 表
  5. Arcgis Android(安卓)定位
  6. Appium+Python appium启动夜神模拟器定位元素(三)
  7. Android使用GPS获取用户地理位置并监听位置变化的方法
  8. Android(安卓)定位的实现
  9. 2017 年你应该了解的Android(安卓)库

随机推荐

  1. 短期项目求PHP开发人员或开发团队(2-3名),并
  2. 如何将JSON传回PHP的AJAX请求?
  3. PHP限制HTML内容中图片必须是本站的方法
  4. php网站 手机归属地查询接口
  5. PHP日期添加1年到当前日期。
  6. PHP将邮件发送到多个电子邮件地址
  7. 在Codeigniter中将javascript变量从视图
  8. php 把驼峰样式的字符串转换成下划线样式
  9. PHP邮件脚本占用了大量资源
  10. Mac升级到EI capitan重新设置Apache和php