gpt4 book ai didi

android - GSM 位置到坐标

转载 作者:行者123 更新时间:2023-11-30 02:32:55 26 4
gpt4 key购买 nike

我有一个设备可以向我发送它的位置。它通常发送从 GPS 获得的坐标,但如果 GPS 信号不好,它会进行 GSM 三角测量并向我发送 MCC、MNC、LAC 和 CELLID 值。

有了这4个值,我需要获取位置坐标(纬度,经度)。为此,我尝试使用谷歌的 GLM 隐藏服务(http://www.google.com/glm/mmap)。假设将这 4 个值传递给该服务,它会返回纬度/经度坐标,但网上关于该服务的信息不多。

根据重新收集的信息,我编写了一个执行此操作的小应用程序,但无法正常工作。这是 Locgact 显示的内容:

11-21 13:02:29.692: W/IInputConnectionWrapper(11990): endBatchEdit on inactive InputConnection
11-21 13:02:31.264: W/IInputConnectionWrapper(11990): endBatchEdit on inactive InputConnection
11-21 13:02:43.506: W/System.err(11990): android.os.NetworkOnMainThreadException
11-21 13:02:43.506: W/System.err(11990): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
11-21 13:02:43.506: W/System.err(11990): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-21 13:02:43.506: W/System.err(11990): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-21 13:02:43.506: W/System.err(11990): at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-21 13:02:43.506: W/System.err(11990): at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
11-21 13:02:43.506: W/System.err(11990): at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
11-21 13:02:43.506: W/System.err(11990): at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
11-21 13:02:43.506: W/System.err(11990): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
11-21 13:02:43.516: W/System.err(11990): at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
11-21 13:02:43.516: W/System.err(11990): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
11-21 13:02:43.516: W/System.err(11990): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
11-21 13:02:43.516: W/System.err(11990): at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
11-21 13:02:43.516: W/System.err(11990): at com.gsmlocation.MainActivity.transform(MainActivity.java:114)
11-21 13:02:43.516: W/System.err(11990): at com.gsmlocation.MainActivity$1.onClick(MainActivity.java:80)
11-21 13:02:43.516: W/System.err(11990): at android.view.View.performClick(View.java:4445)
11-21 13:02:43.526: W/System.err(11990): at android.view.View$PerformClick.run(View.java:18446)
11-21 13:02:43.526: W/System.err(11990): at android.os.Handler.handleCallback(Handler.java:733)
11-21 13:02:43.526: W/System.err(11990): at android.os.Handler.dispatchMessage(Handler.java:95)
11-21 13:02:43.526: W/System.err(11990): at android.os.Looper.loop(Looper.java:136)
11-21 13:02:43.526: W/System.err(11990): at android.app.ActivityThread.main(ActivityThread.java:5146)
11-21 13:02:43.536: W/System.err(11990): at java.lang.reflect.Method.invokeNative(Native Method)
11-21 13:02:43.536: W/System.err(11990): at java.lang.reflect.Method.invoke(Method.java:515)
11-21 13:02:43.536: W/System.err(11990): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
11-21 13:02:43.536: W/System.err(11990): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
11-21 13:02:43.536: W/System.err(11990): at dalvik.system.NativeStart.main(Native Method)

这是我的代码:

public Location transform(int cellID, int lac, int mcc, int mnc) {

try {
URL providerAddress = new URL("http://www.google.com/glm/mmap");
HttpURLConnection connection = (HttpURLConnection) providerAddress.openConnection();

connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.connect();

OutputStream outputStream = connection.getOutputStream();
writePlainData(outputStream, cellID, lac, mcc, mnc);

InputStream inputStream = connection.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);

dataInputStream.readShort();
dataInputStream.readByte();

int code = dataInputStream.readInt();
if (code == 0) {
double lat = dataInputStream.readInt() / 1000000D;
double lon = dataInputStream.readInt() / 1000000D;

coordinates.setLatitude(lat);
coordinates.setLongitude(lon);

return coordinates;
} else {

return null;
}

} catch (Exception e) {
e.printStackTrace();
}
return null;
}



protected void writePlainData(OutputStream out, int cellID, int lac, int mcc, int mnc) throws IOException {
DataOutputStream dos = new DataOutputStream(out);

dos.writeShort(0x0E); // Fct code

dos.writeInt(0); // requesting 8 byte session
dos.writeInt(0);

dos.writeShort(0); // country code string
dos.writeShort(0); // client descriptor string
dos.writeShort(0); // version tag string

dos.writeByte(0x1B); // Fct code

dos.writeInt(0); // MNC?
dos.writeInt(0); // MCC?
dos.writeInt(3); // Radio Access Type (3=GSM, 5=UMTS)

dos.writeShort(0); // length of provider name

// provider name string
dos.writeInt(cellID); // CID
dos.writeInt(lac); // LAC
dos.writeInt(mnc); // MNC
dos.writeInt(mcc); // MCC
dos.writeInt(-1); // always -1
dos.writeInt(0); // rx level

dos.flush();
}

LocCat 指向的线是:

connection.connect();

最佳答案

你遇到了 android.os.NetworkOnMainThreadException 所以在 AsyncTask 中运行你的代码

 class LoadingLocation extends AsyncTask<Void, Void, Void>
{

@Override
protected Void doInBackground(Void... params)
{
public Location transform(int cellID, int lac, int mcc, int mnc) {

try {
URL providerAddress = new URL("http://www.google.com/glm/mmap");
HttpURLConnection connection = (HttpURLConnection) providerAddress.openConnection();

connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.connect();

OutputStream outputStream = connection.getOutputStream();
writePlainData(outputStream, cellID, lac, mcc, mnc);

InputStream inputStream = connection.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);

dataInputStream.readShort();
dataInputStream.readByte();

int code = dataInputStream.readInt();
if (code == 0) {
double lat = dataInputStream.readInt() / 1000000D;
double lon = dataInputStream.readInt() / 1000000D;

coordinates.setLatitude(lat);
coordinates.setLongitude(lon);

return coordinates;
} else {

return null;
}

} catch (Exception e) {
e.printStackTrace();
}
return null;
}
return null;
}

}

关于android - GSM 位置到坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27061058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com