Android实现自动通过GPS定位用户所在地名

最近开始跟随实验室做Android项目,因为之前一直在写PHP,所以比较吃力,这里写的功能借鉴了CSDN博客的大神的博文,在这里提出谢谢了(大神博文的地址:  http://blog.csdn.net/octobershiner/article/details/6628370)。

本来就是想做一个定位获取到经纬度的,老大说要把地名获取放到客户端,只能咬牙做了。

这里只上传一个功能类:

package com.example.helloworld;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

/**
 *提供设备获取位置信息的入口,通过GPS进行定位
 *需要请求定位权限(android.permission.ACCESS_FINE_LOCATION)
 *实例化时必须传参,将Activity传进来,不然会报错
 */
public class GetLocationInfo {
	
	private LocationManager manager;
	private Activity activity;
	private static final String TAG = "LOCATION DEMO";
	
	public GetLocationInfo(Activity activity) {
		this.activity = activity;
	}
	
	public String getLocInfo() {
		String locInfo = null;
		
		//获取系统服务
		manager = (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);
		Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10, locationListener); 
		//获取位置信息
		locInfo = updateLocation(location);
		
		return locInfo;
	} 
	
	private final LocationListener locationListener = new LocationListener() {     
        public void onLocationChanged(Location location) {     
        	updateLocation(location);     
        }     
        public void onProviderDisabled(String provider){  
            updateLocation(null);     
            Log.i(TAG, "Provider now is disabled..");  
        }     
        public void onProviderEnabled(String provider){  
            Log.i(TAG, "Provider now is enabled..");  
        }     
        public void onStatusChanged(String provider, int status,Bundle extras){ }     
	};  
	
	private String updateLocation(Location location) {
		String latLng = null;
		if(location != null) {
			final double lat = location.getLatitude();
			final double lng = location.getLongitude();
			new Thread(){
				public void run() {
					getLocName(lat, lng);
				};
			}.start();
			//latLng = getLocName(lat, lng);
//			latLng = "Latitude:" + lat + "  Longitude:" + lng;
		} else {
			latLng = "Can't access your location";
		}
		
		return latLng;
	}
	
	private String getLocName(double latitude, double longitude) {
		String locName = null;
		String key = "OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77";
		String uri = "http://apis.map.qq.com/ws/geocoder/v1/?location=" + latitude + ',' + longitude + "&key=" + key;
		
		/*建立HTTP GET 对象*/
		HttpGet httpRequest = new HttpGet(uri);
		try {
			/*发送请求并等待响应*/  
			HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);  
			/*若状态码为200 ok*/  
			if(httpResponse.getStatusLine().getStatusCode() == 200) {  
				/*读*/  
				String strResult = EntityUtils.toString(httpResponse.getEntity());  
				locName = changeJsonToArray(strResult);
				Log.d("message", locName);
			} else {  
				locName = "反地址解析请求中断";
			}  
		} catch(ClientProtocolException e) {
			locName = e.getMessage().toString();  
			e.printStackTrace();  
		} catch(IOException e) {
			locName = e.getMessage().toString();
			e.printStackTrace();
		} catch(Exception e) {
			locName = e.getMessage().toString();
			e.printStackTrace();
		}
		
		return locName;
	}
	
	private static String changeJsonToArray(String json){
		String jsonArray = null;
		JSONObject jsonObject = null;
		try {
			jsonObject = new JSONObject(json);
			jsonArray = jsonObject.getString("result");
			jsonObject = new JSONObject(jsonArray);
			jsonArray = jsonObject.getString("address");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		return jsonArray;
	}
}


你可能感兴趣的:(Android记事)