Android中的三角测量接收到错误位置
这有点宽泛,但我们开始吧:我的三角剖分算法出现了一个非常奇怪的问题。有时,它返回正确的纬度/经度,然后在一段时间后返回错误的纬度/经度。更奇怪的是,我无法预测这个错误何时会发生,也无法重现它。即使我没有更改代码中的行(接收正确的值,然后是错误的,然后是正确的,等等),也会发生这种情况 我正在使用谷歌GLM服务,将设备LAC(位置区号)和塔台ID发送到我的位置。我的算法的核心方法如下:Android中的三角测量接收到错误位置,android,geolocation,triangulation,Android,Geolocation,Triangulation,这有点宽泛,但我们开始吧:我的三角剖分算法出现了一个非常奇怪的问题。有时,它返回正确的纬度/经度,然后在一段时间后返回错误的纬度/经度。更奇怪的是,我无法预测这个错误何时会发生,也无法重现它。即使我没有更改代码中的行(接收正确的值,然后是错误的,然后是正确的,等等),也会发生这种情况 我正在使用谷歌GLM服务,将设备LAC(位置区号)和塔台ID发送到我的位置。我的算法的核心方法如下: private double[] getPositionByTriangle(int lac, int cid)
private double[] getPositionByTriangle(int lac, int cid) {
int shortcid = cid & 0xffff;
double location[] = new double[2];
try {
String surl = "http://www.google.com/glm/mmap";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(surl);
httppost.setEntity(new CellIDRequestEntity(shortcid, lac));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
DataInputStream dis = new DataInputStream(entity.getContent());
// Read some prior data
dis.readShort();
dis.readByte();
// Read the error-code
int errorCode = dis.readInt();
if (errorCode == 0) {
location[0] = (double) dis.readInt() / 1000000D;
location[1] = (double) dis.readInt() / 1000000D;
}
} catch (Exception e) {}
return location;
}
额外信息,这可能会有所帮助:此方法用于扩展Android服务的类,通过PendingEvent延迟1分钟调用。调用它的线程将lat/lon值保存在SharedReferences中,然后我在所有视图中使用它
我想知道我是否用错误的算法实现了这个方法,或者在这个过程中是否有我错过的技巧。目前,我正确的近似纬度/经度值为纬度=-23和经度=-46,但我(有时)收到纬度=17和经度=81的值。有人能告诉我发生了什么事吗?好的。我自己找到了解决办法。我们开始吧。 三角测量过程基本上是一个三参数组合过程。根据我所做的一些搜索,我推断可以使用不同类型的数据组合。我使用的组合是塔ID(设备握手的塔收发器)、位置代码(区号和国家代码)和位置区号的混合。因此,我的新代码如下所示: 有了这段新代码,请注意我使用的是Google Location JSON服务,它返回一个JSON对象。我应该说,这是一个非常棘手的问题,我需要搜索更多,以了解所有的机械,但至少,这是一个工作代码。希望它能帮助别人
更新:我还想在以下上下文中完成此回答:三角测量使用通过移动网络(GSM、UMTS、LTE等)的无线电接入。我现在很确定这些价值波动来自无线电波对环境(墙壁、建筑物、相邻的塔等)的干扰这一事实。我自己找到了解决办法。我们开始吧。 三角测量过程基本上是一个三参数组合过程。根据我所做的一些搜索,我推断可以使用不同类型的数据组合。我使用的组合是塔ID(设备握手的塔收发器)、位置代码(区号和国家代码)和位置区号的混合。因此,我的新代码如下所示: 有了这段新代码,请注意我使用的是Google Location JSON服务,它返回一个JSON对象。我应该说,这是一个非常棘手的问题,我需要搜索更多,以了解所有的机械,但至少,这是一个工作代码。希望它能帮助别人 更新:我还想在以下上下文中完成此回答:三角测量使用通过移动网络(GSM、UMTS、LTE等)的无线电接入。我现在非常确定,这些价值波动来自无线电波对环境(墙壁、建筑物、相邻的塔楼等)的干扰。
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation.requestLocationUpdate();
location = (GsmCellLocation) tm.getCellLocation();
networkOperator = tm.getNetworkOperator();
cellID = location.getCid();
lac = location.getLac();
public double[] getLocationByMCCMNC(String mcc, String mnc, String lac, String cellId, boolean requestAddress) throws IOException {
final String data = "{\"cell_towers\": [{\"location_area_code\": \""
+ lac + "\", \"mobile_network_code\": \"" + mnc
+ "\", \"cell_id\": \"" + cellId
+ "\", \"mobile_country_code\": \"" + mcc
+ "\"}], \"version\": \"1.1.0\", \"request_address\": \""
+ requestAddress + "\"}";
URL anUrl = new URL("https://www.google.com/loc/json");
HttpURLConnection conn = (HttpURLConnection) anUrl.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.setRequestProperty("Content-type", "application/jsonrequest");
OutputStream out = conn.getOutputStream();
out.write(data.getBytes());
out.close();
InputStream in = conn.getInputStream();
StringBuilder builder = new StringBuilder();
Reader reader = new InputStreamReader(in);
char[] buf = new char[1024];
int read = 0;
while ((read = reader.read(buf)) >= 0) {
builder.append(String.valueOf(buf, 0, read));
}
in.close();
conn.disconnect();
double location[] = new double[2];
try {
JSONObject loc = new JSONObject(builder.toString()).getJSONObject("location");
location[0] = Double.valueOf(loc.getDouble("latitude"));
location[1] = Double.valueOf(loc.getDouble("longitude"));
} catch (JSONException e) {
location[0] = -1;
location[1] = -1;
}
return location;
}