/**
* 根据wifi获取当前位置
* @param context
*/
public void getCurrentLocationWifi(Context context) {
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
location = lmanager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
/**
* 根据gps获得当前位置
*/
public void getCurrentLocationGPS() {
String locationProvider = LocationManager.GPS_PROVIDER;
location = lmanager.getLastKnownLocation(locationProvider);
}
/**
* 根据基站获取当前的位置
*/
public void getCurrentLocationAGPS() {
TelephonyManager tmanager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (tmanager.getCellLocation() == null) {
return;
}
GsmCellLocation gcl = (GsmCellLocation) tmanager.getCellLocation();
int cid = gcl.getCid();
int lac = gcl.getLac();
String oper=tmanager.getNetworkOperator();
int mcc = 0;
int mnc = 0;
if(!oper.equals("")){
mcc = Integer.valueOf(oper.substring(0,3));
mnc = Integer.valueOf(oper.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 resultStr = new StringBuffer();
String readLine = null;
while ((readLine = br.readLine()) != null) {
resultStr.append(readLine);
}
JSONObject jsonResult = new JSONObject(resultStr.toString());
JSONObject jsonLocation = jsonResult.getJSONObject("location");
double jsonLat = jsonLocation.getDouble("latitude");
double jsonLon = jsonLocation.getDouble("longitude");
location = new Location("AGPS");
location.setLatitude(jsonLat);
location.setLongitude(jsonLon);
} catch (Exception e) {
Log.i("life", "location get failed");
}
}