AndroidManifest.xml 增加 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

public class ditu 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)this.getSystemService(serviceName);
//查询条件
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);
//设置监听器,自动更新的最小时间为间隔1秒,最小位移变化超过5米
locationManager.requestLocationUpdates(provider, 1000, 5, 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)this.findViewById(R.id.myLocationText);
if(location!=null){
double lat=location.getLatitude();
double lng=location.getLongitude();
latLongString="维度:"+lat+"\n经度"+lng;
}else{
latLongString="无法获取地理信息";
}
myLocationText.setText("您所在位置为:"+latLongString);
}
}


哈哈,只要手机开了GPS,你就能随时定位自己的位置,可惜显示出来的是经纬度,如果结合google API,那就能显示在地图上了。

更多相关文章

  1. Android之地理信息服务·Android的定位服务
  2. Android百度地图之位置定位和附近查找代码简单实现 (上)
  3. 一个java android 时钟 非常精致!android 小创意
  4. google map my demo
  5. google map my demo
  6. android GPS 获取位置
  7. 00015-相对布局位置属性
  8. Android对话框的高级设置《二》设置对话框按钮的透明度和对话框
  9. Android之GPS定位网上详解总结

随机推荐

  1. Android开发两个例子:多点触控技术&桌面快
  2. Android中观察者模式(Observable)的理解
  3. Android原生页面Activity与React页面相互
  4. Android的图片缓存ImageCache
  5. Android开发学习笔记-关于Android的消息
  6. android 启动线程注意的问题
  7. Android之MVC模式的使用
  8. Android 关于ZXing的使用
  9. Android之调用其他程序
  10. Android(安卓)点击 APP 图标几秒才显示界