读书人

依据经纬度获取当地的位置

发布时间: 2012-07-30 16:19:05 作者: rapoo

根据经纬度获取当地的位置

package com.okhiking;import java.io.BufferedReader;import java.io.InputStreamReader;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.json.JSONArray;import org.json.JSONObject;import android.content.Context;import android.util.Log;import com.okhiking.SLocation.SCell;import com.okhiking.SLocation.SItude;public class LocationUtil {public static String getLocation(Double lat ,Double lon) throws Exception {String resultString = "";/** 这里采用get方法,直接将参数加到URL上 */String urlString = String.format("http://maps.google.com/maps/geo?key=abcdefg&q=%s,%s",lat, lon);Log.i("URL", urlString);/** 新建HttpClient */HttpClient client = new DefaultHttpClient();/** 采用GET方法 */HttpGet get = new HttpGet(urlString);try {/** 发起GET请求并获得返回数据 */HttpResponse response = client.execute(get);HttpEntity entity = response.getEntity();BufferedReader buffReader = new BufferedReader(new InputStreamReader(entity.getContent()));StringBuffer strBuff = new StringBuffer();String result = null;while ((result = buffReader.readLine()) != null) {strBuff.append(result);}resultString = strBuff.toString();/** 解析JSON数据,获得物理地址 */if (resultString != null && resultString.length() > 0) {JSONObject jsonobject = new JSONObject(resultString);JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark").toString());resultString = "";for (int i = 0; i < jsonArray.length(); i++) {resultString = jsonArray.getJSONObject(i).getString("address");}}} catch (Exception e) {throw new Exception("获取物理位置出现错误:" + e.getMessage());} finally {get.abort();client = null;}return resultString;}}


读书人网 >移动开发

热点推荐