2015-04-27 54 views
1

我有這個類:的位置沒有顯示

public class MapsActivity extends FragmentActivity implements LocationListener { 

    private GoogleMap mMap; // Might be null if Google Play services APK is not available. 
    int f=1; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     setUpMapIfNeeded(); 

      } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
    } 


    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the map. 
     if (mMap == null) { 
      // Try to obtain the map from the SupportMapFragment. 
      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 
      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 
       setUpMap(); 
      } 
     } 
    } 


    private void setUpMap() { 
     mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 


     Log.d("DanT", "location changed"); 
     //if(f==1) 
     // { 
      location.getLatitude(); 
      location.getLongitude(); 
      // f=2; 
     //} 

     String Text = "My current location is: " + 
       "Latitud = " + location.getLatitude() + 
       "Longitud = " + location.getLongitude(); 

     TextView txtgps = (TextView) findViewById(R.id.loc_textView); 

     txtgps.setText(Text); 

     Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show(); 


    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 
} 

我可以看到地圖,但位置沒有文字。

+0

你沒用過的LocationManager ..是你嗎? –

回答

0
//You need to create a location manager object and request location updates or getlastknownlocation 

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    try { 
        locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 2000, 0, this); 
        locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 2000, 0, this); 
       } catch (java.lang.SecurityException ex) { 
        Log.i("", "fail to request location update, ignore", ex); 
       } catch (IllegalArgumentException ex) { 
        Log.d("", "network provider does not exist, " + ex.getMessage()); 
       } 
0

使用LocationManager獲得訪問該位置相關的數據,並通過實例化它:

LocationManager mLocationmanager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 

而且從NETWORK_PROVIDERGPS_PROVIDER獲取位置對象。我建議從網絡供應商處獲取位置數據,因爲我使用的電池電量少於GPS(根據android official document)。

現在通過調用

Double mUserlatitude = location.getLatitude(); 
Double mUserlongitude = location.getLongitude(); 

完全摘錄如下得到您的位置:

LocationManager mLocationmanager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
Location location = mLocationmanager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
Double mUserlatitude = location.getLatitude(); 
Double mUserlongitude = location.getLongitude(); 
Log.d("Latitude",""+mUserlatitude); 
Log.d("Latitude",""+mUserlongitude);