2012-12-13经过测试,貌似不能通过基站号获得经纬度了。
http://www.google.com/loc/json
网站出现404不能用了,暂时还没有想到怎样通过基站号获得经纬度
开始收费了:
https://developers.google.com/maps/documentation/business/geolocation/
下面是我查的的资料:
经过几天的调研以及测试,终于解决了联通2G、移动2G、电信3G的基站定位代码。团队里面只有这些机器的制式了。下面就由我来做一个详细的讲解吧。
1 相关技术内容Google Android Api里面的TelephonyManager的管理。
联通、移动、电信不同制式在获取基站位置的代码区别。
通过基站的基本信息,通过Google Gears获取对应的GPS经纬度。
通过Google Map API根据GPS经纬度获取当前位置。
2 目前存在的几个问题
由于得到的GPS经纬度在Google Map上面显示需要偏移,这块暂时没有进行处理。
没有使用PhoneStateListener来对状态实时进行更新。
没有使用线程异步获取数据
没有使用服务的方式来实时获取数据
所以如果是商业使用的话,还需进一步修改。
3 当然本部分代码已经移植到我们的家庭卫士的项目中了,2提到的问题全部解决了。
下面我针对第一部分的四大内容进行代码注解。
1 Google Android Api里面的TelephonyManager的管理。
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
通过这个方式就可以得到TelephonyManager接口。这个接口的源代码可以通过设置在项目里面查看,这里不具体附上了。得到TelephonyManager后,由于针对不同的运营商,代码有所不同,所以需要判断getNetworkType()在源代码里面有如下的类型定义
/** Network type is unknown */
public static final int NETWORK_TYPE_UNKNOWN = 0;
/** Current network is GPRS */
public static final int NETWORK_TYPE_GPRS = 1;
/** Current network is EDGE */
public static final int NETWORK_TYPE_EDGE = 2;
/** Current network is UMTS */
public static final int NETWORK_TYPE_UMTS = 3;
/** Current network is CDMA: Either IS95A or IS95B*/
public static final int NETWORK_TYPE_CDMA = 4;
/** Current network is EVDO revision 0*/
public static final int NETWORK_TYPE_EVDO_0 = 5;
/** Current network is EVDO revision A*/
public static final int NETWORK_TYPE_EVDO_A = 6;
/** Current network is 1xRTT*/
public static final int NETWORK_TYPE_1xRTT = 7;
/** Current network is HSDPA */
public static final int NETWORK_TYPE_HSDPA = 8;
/** Current network is HSUPA */
public static final int NETWORK_TYPE_HSUPA = 9;
/** Current network is HSPA */
public static final int NETWORK_TYPE_HSPA = 10;
2 联通、移动、电信不同制式在获取基站位置的代码区别。
这部分是我实际测试出来的,经过无数次的拆机,放卡,才实现了不同制式的完美实现。
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int type = tm.getNetworkType();
//中国电信为CTC
//NETWORK_TYPE_EVDO_A是中国电信3G的getNetworkType
//NETWORK_TYPE_CDMA电信2G是CDMA
if (type == TelephonyManager.NETWORK_TYPE_EVDO_A || type == TelephonyManager.NETWORK_TYPE_CDMA || type ==TelephonyManager.NETWORK_TYPE_1xRTT)
{
}
//移动2G卡 + CMCC + 2
//type = NETWORK_TYPE_EDGE
else if(type == TelephonyManager.NETWORK_TYPE_EDGE)
{
}
//联通的2G经过测试 China Unicom 1 NETWORK_TYPE_GPRS
else if(type == TelephonyManager.NETWORK_TYPE_GPRS)
{
}
else
{
tv.setText("Current Not Support This Type.");
}
完成代码:
package cn.LocationStation;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class LocationStation extends Activity {
TextView mTextView;
Button mButton;
TelephonyManager tm;
/*<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.PERMISSION_NAME"></uses-permission>
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.textView001);
mButton = (Button) findViewById(R.id.Button001);
tm = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
mButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
int cid = gcl.getCid();
int lac = gcl.getLac();
int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0,
3));
int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3,
5));
try {
// 组装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);
JSONArray array = new JSONArray();
JSONObject data = new JSONObject();
data.put("cell_id", cid); // 25070
data.put("location_area_code", lac);// 4474
data.put("mobile_country_code", mcc);// 460
data.put("mobile_network_code", mnc);// 0
array.put(data);
holder.put("cell_towers", array);
// 创建连接,发送请求并接受回应
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"http://www.google.com/loc/json");
StringEntity se = new StringEntity(holder.toString());
post.setEntity(se);
HttpResponse resp = client.execute(post);
HttpEntity entity = resp.getEntity();
BufferedReader br = new BufferedReader(
new InputStreamReader(entity.getContent()));
StringBuffer sb = new StringBuffer();
String result = br.readLine();
while (result != null) {
sb.append(result);
result = br.readLine();
}
JSONObject jsonObject = new JSONObject(sb.toString());
JSONObject jsonObject1 = new JSONObject(jsonObject
.getString("location"));
getAddress(jsonObject1.getString("latitude"), jsonObject1
.getString("longitude"));
//mTextView.setText(sb.toString());
} catch (Exception e) {
// TODO: handle exception
}
}
});
}
void getAddress(String lat, String lag) {
try {
URL url = new URL("http://maps.google.cn/maps/geo?key=abcdefg&q="
+ lat + "," + lag);
InputStream inputStream = url.openConnection().getInputStream();
InputStreamReader inputReader = new InputStreamReader(inputStream,
"utf-8");
BufferedReader bufReader = new BufferedReader(inputReader);
String line = "", lines = "";
while ((line = bufReader.readLine()) != null) {
lines += line;
}
if (!lines.equals("")) {
JSONObject jsonobject = new JSONObject(lines);
JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark")
.toString());
for (int i = 0; i < jsonArray.length(); i++) {
mTextView.setText(mTextView.getText() + "\n"
+ jsonArray.getJSONObject(i).getString("address"));
}
}
} catch (Exception e) {
;
}
}
}