Java利用CellID LAC调用Google接口获取经纬度例子

http://code.google.com/intl/zh-CN/apis/gears/geolocation_network_protocol.html

代码中是用GSM查询格式,如果是CDMA查询和GSM查询格式是一样的,只是需要修改4个地方:
1.radio_type 改为 “cdma”
2.cell_id 用 BID值替换
3.location_area_code用NID值替换
4.mobile_network_code用SID值替换

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Java利用CellID LAC调用Google接口获取经纬度例子
 */
public class GoogleJson {
	public static void main(String args[]) {

		GoogleJson test = new GoogleJson();
		URL url = null;
		HttpURLConnection conn = null;
		try {
			url = new URL("http://www.google.com/loc/json");
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");

			String json = test.getJson();
			System.out.println(json);

			conn.getOutputStream().write(json.getBytes());
			conn.getOutputStream().flush();
			conn.getOutputStream().close();
			int code = conn.getResponseCode();
			System.out.println("code " + code);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			String inputLine;
			inputLine = in.readLine();
			System.out.println(inputLine);
			in.close();
			// 解析结果
			// JSONObject result = new JSONObject(inputLine);
			// JSONObject location = result.getJSONObject("location");
			// JSONObject address = location.getJSONObject("address");

			// System.out.println("city = " + address.getString("city"));
			// System.out.println("region = " + address.getString("region"));

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null)
				conn.disconnect();
		}
	}

	/**
	 * Google的官方例子
	 */
	private String getJson() {
		String json = "{ "
				+ "\"version\": \"1.1.0\", "
				+ "\"host\": \"maps.google.com\", "
				// +
				// "\"access_token\": \"2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe\", "
				+ "\"home_mobile_country_code\": 460, "
				+ "\"home_mobile_network_code\": 00, "
				+ "\"radio_type\": \"gsm\", "
				+ "\"carrier\": \"Vodafone\", "
				+ "\"request_address\": true, "
				+ "\"address_language\": \"zh_CN\", "
				// + "\"location\": { "
				// + "\"latitude\": 51.0, " + "\"longitude\": -0.1 " + "}, "
				+ "\"cell_towers\": [ "
				+ "{ "
				+ "\"cell_id\": 4912, "
				+ "\"location_area_code\": 20516, "
				+ "\"mobile_country_code\": 460, "
				+ "\"mobile_network_code\": 00, "
				+ "\"age\": 0, "
				+ "\"signal_strength\": -60, "
				+ "\"timing_advance\": 5555 "
				+ "}"
				// +", " + "{ " + "\"cell_id\": 88, "
				// + "\"location_area_code\": 415, "
				// + "\"mobile_country_code\": 310, "
				// + "\"mobile_network_code\": 580, " + "\"age\": 0, "
				// + "\"signal_strength\": -70, " + "\"timing_advance\": 7777 "
				// + "}"
				+ "]" 
//				+", " + "\"wifi_towers\": [ " + "{ "
//				+ "\"mac_address\": \"00:18:39:f4:29:01\", "
//				+ "\"signal_strength\": 8, " + "\"age\": 0 " + " }"
//				// ", " + "{ "
//				// + " \"mac_address\": \"01-23-45-67-89-ac\", "
//				// + " \"signal_strength\": 4, " + " \"age\": 0 " + "}"
//				+ "] "
				
				+ "}";
		return json;
	}
}

  

猜你喜欢

转载自noobjava.iteye.com/blog/1012651