Android基站定位——单基站定位(二)

news/2025/1/15 16:11:19/

转载请标明出处:http://blog.csdn.net/android_ls/article/details/8672856

基站定位原理:通过手机信号获取基站信息,然后调用第三方公开的根据基站信息查找基站的经纬度值及地址信息(大概位置)。

 一、通过手机信号获取基站信息(详细的可以参考:Android基站定位——通过手机信号获取基站信息(一))

                TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);// 返回值MCC + MNCString operator = mTelephonyManager.getNetworkOperator();mcc = Integer.parseInt(operator.substring(0, 3));mnc = Integer.parseInt(operator.substring(3));// 中国移动和中国联通获取LAC、CID的方式GsmCellLocation location = (GsmCellLocation) mTelephonyManager.getCellLocation();lac = location.getLac();cid = location.getCid();Log.i(TAG, "MCC = " + mcc + "\t MNC = " + mnc + "\t LAC = " + lac + "\t CID = " + cid);

二、调用第三方公开的API(根据基站信息查找基站的经纬度值及地址信息)

      1、组拼JSON形式的请求参数

 /*** 获取JSON形式的基站信息* @param mcc 移动国家代码(中国的为460)* @param mnc 移动网络号码(中国移动为0,中国联通为1,中国电信为2); * @param lac 位置区域码* @param cid 基站编号* @return json* @throws JSONException*/private String getJsonCellPos(int mcc, int mnc, int lac, int cid) throws JSONException {JSONObject jsonCellPos = new JSONObject();jsonCellPos.put("version", "1.1.0");jsonCellPos.put("host", "maps.google.com");JSONArray array = new JSONArray();JSONObject json1 = new JSONObject();json1.put("location_area_code", "" + lac + "");json1.put("mobile_country_code", "" + mcc + "");json1.put("mobile_network_code", "" + mnc + "");json1.put("age", 0);json1.put("cell_id", "" + cid + "");array.put(json1);jsonCellPos.put("cell_towers", array);return jsonCellPos.toString();}

      2、通过HTTP协议网络请求源码: 

request URL:http://www.minigps.net/minigps/map/google/location
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive
Content-Length:191
Content-Type:application/json; charset=UTF-8
Cookie:bdshare_firstime=1356366713546; JSESSIONID=68243935CD3355089CF07A3A22AAB372
Host:www.minigps.net
Origin:http://www.minigps.net
Referer:http://www.minigps.net/map.html
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4
X-Requested-With:XMLHttpRequest
Request Payload
{"cell_towers":[{"mobile_network_code":"1","location_area_code":"43018","cell_id":"11152773","age":0,"mobile_country_code":"460"}],"host":"maps.google.com","version":"1.1.0"}Response Headersview source
Content-Type:application/json
Date:Sat, 03 Jan 2013 14:03:15 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked

      

        3、用JAVA代码具体实现:

 /*** 调用第三方公开的API根据基站信息查找基站的经纬度值及地址信息*/public String httpPost(String url, String jsonCellPos) throws IOException{byte[] data = jsonCellPos.toString().getBytes();URL realUrl = new URL(url);HttpURLConnection httpURLConnection = (HttpURLConnection) realUrl.openConnection();httpURLConnection.setConnectTimeout(6 * 1000);httpURLConnection.setDoOutput(true);httpURLConnection.setDoInput(true);httpURLConnection.setUseCaches(false);httpURLConnection.setRequestMethod("POST");httpURLConnection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");httpURLConnection.setRequestProperty("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");httpURLConnection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");httpURLConnection.setRequestProperty("Connection", "Keep-Alive");httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");httpURLConnection.setRequestProperty("Host", "www.minigps.net");httpURLConnection.setRequestProperty("Referer", "http://www.minigps.net/map.html");httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4X-Requested-With:XMLHttpRequest");httpURLConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");httpURLConnection.setRequestProperty("Host", "www.minigps.net");DataOutputStream outStream = new DataOutputStream(httpURLConnection.getOutputStream());outStream.write(data);outStream.flush();outStream.close();if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {InputStream inputStream = httpURLConnection.getInputStream();return new String(read(inputStream));}return null;}

      4、读取返回的JSON数据流代码:

 /*** 读取IO流并以byte[]形式存储* @param inputSream InputStream* @return byte[]* @throws IOException*/public byte[] read(InputStream inputSream) throws IOException {ByteArrayOutputStream outStream = new ByteArrayOutputStream();int len = -1;byte[] buffer = new byte[1024];while ((len = inputSream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}outStream.close();inputSream.close();return outStream.toByteArray();}

 

三、请求参数及返回结果的JSON形式:

      1、请求的JSON参数值:

       {"cell_towers":[{"mobile_network_code":"1","location_area_code":"43018","cell_id":"11152773","age":0,"mobile_country_code":"460"}],"host":"maps.google.com","version":"1.1.0"}

       2、返回的JSON结果值:

    {"location":{"latitude":"31.211389541625977","longitude":"121.60332489013672","address":{"city":"上海市浦东新区居里路432号;浦东新区光启安老院、第一三共制药上海公司、SUNPLUS[附近]","country":"","country_code":"","county":"","postal_code":"","region":"","street":"","street_number":""}},"access_token":"dummytoken"}

四、完整代码及所需权限:

Java代码:

package com.easipass.test;import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.View;/*** 功能描述:单基站定位* @author android_ls*/
public class GSMCellLocationActivity extends Activity {private static final String TAG = "GSMCellLocationActivity";private int mcc;private int mnc;private int lac;private int cid;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 获取基站信息findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);// 返回值MCC + MNCString operator = mTelephonyManager.getNetworkOperator();mcc = Integer.parseInt(operator.substring(0, 3));mnc = Integer.parseInt(operator.substring(3));// 中国移动和中国联通获取LAC、CID的方式GsmCellLocation location = (GsmCellLocation) mTelephonyManager.getCellLocation();lac = location.getLac();cid = location.getCid();Log.i(TAG, "MCC = " + mcc + "\t MNC = " + mnc + "\t LAC = " + lac + "\t CID = " + cid);new Thread() {@Overridepublic void run() {try {String json = getJsonCellPos(mcc, mnc, lac, cid);Log.i(TAG, "request = " + json);String url = "http://www.minigps.net/minigps/map/google/location";String result = httpPost(url, json);Log.i(TAG, "result = " + result);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}.start();}});}/*** 调用第三方公开的API根据基站信息查找基站的经纬度值及地址信息*/public String httpPost(String url, String jsonCellPos) throws IOException{byte[] data = jsonCellPos.toString().getBytes();URL realUrl = new URL(url);HttpURLConnection httpURLConnection = (HttpURLConnection) realUrl.openConnection();httpURLConnection.setConnectTimeout(6 * 1000);httpURLConnection.setDoOutput(true);httpURLConnection.setDoInput(true);httpURLConnection.setUseCaches(false);httpURLConnection.setRequestMethod("POST");httpURLConnection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");httpURLConnection.setRequestProperty("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");httpURLConnection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");httpURLConnection.setRequestProperty("Connection", "Keep-Alive");httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");httpURLConnection.setRequestProperty("Host", "www.minigps.net");httpURLConnection.setRequestProperty("Referer", "http://www.minigps.net/map.html");httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4X-Requested-With:XMLHttpRequest");httpURLConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");httpURLConnection.setRequestProperty("Host", "www.minigps.net");DataOutputStream outStream = new DataOutputStream(httpURLConnection.getOutputStream());outStream.write(data);outStream.flush();outStream.close();if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {InputStream inputStream = httpURLConnection.getInputStream();return new String(read(inputStream));}return null;}/*** 获取JSON形式的基站信息* @param mcc 移动国家代码(中国的为460)* @param mnc 移动网络号码(中国移动为0,中国联通为1,中国电信为2); * @param lac 位置区域码* @param cid 基站编号* @return json* @throws JSONException*/private String getJsonCellPos(int mcc, int mnc, int lac, int cid) throws JSONException {JSONObject jsonCellPos = new JSONObject();jsonCellPos.put("version", "1.1.0");jsonCellPos.put("host", "maps.google.com");JSONArray array = new JSONArray();JSONObject json1 = new JSONObject();json1.put("location_area_code", "" + lac + "");json1.put("mobile_country_code", "" + mcc + "");json1.put("mobile_network_code", "" + mnc + "");json1.put("age", 0);json1.put("cell_id", "" + cid + "");array.put(json1);jsonCellPos.put("cell_towers", array);return jsonCellPos.toString();}/*** 读取IO流并以byte[]形式存储* @param inputSream InputStream* @return byte[]* @throws IOException*/public byte[] read(InputStream inputSream) throws IOException {ByteArrayOutputStream outStream = new ByteArrayOutputStream();int len = -1;byte[] buffer = new byte[1024];while ((len = inputSream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}outStream.close();inputSream.close();return outStream.toByteArray();}}

在AndroidManifest.xml添加获取位置信息的权限:

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.INTERNET" />

五、测试网址:http://www.minigps.net/map.html

六、Google的基站定位http://www.google.com/loc/json或者http://www.google.com.hk/loc/json已不可用,访问返回404。官方给出的答复:https://developers.google.com/gears/?hl=zh-TW

 参考过的博客:echo3博主的json基站定位接口 免费使用

 


http://www.ppmy.cn/news/671224.html

相关文章

基于基站的移动台定位

基于基站的移动台定位 西安邮电学院 董安亮 摘要&#xff1a;   基于gpsOne技术的移动台定位技术已经在C网实现&#xff0c;而GSM手机定位方式通常可分为基于网络方式和基于终端方式两种。这里介绍的是一种基于基站的移动台定位技术。   关键词&#xff1a;GPSgpsOne …

Android基站定位——通过手机信号获取基站信息(一)

转载请标明出处&#xff1a;http://blog.csdn.net/android_ls/article/details/8672442 基站定位原理&#xff1a;通过手机信号获取基站信息&#xff0c;然后调用第三方公开的根据基站信息查找基站的经纬度值&#xff0c;想要具体地址信息的再根据经纬度值获取具体的地址信息。…

数据结构之 时间复杂度与空间复杂度

目录 1&#xff1a;什么是时间复杂度和空间复杂度 2&#xff1a;时间复杂度与空间复杂度求法 3&#xff1a;几个典型时间复杂度与空间复杂度的分析 1&#xff1a;什么是时间复杂度与空间复杂度 首先对于我们所写的程序来说&#xff0c;为了评估一个算法的好与坏我们需要通过…

基站定位原理

GSM蜂窝基站定位基本原理浅析 2010-12-10 23:49 by MagicBoy110, 31483 阅读, 11 评论, 收藏, 编辑 位置服务已经成为越来越热的一门技术&#xff0c;也将成为以后所有移动设备&#xff08;智能手机、掌上电脑等&#xff09;的标配。随着人们对BLS(Based Location Serices&…

基站定位

最近在做基站定位&#xff0c;移动电信的定位测试都可以。就是联通的定位不了。在这里把源码上传&#xff0c;希望各位帮忙找出原因。 标签&#xff1a; <无> 代码片段(4) [图片] android 基站定位 [文件] GetBaseStationInfo.zip ~ 62KB 下载(276) [代码] android 基…

基站定位技术原理

位置服务已经成为越来越热的一门技术&#xff0c;也将成为以后所有移动设备&#xff08;智能手机、掌上电脑等&#xff09;的标配。随着人们对BLS(Based Location Serices&#xff0c;基于位置的服务)需求的飞速增长&#xff0c;无线定位技术也越来越得到重视。GSM蜂窝基站定位…

基站定位-基于基站的移动台定位系统

基于基站的移动台定位系统 GSM手机定位方式通常可以分为基于网络方式和基于终端方式两种。从技术方面又可分为到达时间&#xff08;TOA&#xff09;、增强测量时间差&#xff08;E&#xff0d;OTD&#xff09;和GPS辅助&#xff08;A&#xff0d;GPS&#xff09;三种方式。而这…

关于移动4G基站定位

关于移动4G基站定位 要了解移动4G基站是怎么样实现定位的&#xff0c;首先要了解清楚&#xff1a; 1、什么是CGI? CGI&#xff08;Cell Global Identity&#xff0c;小区全球标识&#xff09;是全球范围内无线网络小区的唯一标识&#xff0c;不同运营商的不同网络可以有不同的…