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功能,没来得及试验,感觉这段代码写得很好,有需要时再根据实际需要进一步优化

原文地址:http://www.eoeandroid.com/thread-56691-1-1.html

更多相关文章

  1. android GPS定位,定位城市称,经纬度
  2. [置顶] Android 通过经纬度获取地理位置信息
  3. Android GPS 获得 经纬度 并得到该 坐标 精确地址
  4. Android开发者已经度过了初级、中级,如何成为一个Android高手呢?
  5. Android获取经纬度,计算距离,方位角
  6. Android 根据城市获取经纬度 适配Android 7.0 、Android 8.0
  7. Android百度地图——在地图上标注已知GPS纬度经度值的一个或一组

随机推荐

  1. WEB页面工具语言XML带来的好处
  2. 介绍XML和HTML的区别是什么
  3. 认识xml的作用
  4. xml实现多渠道接入网站的构架的方法
  5. xml图像超链接的制作代码
  6. 详解xml型字符串解析时存在& < >符号时的
  7. 具体分析Pull方式解析XML的示例代码
  8. Microsoft.XMLHTTP对象介绍
  9. maven项目不编译xml文件的解决办法
  10. Jaxb2实现Bean与xml互转的示例代码详解