百度的key实在不明白怎么搞,好恶心。谷歌的解码经纬度地址只能返回到区一级别,剩下都是未知,蛋碎。不知道是我获取方法不对还是地图太老。写着玩玩的,代码有点乱。
package com.location; import java.io.IOException; import java.util.List; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.telephony.gsm.SmsManager; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends Activity { Button showGeroInfoBtn,showGcsInfoBtn,sendSmsBtn; TextView showGeroInfoText,showGcsInfoText; EditText emcNumEdiTex; private double latGps,lonGps,latNet,lonNet; Location locationGps,locationNet; double geoLat,geoLon; String geroAddress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showGeroInfoBtn = (Button) findViewById(R.id.showGeroInfoBtn); showGeroInfoText = (TextView) findViewById(R.id.showGeroInfoText); showGcsInfoBtn = (Button) findViewById(R.id.showGcsInfoBtn); showGcsInfoText = (TextView) findViewById(R.id.showGcsInfoText); sendSmsBtn = (Button) findViewById(R.id.sendSmsBtn); emcNumEdiTex = (EditText) findViewById(R.id.emcNumEdiTex); emcNumEdiTex.setText("18612275213"); showGcsInfoBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationGps = locationManager .getLastKnownLocation(locationManager.GPS_PROVIDER); locationNet = locationManager .getLastKnownLocation(locationManager.NETWORK_PROVIDER); LocationListener locationListenerGps = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub locationGps = location; latGps = location.getLatitude(); lonGps = location.getLongitude(); MainActivity.this.geoLat = latNet; MainActivity.this.geoLon = lonNet; Log.e("GGGGGGGGG", latGps + "_!!!___!!!_" + lonGps); showGcsInfoText.setText("经度:" + latGps + ";" + "纬度:" + lonGps + "Gps"); } }; LocationListener locationListenerNet = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub locationNet = location; latNet = location.getLatitude(); lonNet = location.getLongitude(); MainActivity.this.geoLat = latNet; MainActivity.this.geoLon = lonNet; Log.e("NNNNNNNNNN", latNet + "_!!!___!!!_" + lonNet); showGcsInfoText.setText("经度:" + latNet + ";" + "纬度:" + lonNet + "Net"); } }; locationManager.requestLocationUpdates( locationManager.GPS_PROVIDER, 1000, 0, locationListenerGps); // locationGps=locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER); locationManager.requestLocationUpdates( locationManager.NETWORK_PROVIDER, 1000, 0, locationListenerNet); // locationNet=locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER); if (locationGps == null && locationNet == null) { showGcsInfoText.setText("请确保net或者gps开启!"); } } }); showGeroInfoBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub GroAsyncTask groAsyncTask = new GroAsyncTask(); groAsyncTask.execute(); } }); sendSmsBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String sms = "此为定位求助短信,请即刻报警。我在:" + showGeroInfoText.getText().toString() + "—" + showGcsInfoText.getText().toString(); SmsManager smsManager = SmsManager.getDefault(); Log.e("SMSSMSSMS", emcNumEdiTex.getText().toString().trim()); if (sms.length() > 70) { //短信分条,不然超出长度会报错 List<String> smsList = smsManager.divideMessage(sms); for (String smsDivide : smsList) { smsManager .sendTextMessage(emcNumEdiTex.getText() .toString().trim(), null, smsDivide, null, null); } } else { smsManager.sendTextMessage(emcNumEdiTex.getText() .toString().trim(), null, sms, null, null); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } class GroAsyncTask extends AsyncTask<String, ProgressBar, String> { //根据经纬度查询地址,返回内容留null会报错,用未知代替 @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub Geocoder geocoder = new Geocoder(MainActivity.this); Log.e("GEA+GEO", geoLat + "---" + geoLon); try { List<Address> list = geocoder .getFromLocation(geoLat, geoLon, 2); for (int i = 0; i < list.size(); i++) { Address address = list.get(i); if (address.getPremises() != null) { if (address.getThoroughfare() != null) { if (address.getSubThoroughfare() != null) { geroAddress = address.getCountryName() + address.getAdminArea() + address.getLocality() + address.getSubLocality() + address.getPremises() + address.getThoroughfare() + address.getSubThoroughfare(); } else { geroAddress = address.getCountryName() + address.getAdminArea() + address.getLocality() + address.getSubLocality() + address.getPremises() + address.getThoroughfare() + "未知"; } } else { geroAddress = address.getCountryName() + address.getAdminArea() + address.getLocality() + address.getSubLocality() + address.getPremises() + "未知" + "未知"; } } else { geroAddress = address.getCountryName() + address.getAdminArea() + address.getLocality() + address.getSubLocality() + "未知" + "未知" + "未知"; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return geroAddress; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); showGeroInfoText.setText(result); } } }
地址查询需在已获得经纬度的情况下。
相关推荐
NULL 博文链接:https://yypdc.iteye.com/blog/2009070
异步更新location源码异步更新location源码异步更新location源码异步更新location源码异步更新location源码异步更新location源码异步更新location源码异步更新location源码
Android第一行代码源码 LocationTestAndroid第一行代码源码 LocationTestAndroid第一行代码源码 LocationTestAndroid第一行代码源码 LocationTestAndroid第一行代码源码 LocationTestAndroid第一行代码源码 ...
安卓Android源码——Location.zip
Android 定位Location举例6个打包 ,一共6个小例子
安卓Android源码——Location1014.zip
Android Framework Source Code , sdk19的源码,附关联方法: 打开Eclipse,随便新建一个Android项目,在此项目目录下面有个Android 4.4,点击展开遍出现了android.jar。右键此包---->Properties,在弹出的窗口中,...
NULL 博文链接:https://913.iteye.com/blog/1489637
Android第一行代码源码 LocationTest.rar
Android开发—通过Location获取Address,获取手机一些信息
android-location-service-源码.rar
Android应用源码之androidGPS及WIFI基站定位坐标源码.zip
android-location-tracker Android Simple Location Tracker is an Android library that helps you get user location with a object named LocationTracker Installation Add this to your build.gradle file ...
介绍android本地服务类的使用方法,附源码。
Android Fused Location Provider: on how to use fused location API in Android
Android锁屏无法继续定位问题 产生问题的原因: 手机锁屏后,Android系统为了省电以及减少CPU消耗,在一段时间后会将手机进入休眠状态。此时的服务以及线程等都会停止。 最近就这个问题,阅读了很多代码以及官方文档...
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> **1.获取位置管理器对象LocationManager** ``` import android.location.LocationManager; LocationManager lm; // lm =...
Android GPS 和 Location Manager
Android录音程序源码.zip