上圖是過手機來定位測試,如果用模擬器測試也是沒問題的喔,只是模擬器要記得按裝Google Play Service~另外如果要使用手機測試,請到收訊良好的地方,盡量不要在室內。
_
下面程式碼是Android 6.0棉花糖以後版本的寫法,要跟使用者取得權限,設計上來說變麻煩了...
加入權限:
activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.gps_example.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textview" />
</RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private TextView textView;
private LocationManager mLocationManager;
private LocationListener mLocationListener;
private Context context;
private double lat,lng;
private final int REQUEST=103;
public final String LM_GPS = LocationManager.GPS_PROVIDER;
public final String LM_NETWORK = LocationManager.NETWORK_PROVIDER;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textview);
mLocationManager = (LocationManager) getSystemService(context.LOCATION_SERVICE); //取得服務
mLocationListener = new MyLocationListener();
}
protected void onResume() {
if (mLocationManager == null) {
mLocationManager = (LocationManager) getSystemService(context.LOCATION_SERVICE);
mLocationListener = new MyLocationListener();
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//如果沒有取得權限的話,就跟使用者要求允許
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_NETWORK_STATE},REQUEST);
}else {
mLocationManager.requestLocationUpdates(LM_GPS, 2000, 5, mLocationListener); //每2秒,並且使用者移動5公尺時update一次
mLocationManager.requestLocationUpdates(LM_NETWORK, 2000, 5, mLocationListener);
}
setTitle("onResume....");
super.onResume();
}
protected void onPause(){
if (mLocationManager!=null){
mLocationManager.removeUpdates(mLocationListener);
mLocationManager=null;
setTitle("onPause...");
}
super.onPause();
}
private class MyLocationListener implements LocationListener{
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onLocationChanged(Location location) {
lat=location.getLatitude();
lng=location.getLongitude();
textView.setText("經度"+lat+"\n"+"緯度"+lng);
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
format.setTimeZone(android.icu.util.TimeZone.getTimeZone("GMT+08"));
setTitle("GPS位置已更新"+format.format(date));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
}
PS:此程式每2秒會監聽當使用者有移動5公尺時,會透過位置管理器調用requestLocationUpdates,並且當gps位置資訊被更新時會回乎onLocationChanged方法,取得使用者經緯度。
沒有留言:
張貼留言