从google的json接口获取地理位置信息的Java代码

转自:作者: lizongbo  发表于:22:55. 星期四, 五月 5th, 2011 
版权声明 :可以任意转载,转载时请务必以超链接形式标明文章 和作者信息及本版权声明。



从google的json接口获取地理位置信息的Java代码

 提供了公开的接口,通过,这个接口,根据gps或基站信息或wifi热点信息来获取当前位置的地理信息.
gps信息是经纬度,基站信息是基站的cellid等信息,wifi热点信息是wifi的mac地址是热点名字,信号强度等。
因此可以通过java程序获取相应信息,在LBS项目中可以用上:

api接口定义描述在:

http://code.google.com/p/gears/wiki/GeolocationAPI

Java代码如下:

package com.twoicewoo;

import org.json.JSONArray;
import org.json.JSONObject;

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

/**
 * 从google的json接口获取地理位置信息的代码
 *
 * @author lizongbo
 *
 */
public class GeoTest {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String url = "http://www.google.com/loc/json";
		JSONObject json = new JSONObject();
		json.put("version", "1.1.0");
		json.put("host", "maps.618119.com");
		json.put("home_mobile_country_code", 460);// 国家代码
		json.put("home_mobile_network_code", 0);// 移动运营商代码
		json.put("radio_type", "gsm");
		json.put("carrier", "lizongbo");
		json.put("request_address", true);
		json.put("address_language", "zh_CN");
		JSONArray jsoncells = new JSONArray();
		json.put("cell_towers", jsoncells);
		JSONArray jsonwifis = new JSONArray();
		json.put("wifi_towers", jsonwifis);
		JSONObject jsoncell = new JSONObject();
		jsoncell.put("mobile_country_code", 460);// 国家代码,mcc
		jsoncell.put("mobile_network_code", 0);// 移动运营商代码,mnc
		jsoncell.put("location_area_code", 9364);// 位置区域代码,lac
		jsoncell.put("cell_id", "3851");// 移动基站id
		// jsoncell.put("age", 0);
		// jsoncell.put("signal_strength", -70);
		// jsoncell.put("timing_advance", 7777);
		jsoncells.put(jsoncell);
		JSONObject jsonwifi = new JSONObject();
		// jsonwifi.put("mac_address", "00-11-22-33-44-55");
		// jsonwifi.put("signal_strength", 8);
		// jsonwifi.put("age", 0);
		// jsonwifis.put(jsonwifi);
		// jsonwifi = new JSONObject();
		jsonwifi.put("mac_address", "00-55-44-33-22-11");//
		jsonwifi.put("ssid", "TPLINK_618119");// 无线路由器的名字
		jsonwifi.put("signal_strength", 8);// 信号强度
		jsonwifi.put("age", 0);
		// jsonwifis.put(jsonwifi);

		System.out.println(json.toString());
		System.out.println(downloadUrlbyPOST(url, json.toString(), null,
				"UTF-8"));

	}

	public static String downloadUrlbyPOST(String urlStr, String query,
			String referer, String encoding) throws Exception {
		String line = "";
		StringBuilder sb = new StringBuilder();
		HttpURLConnection httpConn = null;
		try {
			URL url = new URL(urlStr);
			System.out.println(urlStr + "?" + query);
			Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
					"proxy.lizongbo.com", 8080));
			proxy = Proxy.NO_PROXY;
			httpConn = (HttpURLConnection) url.openConnection(proxy);
			httpConn.setDoInput(true);
			httpConn.setDoOutput(true);
			httpConn.setRequestMethod("POST");
			if (referer != null) {
				httpConn.setRequestProperty("Referer", referer);
			}
			httpConn.setConnectTimeout(5000);
			// httpConn.getOutputStream().write(
			// java.net.URLEncoder.encode(query, "UTF-8").getBytes());
			httpConn.getOutputStream().write(query.getBytes());
			httpConn.getOutputStream().flush();
			httpConn.getOutputStream().close();

			BufferedReader in = null;
			if (httpConn.getResponseCode() != 200) {
				System.err.println("error:" + httpConn.getResponseMessage());
				in = new BufferedReader(new InputStreamReader(
						httpConn.getErrorStream(), "UTF-8"));
			} else {
				in = new BufferedReader(new InputStreamReader(
						httpConn.getInputStream(), "UTF-8"));
			}
			while ((line = in.readLine()) != null) {
				sb.append(line).append('\n');
			}
			// 关闭连接
			httpConn.disconnect();
			return sb.toString();
		} catch (Exception e) {
			// 关闭连接
			httpConn.disconnect();
			System.out.println(e.getMessage());
			throw e;
		}
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值