Android開發(fā)筆記之簡(jiǎn)單基站定位程序的實(shí)現(xiàn)
經(jīng)過(guò)學(xué)習(xí),已經(jīng)對(duì)Android程序的開發(fā)流程有了個(gè)大體的了解,為了提高我們的學(xué)習(xí)興趣,在這一節(jié)我們將編寫一個(gè)簡(jiǎn)單的基站定位程序?,F(xiàn)在LBS(Location Based Service,基于位置的服務(wù))移動(dòng)應(yīng)用相當(dāng)流行(如:微信,切客,嘀咕,街旁等),基站定位是這類程序用到的關(guān)鍵性技術(shù)之一,我們來(lái)揭開它的神秘面紗吧。
在這一節(jié)里,我們會(huì)接觸到事件、TelephonyManager、HTTP通信、JSON的使用等知識(shí)點(diǎn)。
在Android操作系統(tǒng)下,基站定位其實(shí)很簡(jiǎn)單,先說(shuō)一下實(shí)現(xiàn)流程:
調(diào)用SDK中的API(TelephonyManager)獲得MCC、MNC、LAC、CID等信息,然后通過(guò)google的API獲得所在位置的經(jīng)緯度,最后再通過(guò)google map的API獲得實(shí)際的地理位置。(google真牛?。?/p>
有同學(xué)會(huì)問:MNC、MCC、LAC、CID都是些什么東西?google又怎么通過(guò)這些東西就獲得經(jīng)緯度了呢?
我們一起來(lái)學(xué)習(xí)一下:
MCC,Mobile Country Code,移動(dòng)國(guó)家代碼(中國(guó)的為460);
MNC,Mobile Network Code,移動(dòng)網(wǎng)絡(luò)號(hào)碼(中國(guó)移動(dòng)為00,中國(guó)聯(lián)通為01);
LAC,Location Area Code,位置區(qū)域碼;
CID,Cell Identity,基站編號(hào),是個(gè)16位的數(shù)據(jù)(范圍是0到65535)。
了解了這幾個(gè)名詞的意思,相信有些朋友已經(jīng)知道后面的事了:google存儲(chǔ)了這些信息,直接查詢就能得到經(jīng)緯度了。(至于google怎么得到移動(dòng)、聯(lián)通的基站信息,這就不得而知了,反正google免費(fèi)提供接口,直接調(diào)用就是)
下面開始動(dòng)手。
一、設(shè)置界面
我們?cè)谏弦还?jié)的程序的基礎(chǔ)上進(jìn)行開發(fā),在DemoActivity的界面上實(shí)現(xiàn)這個(gè)功能。
首先我們將DemoActivity使用的布局修改一下:

第1行為TextView,顯示提示文字;第2行為一個(gè)Button,觸發(fā)事件;第3行、第4行分別顯示基站信息和地理位置(現(xiàn)在為空,看不到)。
layout/main.xml文件內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please click the button below to get your location" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
<TextView
android:id="@+id/cellText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/lacationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
接下來(lái)我們打開DemoActivity.java編寫代碼。
二、為按鈕綁定事件
我們?cè)贏ctivity創(chuàng)建時(shí)綁定事件,將以下代碼添加到setContentView(R.layout.main);后:
/** 為按鈕綁定事件 */
Button btnGetLocation = (Button)findViewById(R.id.button1);
btnGetLocation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
onBtnClick();
}
});
同時(shí)還需要在頭部import相關(guān)組件:
import android.view.View; import android.widget.Button; import android.view.View.OnClickListener;
我們來(lái)分析一下這段代碼:
首先我們通過(guò)findViewById(R.id.button1)找到按鈕這個(gè)對(duì)象,前面加(Button)表示顯示的轉(zhuǎn)換為Button對(duì)象;
然后設(shè)置按鈕點(diǎn)擊事件的監(jiān)聽器,參數(shù)為OnClickListener對(duì)象,再重載這個(gè)類的onClick方法,調(diào)用onBtnClick方法(這個(gè)方法得由我們自己去寫,他在點(diǎn)擊按鈕時(shí)被調(diào)用)。
好了,調(diào)用方法寫好了,我們來(lái)寫實(shí)現(xiàn)(調(diào)用后需要做什么事)。動(dòng)手編碼之前先在腦中整理好思路,養(yǎng)成好習(xí)慣。
我們需要在DemoActivty類中添加如下私有方法:
我們需要?jiǎng)倓偺岬降膐nBtnClick回調(diào)方法,被調(diào)用時(shí)實(shí)現(xiàn)取得基站信息、獲取經(jīng)緯度、獲取地理位置、顯示的功能。但是很顯然,全部揉到一個(gè)方法里面并不是個(gè)好主意,我們將它分割為幾個(gè)方法;
- 添加獲取基站信息的方法getCellInfo,返回基站信息;
- 添加獲取經(jīng)緯度的方法getItude,傳入基站信息,返回經(jīng)緯度;
- 添加獲取地理位置的方法getLocation,傳入經(jīng)緯度,返回地理位置;
- 添加顯示結(jié)果的方法showResult,傳入得到的信息在界面上顯示出來(lái)。
好了,先將方法添上,完整代碼如下:
package com.android.demo;
import android.R.bool;
import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class DemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** 為按鈕綁定事件 */
Button btnGetLocation = (Button)findViewById(R.id.button1);
btnGetLocation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
onBtnClick();
}
});
}
/** 基站信息結(jié)構(gòu)體 */
public class SCell{
public int MCC;
public int MNC;
public int LAC;
public int CID;
}
/** 經(jīng)緯度信息結(jié)構(gòu)體 */
public class SItude{
public String latitude;
public String longitude;
}
/** 按鈕點(diǎn)擊回調(diào)函數(shù) */
private void onBtnClick(){
}
/** 獲取基站信息 */
private SCell getCellInfo(){
}
/** 獲取經(jīng)緯度 */
private SItude getItude(SCell cell){
}
/** 獲取地理位置 */
private String getLocation(SItude itude){
}
/** 顯示結(jié)果 */
private void showResult(SCell cell, String location){
}
}
現(xiàn)在在onBtnClick方法中編碼,依次調(diào)用后面幾個(gè)方法,代碼如下:
/** 按鈕點(diǎn)擊回調(diào)函數(shù) */
private void onBtnClick(){
/** 彈出一個(gè)等待狀態(tài)的框 */
ProgressDialog mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("正在獲取中...");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.show();
try {
/** 獲取基站數(shù)據(jù) */
SCell cell = getCellInfo();
/** 根據(jù)基站數(shù)據(jù)獲取經(jīng)緯度 */
SItude itude = getItude(cell);
/** 獲取地理位置 */
String location = getLocation(itude);
/** 顯示結(jié)果 */
showResult(cell, location);
/** 關(guān)閉對(duì)話框 */
mProgressDialog.dismiss();
}catch (Exception e) {
/** 關(guān)閉對(duì)話框 */
mProgressDialog.dismiss();
/** 顯示錯(cuò)誤 */
TextView cellText = (TextView)findViewById(R.id.cellText);
cellText.setText(e.getMessage());
}
}
按鈕相關(guān)的工作就完成了,接下來(lái)編寫獲取基站信息的方法。
三、獲取基站信息
獲取基站信息我們需要調(diào)用SDK提供的API中的TelephonyManager,需要在文件頭部引入:
import android.telephony.TelephonyManager; import android.telephony.gsm.GsmCellLocation;
完整代碼為:
/**
* 獲取基站信息
*
* @throws Exception
*/
private SCell getCellInfo() throws Exception {
SCell cell = new SCell();
/** 調(diào)用API獲取基站信息 */
TelephonyManager mTelNet = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation location = (GsmCellLocation) mTelNet.getCellLocation();
if (location == null)
throw new Exception("獲取基站信息失敗");
String operator = mTelNet.getNetworkOperator();
int mcc = Integer.parseInt(operator.substring(0, 3));
int mnc = Integer.parseInt(operator.substring(3));
int cid = location.getCid();
int lac = location.getLac();
/** 將獲得的數(shù)據(jù)放到結(jié)構(gòu)體中 */
cell.MCC = mcc;
cell.MNC = mnc;
cell.LAC = lac;
cell.CID = cid;
return cell;
}
如果獲得的位置信息為null將拋出錯(cuò)誤,不再繼續(xù)執(zhí)行。最后將獲取的基站信息封裝為結(jié)構(gòu)體返回。
四、獲取經(jīng)緯度
在這一步,我們需要采用HTTP調(diào)用google的API以獲取基站所在的經(jīng)緯度。
Android作為一款互聯(lián)網(wǎng)手機(jī),聯(lián)網(wǎng)的功能必不可少。Android提供了多個(gè)接口供我們使用,這里我們使用DefaultHttpClient。
完整的方法代碼如下:
/**
* 獲取經(jīng)緯度
*
* @throws Exception
*/
private SItude getItude(SCell cell) throws Exception {
SItude itude = new SItude();
/** 采用Android默認(rèn)的HttpClient */
HttpClient client = new DefaultHttpClient();
/** 采用POST方法 */
HttpPost post = new HttpPost("http://www.google.com/loc/json");
try {
/** 構(gòu)造POST的JSON數(shù)據(jù) */
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", "gsm");
holder.put("carrier", "HTC");
JSONObject tower = new JSONObject();
tower.put("mobile_country_code", cell.MCC);
tower.put("mobile_network_code", cell.MNC);
tower.put("cell_id", cell.CID);
tower.put("location_area_code", cell.LAC);
JSONArray towerarray = new JSONArray();
towerarray.put(tower);
holder.put("cell_towers", towerarray);
StringEntity query = new StringEntity(holder.toString());
post.setEntity(query);
/** 發(fā)出POST數(shù)據(jù)并獲取返回?cái)?shù)據(jù) */
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
BufferedReader buffReader = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer strBuff = new StringBuffer();
String result = null;
while ((result = buffReader.readLine()) != null) {
strBuff.append(result);
}
/** 解析返回的JSON數(shù)據(jù)獲得經(jīng)緯度 */
JSONObject json = new JSONObject(strBuff.toString());
JSONObject subjosn = new JSONObject(json.getString("location"));
itude.latitude = subjosn.getString("latitude");
itude.longitude = subjosn.getString("longitude");
Log.i("Itude", itude.latitude + itude.longitude);
} catch (Exception e) {
Log.e(e.getMessage(), e.toString());
throw new Exception("獲取經(jīng)緯度出現(xiàn)錯(cuò)誤:"+e.getMessage());
} finally{
post.abort();
client = null;
}
return itude;
}
在這里采用POST方法將JSON數(shù)據(jù)發(fā)送到googleAPI,google返回JSON數(shù)據(jù),我們得到數(shù)據(jù)后解析,得到經(jīng)緯度信息。
五、獲取物理位置
得到經(jīng)緯度后,我們將之轉(zhuǎn)換為物理地址。
我們?nèi)匀皇褂肈efaultHttpClient來(lái)調(diào)用google地圖的API,獲得物理信息,不過(guò)在這里我們使用GET方法。
完整的方法代碼如下:
/**
* 獲取地理位置
*
* @throws Exception
*/
private String getLocation(SItude itude) throws Exception {
String resultString = "";
/** 這里采用get方法,直接將參數(shù)加到URL上 */
String urlString = String.format("http://maps.google.cn/maps/geo?key=abcdefg&q=%s,%s", itude.latitude, itude.longitude);
Log.i("URL", urlString);
/** 新建HttpClient */
HttpClient client = new DefaultHttpClient();
/** 采用GET方法 */
HttpGet get = new HttpGet(urlString);
try {
/** 發(fā)起GET請(qǐng)求并獲得返回?cái)?shù)據(jù) */
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
BufferedReader buffReader = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer strBuff = new StringBuffer();
String result = null;
while ((result = buffReader.readLine()) != null) {
strBuff.append(result);
}
resultString = strBuff.toString();
/** 解析JSON數(shù)據(jù),獲得物理地址 */
if (resultString != null && resultString.length() > 0) {
JSONObject jsonobject = new JSONObject(resultString);
JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark").toString());
resultString = "";
for (int i = 0; i < jsonArray.length(); i++) {
resultString = jsonArray.getJSONObject(i).getString("address");
}
}
} catch (Exception e) {
throw new Exception("獲取物理位置出現(xiàn)錯(cuò)誤:" + e.getMessage());
} finally {
get.abort();
client = null;
}
return resultString;
}
GET方法就比POST方法簡(jiǎn)單多了,得到的數(shù)據(jù)同樣為JSON格式,解析一下得到物理地址。
六、顯示結(jié)果
好了,我們已經(jīng)得到我們想要的信息了,我們把它顯示出來(lái),方法代碼如下:
/** 顯示結(jié)果 */
private void showResult(SCell cell, String location) {
TextView cellText = (TextView) findViewById(R.id.cellText);
cellText.setText(String.format("基站信息:mcc:%d, mnc:%d, lac:%d, cid:%d",
cell.MCC, cell.MNC, cell.LAC, cell.CID));
TextView locationText = (TextView) findViewById(R.id.lacationText);
locationText.setText("物理位置:" + location);
}
七、運(yùn)行程序
我們的編碼工作已經(jīng)完成了。在上面的代碼中有些地方需要的引入代碼沒有提到,下面把完整的代碼貼出來(lái):
package com.android.demo;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
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 android.app.Activity;
import android.app.ProgressDialog;
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;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class DemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** 為按鈕綁定事件 */
Button btnGetLocation = (Button) findViewById(R.id.button1);
btnGetLocation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
onBtnClick();
}
});
}
/** 基站信息結(jié)構(gòu)體 */
public class SCell{
public int MCC;
public int MNC;
public int LAC;
public int CID;
}
/** 經(jīng)緯度信息結(jié)構(gòu)體 */
public class SItude{
public String latitude;
public String longitude;
}
/** 按鈕點(diǎn)擊回調(diào)函數(shù) */
private void onBtnClick() {
/** 彈出一個(gè)等待狀態(tài)的框 */
ProgressDialog mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("正在獲取中...");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.show();
try {
/** 獲取基站數(shù)據(jù) */
SCell cell = getCellInfo();
/** 根據(jù)基站數(shù)據(jù)獲取經(jīng)緯度 */
SItude itude = getItude(cell);
/** 獲取地理位置 */
String location = getLocation(itude);
/** 顯示結(jié)果 */
showResult(cell, location);
/** 關(guān)閉對(duì)話框 */
mProgressDialog.dismiss();
} catch (Exception e) {
/** 關(guān)閉對(duì)話框 */
mProgressDialog.dismiss();
/** 顯示錯(cuò)誤 */
TextView cellText = (TextView) findViewById(R.id.cellText);
cellText.setText(e.getMessage());
Log.e("Error", e.getMessage());
}
}
/**
* 獲取基站信息
*
* @throws Exception
*/
private SCell getCellInfo() throws Exception {
SCell cell = new SCell();
/** 調(diào)用API獲取基站信息 */
TelephonyManager mTelNet = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation location = (GsmCellLocation) mTelNet.getCellLocation();
if (location == null)
throw new Exception("獲取基站信息失敗");
String operator = mTelNet.getNetworkOperator();
int mcc = Integer.parseInt(operator.substring(0, 3));
int mnc = Integer.parseInt(operator.substring(3));
int cid = location.getCid();
int lac = location.getLac();
/** 將獲得的數(shù)據(jù)放到結(jié)構(gòu)體中 */
cell.MCC = mcc;
cell.MNC = mnc;
cell.LAC = lac;
cell.CID = cid;
return cell;
}
/**
* 獲取經(jīng)緯度
*
* @throws Exception
*/
private SItude getItude(SCell cell) throws Exception {
SItude itude = new SItude();
/** 采用Android默認(rèn)的HttpClient */
HttpClient client = new DefaultHttpClient();
/** 采用POST方法 */
HttpPost post = new HttpPost("http://www.google.com/loc/json");
try {
/** 構(gòu)造POST的JSON數(shù)據(jù) */
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", "gsm");
holder.put("carrier", "HTC");
JSONObject tower = new JSONObject();
tower.put("mobile_country_code", cell.MCC);
tower.put("mobile_network_code", cell.MNC);
tower.put("cell_id", cell.CID);
tower.put("location_area_code", cell.LAC);
JSONArray towerarray = new JSONArray();
towerarray.put(tower);
holder.put("cell_towers", towerarray);
StringEntity query = new StringEntity(holder.toString());
post.setEntity(query);
/** 發(fā)出POST數(shù)據(jù)并獲取返回?cái)?shù)據(jù) */
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
BufferedReader buffReader = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer strBuff = new StringBuffer();
String result = null;
while ((result = buffReader.readLine()) != null) {
strBuff.append(result);
}
/** 解析返回的JSON數(shù)據(jù)獲得經(jīng)緯度 */
JSONObject json = new JSONObject(strBuff.toString());
JSONObject subjosn = new JSONObject(json.getString("location"));
itude.latitude = subjosn.getString("latitude");
itude.longitude = subjosn.getString("longitude");
Log.i("Itude", itude.latitude + itude.longitude);
} catch (Exception e) {
Log.e(e.getMessage(), e.toString());
throw new Exception("獲取經(jīng)緯度出現(xiàn)錯(cuò)誤:"+e.getMessage());
} finally{
post.abort();
client = null;
}
return itude;
}
/**
* 獲取地理位置
*
* @throws Exception
*/
private String getLocation(SItude itude) throws Exception {
String resultString = "";
/** 這里采用get方法,直接將參數(shù)加到URL上 */
String urlString = String.format("http://maps.google.cn/maps/geo?key=abcdefg&q=%s,%s", itude.latitude, itude.longitude);
Log.i("URL", urlString);
/** 新建HttpClient */
HttpClient client = new DefaultHttpClient();
/** 采用GET方法 */
HttpGet get = new HttpGet(urlString);
try {
/** 發(fā)起GET請(qǐng)求并獲得返回?cái)?shù)據(jù) */
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
BufferedReader buffReader = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer strBuff = new StringBuffer();
String result = null;
while ((result = buffReader.readLine()) != null) {
strBuff.append(result);
}
resultString = strBuff.toString();
/** 解析JSON數(shù)據(jù),獲得物理地址 */
if (resultString != null && resultString.length() > 0) {
JSONObject jsonobject = new JSONObject(resultString);
JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark").toString());
resultString = "";
for (int i = 0; i < jsonArray.length(); i++) {
resultString = jsonArray.getJSONObject(i).getString("address");
}
}
} catch (Exception e) {
throw new Exception("獲取物理位置出現(xiàn)錯(cuò)誤:" + e.getMessage());
} finally {
get.abort();
client = null;
}
return resultString;
}
/** 顯示結(jié)果 */
private void showResult(SCell cell, String location) {
TextView cellText = (TextView) findViewById(R.id.cellText);
cellText.setText(String.format("基站信息:mcc:%d, mnc:%d, lac:%d, cid:%d",
cell.MCC, cell.MNC, cell.LAC, cell.CID));
TextView locationText = (TextView) findViewById(R.id.lacationText);
locationText.setText("物理位置:" + location);
}
}
我們連上手機(jī)在手機(jī)上運(yùn)行程序看看。
不出意外的話程序運(yùn)行起來(lái)了,自動(dòng)跳轉(zhuǎn)到了主界面。點(diǎn)擊“Click Me”,出錯(cuò)了!

詳細(xì)的錯(cuò)誤信息為:Neither user 10078 nor current process has android.permission.ACCESS_COARSE_LOCATION.
原來(lái)是沒有權(quán)限,經(jīng)過(guò)前面的學(xué)習(xí),我們知道Android在應(yīng)用的安全上下了一番功夫,要用一些特殊功能必須先報(bào)告,安裝應(yīng)用的時(shí)候列給用戶看,必須要得到用戶的允許。這里我們用了獲取基站信息的功能,涉及到用戶的隱私了,所以我們必須申明一下。
打開AndroidManifest.xml配置文件,在里面添加相應(yīng)的配置信息:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
我們繼續(xù)把網(wǎng)絡(luò)連接的權(quán)限申明也加上:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
再編譯運(yùn)行看看(點(diǎn)擊“Click Me”后程序會(huì)卡住,等待一段時(shí)間才有反應(yīng),取決于網(wǎng)絡(luò)情況):

成功啦!
可能有的同學(xué)還是出現(xiàn)錯(cuò)誤,沒有成功:
█ 提示“www.google.com…”什么的錯(cuò)誤
請(qǐng)確認(rèn)你的手機(jī)能訪問互聯(lián)網(wǎng),調(diào)用google的API是必須聯(lián)網(wǎng)的。
█ 提示獲取不到基站信息
你確定你是在手機(jī)上測(cè)試的嗎?模擬器可不行哦?;蛘吣愕氖謾C(jī)使用的CMDA網(wǎng)絡(luò)?這個(gè)例子只支持GSM網(wǎng)絡(luò)…
█ 獲取不到經(jīng)緯度
很有可能你中獎(jiǎng)了,你所在的基站還沒納入google的數(shù)據(jù)庫(kù)…(話說(shuō)我之前也遇到過(guò),怎么查就是查不出經(jīng)緯度來(lái),返回?cái)?shù)據(jù)為空)
█ 獲取到的地理地址不正確
這個(gè)可能程序出錯(cuò)了,可能google出錯(cuò)了?
其實(shí)google map API返回的數(shù)據(jù)中還包含了很多其他信息,我們可以用來(lái)開發(fā)一些更有趣的功能,如制作我們專屬的地圖軟件、足跡記錄軟件等,充分發(fā)揮你的創(chuàng)造力:)
八、總結(jié)
這個(gè)程序基本實(shí)現(xiàn)了基站定位功能,但還有很多問題,如:點(diǎn)擊了按鈕后界面會(huì)卡?。ㄔL問網(wǎng)絡(luò)時(shí)阻塞了進(jìn)程)、未對(duì)異常進(jìn)一步處理、不兼容CMDA網(wǎng)絡(luò)等。
另外這個(gè)程序的精度也不夠,獲得的位置實(shí)際上是基站的物理位置,與人所在的位置還有一定差距。在城市里面,一般采用密集型的小功率基站,精度一般在幾百米范圍內(nèi),而在郊區(qū)常為大功率基站,密度很小,精度一般在幾千米以上。
想要取得更高的精度需要通過(guò)一些其他的算法來(lái)實(shí)現(xiàn),如果大家有興趣的話我們可以一起來(lái)研究一下,再專門寫篇筆記。
可見寫一段程序和做一個(gè)實(shí)際的產(chǎn)品是有很大差別的。
結(jié)尾
這一節(jié)基本實(shí)現(xiàn)了最簡(jiǎn)單的基站定位,只是作為學(xué)習(xí)的例子,遠(yuǎn)遠(yuǎn)達(dá)不到產(chǎn)品的要求,請(qǐng)大家見諒。
我們進(jìn)一步熟悉了JAVA編碼,之前沒怎么接觸JAVA看起來(lái)有點(diǎn)吃力的同學(xué)建議找點(diǎn)JAVA基礎(chǔ)的書來(lái)看看。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS在頁(yè)面銷毀時(shí)如何優(yōu)雅的cancel網(wǎng)絡(luò)請(qǐng)求詳解
這篇文章主要給大家介紹了關(guān)于iOS在頁(yè)面銷毀時(shí)如何優(yōu)雅的cancel網(wǎng)絡(luò)請(qǐng)求的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
iOS開發(fā)-實(shí)現(xiàn)大文件下載與斷點(diǎn)下載思路
本篇文章主要介紹了iOS開發(fā)-實(shí)現(xiàn)大文件下載與斷點(diǎn)下載思路,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
詳解IOS開發(fā)之實(shí)現(xiàn)App消息推送(最新)
這篇文章主要介紹了詳解IOS開發(fā)之實(shí)現(xiàn)App消息推送(最新),具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12
深入講解iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲(chǔ)方式
這篇文章主要介紹了iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲(chǔ)方式,包括plistXML屬性列表和NSKeydeArchiver歸檔兩個(gè)部分,需要的朋友可以參考下2015-12-12

