获得当前经纬度
16lz
2022-07-30
直接上代码:注意添加权限<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class testGps extends Activity {
/** Called when the activity is first created. */
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LocationManager mLocationManager=(LocationManager)
getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 0, new LocationListener() {
public void onLocationChanged(Location location) { //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
// log it when the location changes
if (location != null) {
Log.i("SuperMap", "Location changed : Lat: "
+ location.getLatitude() + " Lng: "
+ location.getLongitude());
}
}
public void onProviderDisabled(String provider) {
// Provider被disable时触发此函数,比如GPS被关闭
}
public void onProviderEnabled(String provider) {
// Provider被enable时触发此函数,比如GPS被打开
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
}
} );
Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
tv=new TextView(this);
if(location!=null){
double latitude = location.getLatitude(); //经度
double longitude = location.getLongitude(); //纬度
double altitude = location.getAltitude(); //海拔
tv.setText(latitude+" "+longitude+" "+altitude);
}
// tv.setText("dsds");
setContentView(tv);
}
}
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class testGps extends Activity {
/** Called when the activity is first created. */
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LocationManager mLocationManager=(LocationManager)
getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 0, new LocationListener() {
public void onLocationChanged(Location location) { //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
// log it when the location changes
if (location != null) {
Log.i("SuperMap", "Location changed : Lat: "
+ location.getLatitude() + " Lng: "
+ location.getLongitude());
}
}
public void onProviderDisabled(String provider) {
// Provider被disable时触发此函数,比如GPS被关闭
}
public void onProviderEnabled(String provider) {
// Provider被enable时触发此函数,比如GPS被打开
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
}
} );
Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
tv=new TextView(this);
if(location!=null){
double latitude = location.getLatitude(); //经度
double longitude = location.getLongitude(); //纬度
double altitude = location.getAltitude(); //海拔
tv.setText(latitude+" "+longitude+" "+altitude);
}
// tv.setText("dsds");
setContentView(tv);
}
}
更多相关文章
- 箭头函数的基础使用
- Python技巧匿名函数、回调函数和高阶函数
- android上一些方法的区别和用法的注意事项
- 浅析android通过jni控制service服务程序的简易流程
- Android(安卓)bluetooth介绍(四): a2dp connect流程分析
- Android架构分析之使用自定义硬件抽象层(HAL)模块
- Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明
- android上一些方法的区别和用法的注意事项
- Android(安卓)中文 API (27) —— SeekBar.OnSeekBarChangeListene