package cn.xx.currentlocation;import java.io.IOException;import java.util.List;import java.util.Locale;import android.app.Activity;import android.content.Context;import android.location.Address;import android.location.Criteria;import android.location.Geocoder;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.widget.TextView;import android.widget.Toast;public class CurrentLocation extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LocationManager locationManager; String serviceName = Context.LOCATION_SERVICE; locationManager = (LocationManager) getSystemService(serviceName);// String provider = LocationManager.GPS_PROVIDER;// String provider = "gps"; Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); String provider = locationManager.getBestProvider(criteria, true); Location location = locationManager.getLastKnownLocation(provider); updateWithNewLocation(location); locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); } private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateWithNewLocation(location); } public void onProviderDisabled(String provider) { updateWithNewLocation(null); } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider, int status, Bundle extras) { } }; private void updateWithNewLocation(Location location) { String latLongString; TextView myLocationText; myLocationText = (TextView) findViewById(R.id.myLocationText);// try {// Thread.sleep(0);//因为真机获取gps数据需要一定的时间,为了保证获取到,采取系统休眠的延迟方法// } catch (InterruptedException e) {// e.printStackTrace();// throw new RuntimeException(e);// } if (location != null) { double lat = location.getLatitude(); double lng = location.getLongitude(); Geocoder geocoder=new Geocoder(this); // Geocoder geocoder = new Geocoder(this, Locale.CHINA); List places = null; try {// Thread.sleep(2000); places = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 5);// Thread.sleep(2000); Toast.makeText(CurrentLocation.this, places.size()+"", Toast.LENGTH_LONG).show(); System.out.println(places.size()+""); } catch (Exception e) { e.printStackTrace(); } String placename = ""; if (places != null && places.size() > 0) { // placename=((Address)places.get(0)).getLocality(); //一下的信息将会具体到某条街 //其中getAddressLine(0)表示国家,getAddressLine(1)表示精确到某个区,getAddressLine(2)表示精确到具体的街 placename = ((Address) places.get(0)).getAddressLine(0) + ", " + System.getProperty("line.separator") + ((Address) places.get(0)).getAddressLine(1) + ", " + ((Address) places.get(0)).getAddressLine(2); } latLongString = "纬度:" + lat + "/n经度:" + lng; Toast.makeText(CurrentLocation.this, placename, Toast.LENGTH_LONG).show(); } else { latLongString = "无法获取地理信息"; } myLocationText.setText("您当前的位置是:/n" + latLongString); }} 又有其他项目,还没着手这个GPS功能,没来得及试
详细解决方案
Android GPS 取得 经纬度 并得到该 坐标 精确地址
热度:322 发布时间:2016-05-01 20:48:20.0
相关解决方案
- android 读取byte[]中的元素解决方案
- android 标题栏兑现方式
- android 中Activity向BroadcastReceiver发送数据,该怎么解决
- Android 4.0 为什么模拟器老是提示小弟我谷歌拼音输入法已停止
- android:getSharedPreferences() 这是哪个类的方法解决思路
- android 怎么判断一个程序是否联网
- android RadioButton如何设置默认选中
- android 怎么重新设置锚点
- android 图片对象获取的有关问题
- android 关于服务连接的疑义
- android 怎么实现对view的放大和缩小
- android ID,该如何处理
- 准备复习2-3个月,看java+android,请问有经验者,怎么看效果最好》
- android UI线程与AsyncTask的有关问题
- android(java) 中文乱码的有关问题
- android 动态设立控件高度
- Android test project 编译方法
- android 4.03启动出错,显示"Encryption Unsuccessful"该如何解决
- Android 下面的listView的动态效果怎么实现的?求源代码,多谢
- Android?Palm?Symbian?Windows Mobile?学哪个有“钱图”?解决方案
- Android 开发语言,该怎么解决
- 单个人去做手机开发选什么平台好?android,iphone?解决办法
- Android 虚拟机崩溃的有关问题
- 想学学移动平台的开发 android 还是windows phone7好啊该怎么处理
- Android 上的如何读写设备文件
- Android 2.2 API 汉语文档系列(3) —— AccessibilityService
- 【先睹为快】ArcGIS Android SDK上一版本新特性
- ArcGIS for Android app v2.0.4公布
- android.hardware.USB种介绍
- android linux开发 在线电子书解决方案