2012-12-16 22 views
0

我正在嘗試爲Android項目查找我的當前位置。當應用程序加載時,我的當前位置始終爲空。我已經在清單中設置了權限等。當我找到當前位置時,我打算使用座標來查找到地圖上其他位置的距離。我的代碼片段如下。爲什麼我總是得到一個空值? 這是我的代碼: -無法使用Google地圖獲取位置

package com.example.location; 
    import android.content.Context; 
    import android.location.LocationManager; 
    import android.os.Bundle; 
    import android.location.Location; 
    import com.google.android.maps.MapActivity; 
    import com.google.android.maps.MapView; 
    import android.location.LocationListener; 
    import com.google.android.maps.MapController; 
    import android.widget.Toast; 
    import com.google.android.maps.GeoPoint; 

    public class MainActivity extends MapActivity implements LocationListener { 

     private MapView mapView; 
     private LocationManager locManager; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      //fetch the map view from the layout 
      MapView mapView = (MapView) findViewById(R.id.mapview); 

      //make available zoom controls 
      mapView.setBuiltInZoomControls(true); 
      //latitude and longitude of Rome 
      double lat = 33.6667; 
      double lon = 73.1667; 

      //create geo point 
      GeoPoint point = new GeoPoint((int)(lat * 1E6), (int)(lon *1E6)); 

      //get the MapController object 
      MapController controller = mapView.getController(); 

      //animate to the desired point 
      controller.animateTo(point); 

      //set the map zoom to 13 
      // zoom 1 is top world view 
      controller.setZoom(13); 

      // invalidate the map in order to show changes 
      **mapView.invalidate(); 
      // Use the location manager through GPS 
      locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 
        0, this); 
      //get the current location (last known location) from the location manager 
      Location location = locManager 
        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

      if (location != null) { 

       Toast.makeText(
         this, 
         "Current location:\nLatitude: " + location.getLatitude() 
           + "\n" + "Longitude: " + location.getLongitude(), 
         Toast.LENGTH_LONG).show(); } 
       else { 

        Toast.makeText(this, "Cannot fetch current location!", 
          Toast.LENGTH_LONG).show(); 
       }** 
       //when the current location is found – stop listening for updates (preserves battery) 
       locManager.removeUpdates(this); 
     } 

     @Override 
     protected boolean isRouteDisplayed() { 

      return false; 

     } 
     @Override 
     protected void onPause() { 
      super.onPause(); 
      locManager.removeUpdates(this); //activity pauses => stop listening for updates 
     } 
      @Override 
     public void onLocationChanged(Location location) { 

     } 


     @Override 
     public void onProviderDisabled(String provider) { 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

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

     } 

    } 
+0

試試這個http://stackoverflow.com/a/3683532 – nayab

回答

0

看看這個例子。非常好的例子來處理你喜歡的事情。

http://mobile.tutsplus.com/tutorials/android/android-sdk-build-a-mall-finder-app-points-of-interest/

+0

我已經厭倦了烏爾鏈接,但問題仍然存在,它進入其中location == NULL plz幫助我發現我已經厭倦了 – Shazar

+0

你有沒有喂DDMS在您的模擬器上的最新已知位置方法的經度和緯度。如果你沒有註冊,你需要通過DDMS提供任何以前的位置。 – Rahul

相關問題