`
willvvv
  • 浏览: 329024 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

调用谷歌接口实现基站转经纬度

阅读更多

Cell2GPSUtil.java,最近项目中用到的,记录一下。

 

package your.package;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Cell2GPSUtil {

	private static final Logger Log = LoggerFactory
			.getLogger(Cell2GPSUtil.class);

	private static final String GOOGLE_API_KEY = "YOURS";

	/**
	 * 通过google接口基站转经纬度 way 2: use JSONObject to build post data
	 * 
	 * @param towers
	 * @return
	 */
	public static Map<String, Double> cell2GPSByGoogle(
			List<Map<String, String>> towers) {
		Map<String, Double> result = new HashMap<String, Double>();
		StringBuffer sb = new StringBuffer();
		String cellId = null;
		String LAC = null;
		String MCC = null;
		String MNC = null;
		for (Map<String, String> tower : towers) {
			cellId = tower.get("cellId");
			LAC = tower.get("LAC");
			MCC = tower.get("MCC");
			MNC = tower.get("MNC");
			if (MCC == null || MNC == null) {
				continue;
			}
		}

		try {
			// 组装JSON查询字符串
			JSONObject holder = new JSONObject();
			holder.put("version", "1.1.0");
			holder.put("host", "maps.google.com");
			holder.put("access_token", GOOGLE_API_KEY);
			holder.put("address_language", "zh_CN");
			// holder.put("request_address", true);
			JSONArray array = new JSONArray();
			JSONObject data = new JSONObject();
			data.put("cell_id", cellId); // 25070
			data.put("location_area_code", LAC);// 4474
			data.put("mobile_country_code", MCC);// 460
			data.put("mobile_network_code", MNC);// 0
			array.put(data);
			holder.put("cell_towers", array);
			// 创建连接,发送请求并接受回应
			DefaultHttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost("http://www.google.com/loc/json");
			StringEntity se = new StringEntity(holder.toString());
			post.setEntity(se);
			HttpResponse resp = client.execute(post);
			HttpEntity entity = resp.getEntity();
			BufferedReader br = new BufferedReader(new InputStreamReader(
					entity.getContent()));
			String line = br.readLine();
			while (line != null) {
				sb.append(line);
				line = br.readLine();
			}
			JSONObject jo = new JSONObject(sb.toString());
			if (jo.has("location")
					&& jo.getJSONObject("location").has("latitude")
					&& jo.getJSONObject("location").has("longitude")) {
				result.put("lat",
						jo.getJSONObject("location").getDouble("latitude"));
				result.put("log",
						jo.getJSONObject("location").getDouble("longitude"));
			}
		} catch (Exception e) {
			Log.error("cell2GPSByGoogle Exception" + e.getMessage());
		}
		Log.debug("cellId -> GPS: cellId=" + cellId + ",LAC=" + LAC + ",MNC="
				+ MNC + ",MCC=" + MCC + " => " + result);
		return result;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List<Map<String, String>> towers = new ArrayList<Map<String, String>>();
		Map<String, String> towerMap = new HashMap<String, String>();
		towerMap.put("cellId", "3620");
		towerMap.put("LAC", "9340");
		towerMap.put("MCC", "460");
		towerMap.put("MNC", "0"); // is 0, not 01
		towers.add(towerMap);
		Map<String, Double> gps = Cell2GPSUtil.cell2GPSByGoogle(towers);
		System.out.println(gps);
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics