2017-05-07 65 views
0

这是我的班级的代码。 textviews在活动中,我的目标只是在textviews中显示纬度和经度。现在,当我运行应用程序时,看起来没有任何事情发生。 textviews只显示“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> 
+2

您应该检查的权限调用requestLocationUpdates之前。如果他们被授予,您还应该在onRequestPermissionResults中请求位置更新。 –

回答

0

GPS系统实在是太慢了,你可以通过网络获取坐标。 Here是Android官方开发者指南。

事实上,以检索你可以使用这个最快的方式坐标:

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);} 

Source of this code