---
title: "通过基站,wifi 获取手机位置"
author: "fortunate179"
date: "2012-06-05T18:31:19Z"
tags: []
categories: ["互联网"]
slug: "wifi"
draft: false
lastmod: "2012-06-08T12:54:37Z"
id: 1552314
url: "https://fortunate179.iteye.com/blog/1552314"
---

潜水,换名字,免得被人认出来

最近需要对手机进行定位,可是如果用gps的话,室内的话获取不到,获取速度也是个问题.
so,使用基站获取,或者使用wifi获取是个不错的途径(精确度稍微差了点,不过可以接受).
  采用如上两种方式可以很方便的获取,只需要向google发送基站信息就可以了.
地址是www.google.com/loc/json如果想返回xml格式将json替换为xml.
更详细情况可以看
http://code.google.com/p/gears/wiki/GeolocationAPI

手机的网络类型包括gsm,cdma,wcdma,wifi,四种方式.gsm,cdma这两种类型我已经测试通过了,另外两中因为不需要,所以我也没有测试,如果有兴趣可以自己测试

里面有请求的格式已经请求字段的说明.具体代码网上搜索一大把.
此处大概贴出测试代码,经测试还是不错的阿
HttpClient client = new DefaultHttpClient();
		/** 采用POST方法 */
		HttpPost post = new HttpPost("http://www.google.com/loc/json");

		JSONObject holder = new JSONObject();
		holder.put("version", "1.1.0");
		holder.put("host", "maps.google.com");
		holder.put("address_language", "zh_CN");
		holder.put("request_address", true);
		holder.put("radio_type", cell.getType());

		JSONObject tower = new JSONObject();
		JSONArray towerarray = new JSONArray();
		towerarray.add(tower);
		if ("gsm".equals(cell.getType())) {
			// gsm
			tower.put("cell_id", cell.getCID());
			tower.put("location_area_code", cell.getLAC());

			tower.put("mobile_country_code", cell.getMCC());
			tower.put("mobile_network_code", cell.getMNC());
			holder.put("cell_towers", towerarray);
		} else if ("cdma".equals(cell.getType())) {
			// cdma
			tower.put("cell_id", cell.getBid());
			tower.put("location_area_code", cell.getNid());

			tower.put("mobile_country_code", cell.getMCC());
			tower.put("mobile_network_code", cell.getSid());
			holder.put("cell_towers", towerarray);
		} else if ("wcdma".equals(cell.getType())) {
			// wcdma
			// holder.put("cell_towers", towerarray);
		} else if ("wifi".equals(cell.getType())) {
			// wifi
			// holder.put("wifi_towers", towerarray);
		}

		HttpEntity entity = new StringEntity(holder.toJSONString());
		post.setEntity(entity);

		/** 发出POST数据并获取返回数据 */
		HttpResponse response = client.execute(post);
		HttpEntity responseEntity = response.getEntity();
		BufferedReader buffReader = new BufferedReader(new InputStreamReader(
				responseEntity.getContent()));
		StringBuffer strBuff = new StringBuffer();
		String result = null;
		while ((result = buffReader.readLine()) != null) {
			strBuff.append(result);
			System.out.println(strBuff);
		}

下面的是单元测试的代码:
Cell cell = new Cell();
		// gsm
		// cell.setCID("39990");
		// cell.setLAC("4303");
		// cell.setMCC("460");
		// cell.setMNC("0");
		// cell.setType("gsm");

		// cdma
		cell = new Cell();
		cell.setSid("13824");// mnc
		cell.setNid("2");// lac
		cell.setBid("30737");// cid
		cell.setMCC("460");
		cell.setType("cdma");