Android系统下Gps、Network、Gsm、Cdma四种方式得到经纬度坐标

//首先,创建一个对象来封装经度和纬度;
public class LocationInfo {
double lat;
double lon;

public LocationInfo() {

}

public LocationInfo(double lat, double lon) {
this.lat = lat;
this.lon = lon;

}

public double getLat() {
return lat;
}

public void setLat(double lat) {
this.lat = lat;
}

public double getLon() {
return lon;
}

public void setLon(double lon) {
this.lon = lon;
}

}

// 接下来就是主要代码了,请大家鉴定,有错误的地方可以提出,一起改进。
public class LocationGetter {
Context context;

public LocationGetter(Context context) {
this.context = context;

}

public static LocationInfo AllGet(Context context) {
LocationInfo loInfo = null;
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
loInfo = GpsGet(context);
if (loInfo == null) {
loInfo = NetworkGet(context);
if (loInfo == null) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int type = tm.getPhoneType();
if (type == TelephonyManager.PHONE_TYPE_GSM) {
loInfo = GsmGet(context);
} else if (type == TelephonyManager.PHONE_TYPE_CDMA) {
loInfo = CdmaGet(context);
}
}
}
} else {
loInfo = NetworkGet(context);
if (loInfo == null) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int type = tm.getPhoneType();
if (type == TelephonyManager.PHONE_TYPE_GSM) {
loInfo = GsmGet(context);
} else if (type == TelephonyManager.PHONE_TYPE_CDMA) {
loInfo = CdmaGet(context);
}
}
}

return loInfo;
}

/**
* 通过GPS获取经纬度 适用于Gps
*/
public static LocationInfo GpsGet(Context context) {

LocationInfo loInfo = null;
double lat = 0.0;
double lon = 0.0;
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

LocationListener locationListener = new LocationListener() {

// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

// Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {

}

// Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {

}

// 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
@Override
public void onLocationChanged(Location location) {

}

};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5f, locationListener);

Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lon = location.getLongitude();
loInfo = new LocationInfo(lat, lon);
}

return loInfo;
}

/**
* 通过网络获取经纬度 适用于wifi和GPRS
*/
public static LocationInfo NetworkGet(Context context) {

Log.e("tag", "NetWorkLocation");

LocationInfo loInfo = null;
double lat = 0.0;
double lon = 0.0;
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

LocationListener locationListener = new LocationListener() {

// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

// Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {

}

// Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {

}

// 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
@Override
public void onLocationChanged(Location location) {

}

};
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 5f, locationListener);

Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (location != null) {
Log.e("tag", "network!=null");
lat = location.getLatitude();
lon = location.getLongitude();
loInfo = new LocationInfo(lat, lon);
}

return loInfo;

}

/**
* 通过CDMA获取经纬度 适用于中国电信
*/
public static LocationInfo CdmaGet(Context context) {
LocationInfo loInfo = null;
double lat = 0.0;
double lon = 0.0;
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
// tm.getPhoneType()

CdmaCellLocation ccl = (CdmaCellLocation) tm.getCellLocation();

if (ccl != null) {
lat = (double) ccl.getBaseStationLatitude() / 14400;
lon = (double) ccl.getBaseStationLongitude() / 14400;
loInfo = new LocationInfo(lat, lon);
}

return loInfo;

}

/**
* 通过基站获取经纬度 适用于中国移动和联通 3g
*/

public static LocationInfo GsmGet(Context context) {
LocationInfo loInfo = null;
double lat = 0.0;
double lon = 0.0;

TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

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 {
Log.e("tag", "传送json  获取数据");

// 组装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();

Log.e("GsmGet", result);

while (result != null) {

sb.append(result);
result = br.readLine();
}
JSONObject jsonObject = new JSONObject(sb.toString());

JSONObject jsonObject1 = new JSONObject(jsonObject.getString("location"));

lat = Double.parseDouble(jsonObject1.getString("latitude"));
lon = Double.parseDouble(jsonObject1.getString("longitude"));

Log.e("json", lat + "");

loInfo = new LocationInfo(lat, lon);
} catch (Exception e) {
// TODO: handle exception
Log.e("tag", "gsm  出了异常");
}

return loInfo;
}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值