这是我班级的代码。文本视图位于活动中,我的目标只是在文本视图中显示纬度和经度。现在,当我运行应用程序时,看起来没有任何事情发生。文本视图只显示" Textview"。
public class MBTATracker extends Activity {
public LocationManager locationManager;
public LocationListener locationListener;
public double latittudenumber;
public double longitudenumber;
TextView lat;
TextView longitude;
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mbta_tracker);
lat = (TextView) findViewById(R.id.latitude_label);
longitude = (TextView) findViewById(R.id.longitude_label);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
latittudenumber = location.getLatitude();
longitudenumber = location.getLongitude();
lat.setText(Double.toString(latittudenumber));
longitude.setText(Double.toString(longitudenumber));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent (Settings.ACTION_LOCALE_SETTINGS);
startActivity (intent);
}
};
locationManager.requestLocationUpdates("gps", 5000, 0,locationListener);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.INTERNET
}, 10);
return;
}
} else {
}
}
public void onRequestPermissionResult (int requestCode, String [] permissions,int [] grantResults){
switch (requestCode) {
case 10:
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED);
return;
}
}
}
以下是XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/latitude_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/local_mbta_list"
android:layout_marginStart="40dp"
android:layout_marginTop="91dp"
android:text="TextView" />
<TextView
android:id="@+id/longitude_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/latitude_label"
android:layout_alignParentEnd="true"
android:layout_marginEnd="107dp"
android:text="TextView" />
</RelativeLayout>
答案 0 :(得分:0)
GPS系统非常慢,您可以通过网络获取坐标。 Here是官方的Android开发者&#39;引导。
事实上,要以最快的方式检索坐标,您可以使用它:
Location loc;
try {
loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}catch (Exception e){
//SecurityException if permissions are not enabled
loc = null;
e.printStackTrace();
}
if(loc!=null){//We can use coordinates, yay!
longitude = (float) loc.getLongitude();
latitude = (float) loc.getLatitude();
txtLatLng.setText(getString(R.string.latitude).concat(Float.toString(latitude)).concat(" ").concat(getString(R.string.longitude).concat(Float.toString(longitude))));
}else{//No last location updated so we must use slow GPS method
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);}