android 百度API定位以及获取天气

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");// 返回的定位结果是百度经纬度,默认值gcj02
		option.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 {
		@Override
		public 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");// 返回的定位结果是百度经纬度,默认值gcj02
		option.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 {
		@Override
		public 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() {

		@Override
		public 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);
			}
		}

		@Override
		public 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 {

	@Override
	protected 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;
	}

	@Override
	protected String createUri(Object... arg0) {
		return String.format(
				"http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=T2AHbrsEn5FaSg4Iir8YfP1U",
				arg0[0]);
	}

}




你可能感兴趣的:(android,应用)