代码如下:
/ TODO: 2017-06-20 吴凯 public class MainActivity extends AppCompatActivity { TextView txt_location; LocationManager locationManager; String location; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txt_location = (TextView) findViewById(R.id.position_location); //表示获取系统的哪一个服务 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //获取所有可用的额位置提供器 List<String> providerlist = locationManager.getProviders(true); if (providerlist.contains(LocationManager.GPS_PROVIDER)) { location = LocationManager.GPS_PROVIDER; } else if (providerlist.contains(LocationManager.NETWORK_PROVIDER)) { location = LocationManager.NETWORK_PROVIDER; } else { //当前没有可用的位置提供器 Toast.makeText(this, "当前没有可用的位置提供器", Toast.LENGTH_SHORT).show(); return; } if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } Location locationmessage = locationManager.getLastKnownLocation(location); if (locationmessage != null) { //显示当前设备位置信息 showlocation(locationmessage); } locationManager.requestLocationUpdates(location, 5000, 1, locationlistener); } @Override protected void onDestroy() { super.onDestroy(); if (locationManager != null) { //关闭程序时候监听器移除 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } locationManager.removeUpdates(locationlistener); } } LocationListener locationlistener = 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 locationmessage) { String currentposition = "经度 :" + locationmessage.getLatitude() + "\n" + "纬度 :" + locationmessage.getLongitude(); txt_location.setText(currentposition); } }
-------------------------===============================================================================================
上面是只显示经纬度,不过那普通人也是看不懂的呢,后来改进下为显示详细地址信息,利用到Google自己的api,给Google上传实时定位,进行网络数据处理,显示为准确地址如下
// TODO: 2017-06-21 对于经纬度进行反向编译为我们熟悉的路径 public static final int show_location = 0; private void showlocation(final Location locationmessage) { new Thread(new Runnable() { @Override public void run() { try { //组装反向地理编码接口地址 StringBuilder url = new StringBuilder(); url.append("http://maps.googleapis.com/maps/api/geocode/json?latlng="); url.append(locationmessage.getLatitude()).append(","); url.append(locationmessage.getLongitude()); url.append("&sensor=false"); HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url.toString()); Log.e("url", "====" + url.toString()); //在请求中指定语音,保证服务器返回中文数据 httpGet.addHeader("Accept-Language", "zh-CN"); HttpResponse httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200) { Log.e("222222222", "2222222222222222"); HttpEntity entity = httpResponse.getEntity(); String response = EntityUtils.toString(entity, "utf-8"); JSONObject jsonObject = new JSONObject(response); //获取results节点下的位置信息 JSONArray array_result = jsonObject.getJSONArray("results"); if (array_result.length() > 0) { Log.e("33333333333", "33333333333333333333"); JSONObject subobject = array_result.getJSONObject(0); //取出格式化后的位置信息 String address = subobject.getString("formatted_address"); Message message = new Message(); message.what = show_location; message.obj = address; handler.handleMessage(message); Log.e("message", "===" + message); } } } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } }).start(); } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case show_location: String currentposition = (String) msg.obj; Log.e("currentposion", "=====" + currentposition); txt_location.setText(currentposition); break; default: break; } } }; }