一.使用之前的简单封装的OkHttpUtil
http://www.cnblogs.com/itfenqing/p/6758909.html
二.关键代码:
public class MainActivity extends Activity { private TextView tip; private LocationManager locationManager; private String provider; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tip = (TextView)findViewById(R.id.tip); locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); // 获取所有可用的地理位置提供器 List<String> providerList = locationManager.getProviders(true); if( providerList.contains(locationManager.GPS_PROVIDER) ){ provider = locationManager.GPS_PROVIDER; }else if( providerList.contains(locationManager.NETWORK_PROVIDER) ){ provider = locationManager.NETWORK_PROVIDER; }else{ Toast.makeText(this, "没有地理位置提供器", Toast.LENGTH_LONG).show(); return; } Location location = null; try { location = locationManager.getLastKnownLocation(provider); }catch (SecurityException e){ e.printStackTrace(); } if(location != null){ showLocation(location); } try{ // 设置一个监听器,每1秒检测一次,位置移动超过1米,则更新 locationManager.requestLocationUpdates(provider, 1000, 1, listener); }catch (SecurityException e){ e.printStackTrace(); } } // 监听器 LocationListener listener = new LocationListener() { @Override public void onLocationChanged(Location location) { showLocation(location); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; private void showLocation(Location location){ String url = "http://api.map.baidu.com/geocoder/v2/?ak=nUvlnh8wk6cDupYs5foacyXz&" + "location="+location.getLatitude()+"," +location.getLongitude()+"&output=json&pois=1"; // 异步获取 OkHttpUtil.AsyncGet(url, new HttpListener() { @Override public void onFinish(String reponse) { parseJson(reponse); } @Override public void onError(Exception e) { e.printStackTrace(); } }); //String tipStr = "纬度:" + location.getLatitude() + ",经度:" + location.getLongitude(); //tip.setText(tipStr); } // parseJson private void parseJson(String jsonStr){ try { final StringBuilder info = new StringBuilder(); JSONObject json = new JSONObject(jsonStr); if( json.get("status").toString().equals("0") ){ info.append("经度" + json.getJSONObject("result") .getJSONObject("location").get("lng").toString()); info.append(",纬度" + json.getJSONObject("result") .getJSONObject("location").get("lat").toString()); info.append(",地理位置" + json.getJSONObject("result") .get("formatted_address").toString()); runOnUiThread(new Runnable() { @Override public void run() { tip.setText(info.toString()); } }); } }catch (Exception e){ e.printStackTrace(); } } @Override protected void onDestroy() { super.onDestroy(); // 取消监听 if( locationManager != null ){ try { locationManager.removeUpdates(listener); }catch (SecurityException e){ e.printStackTrace(); } } } }
三.所需权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" />
时间: 2024-10-17 18:16:41