做了一些有关GPS的东西,需要记录的如下几点:
1、判断GPS是否是打开状态
public boolean isGpsOn()
{
LocationManager alm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if( alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
{
return true;
}
else
{
return false;
}
}
2、通过GPS得到位置
public void getLocation()
{
try
{
String serviceName = Context.LOCATION_SERVICE;
mLocationManager = (LocationManager)getSystemService(serviceName);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE); // 精度低
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
String provider = mLocationManager.getBestProvider(criteria, true); // 获取GPS信息
Location location = mLocationManager.getLastKnownLocation(provider); // 通过GPS获取位置
}
catch(Exception e)
{
e.printStackTrace();
}
}
try
{
TelephonyManager tm = (TelephonyManager)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)); // 国家吗,中国460
int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3, 5)); // 网络运营商码 00 01
// 开始拼装JSON查询字符串
JSONObject holder = null;
holder = new JSONObject();
holder.put("version", "1.1.0");
holder.put("host", "maps.google.com");
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);
String url = "http://www.google.com.hk/loc/json";
DefaultHttpClient client = HttpClientFactory.getInstance();
HttpPost post = new HttpPost(url);
StringEntity entity = new StringEntity(holder.toString());
post.setEntity(entity);
HttpResponse response = client.execute(post);
retCode = response.getStatusLine().getStatusCode();
String result = EntityUtils.toString(response.getEntity());
Log.i("OBD", "Google 返回结果: " + result);
if(result.equals("{}"))
{
return -1;
}
// gps = 3; gps缓存=2 cellid=1; google=0 ;文件缓存 -1;优先级
if( level<0 )
{
JSONObject jsonObject = new JSONObject(result);
String location = jsonObject.getString("location");
Log.i("OBD", "Google返回的地址为: " + location);
JSONObject jsonJWD = new JSONObject(location);
Log.i("OBD", "Google返回的 经度:" + jsonJWD.getString("longitude") + " 纬度: " + jsonJWD.getString("latitude"));
longitude = jsonJWD.getString("longitude");
latitude = jsonJWD.getString("latitude");
Log.i("OBD", "getTaskGoogle " + "longitude " +longitude + " latitude " + latitude);
String address = jsonJWD.getString("address");
JSONObject jsonAddress = new JSONObject(address);
if (address.contains("street_number")) {
Log.i("OBD", jsonAddress.get("city") + " "
+ jsonAddress.get("street") + " "
+ jsonAddress.get("street_number"));
String accuracy = jsonJWD.getString("accuracy");
Log.i("OBD", "accuracy: " + accuracy);
position = jsonAddress.get("city") + " " + jsonAddress.get("street")
+ " " + jsonAddress.get("street_number")
+ " 精确距离: " + accuracy
+ "米";
} else {
Log.i("OBD", jsonAddress.get("city") + " "
+ jsonAddress.get("street"));
String accuracy = jsonJWD.getString("accuracy");
Log.i("OBD", "accuracy: " + accuracy);
position = jsonAddress.get("city") + " " + jsonAddress.get("street")
+ " 精确距离: " + accuracy
+ "米";
}
}
else
{
Log.i("OBD", "已经获得更高级的定位,忽略掉Google_CellID定位");
return -1;
}
}
catch (UnsupportedEncodingException e) {
System.out.println(e);
} catch (ClientProtocolException e) {
System.out.println(e);
} catch (ParseException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e);
}
http://maps.googleapis.com/maps/api/geocode/json?latlng=39.983434,116.316547&sensor=true&language=zh-CN
参数latlng是上传的经纬度坐标,注意中间没有空格,先是纬度后是经度,
参数language是附加的上传语言类型,因为在PC上返回的是中文,在模拟器或真机上返回的是英文的,所以加上这个参数。
5、在2.2API下打开GPS
/**
* 打开或者关闭GPS开关,原理只是一个变量的两个状态,一次开启一次关闭
*/
public void openCloseGPS()
{
Intent myIntent = new Intent();
myIntent.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
myIntent.addCategory("android.intent.category.ALTERNATIVE");
myIntent.setData(Uri.parse("3"));
sendBroadcast(myIntent);
}