2012-10-11 72 views
0

我用得到的服務目前的緯度和經度下面的代碼,但它總是返回null。請幫幫我。安卓GPS當前場所空值

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    provider = locationManager.getBestProvider(criteria, false); 
    System.out.println("provider "+provider); 
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

回答

1

使用這段代碼可能是它可以幫助你

import com.example.ConfigClass; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 

public class GpsListener { 

    public static GpsListener refrence = null ; 
    public LocationManager locationManager = null; 
    public LocationListener locationListener = null; 
    public Location location = null; 

    public static GpsListener getInstance(){ 
     if(refrence == null){ 
      refrence = new GpsListener(); 
     } 
     return refrence; 
    } 

    public void startGpsCallBack(Context activityContext){ 
     locationManager = (LocationManager) activityContext.getSystemService(Context.LOCATION_SERVICE); 
     locationListener = new mylocationlistener(); 
     locationManager.requestLocationUpdates(
       LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
     location = locationManager 
       .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
     if (location != null) { 
      ConfigClass.latitudeValue = location.getLatitude(); 
      ConfigClass.longitudeValue = location.getLongitude(); 
     } 
    } 

    public class mylocationlistener implements LocationListener { 
     @Override 
     public void onLocationChanged(Location location) { 
      if (location != null) { 
       ConfigClass.latitudeValue = location.getLatitude(); 
       ConfigClass.longitudeValue = location.getLongitude(); 
      } 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
     } 

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

    public void stopGpsCallBack(){ 
     if (locationManager != null) { 
      locationManager.removeUpdates(locationListener); 
     } 
    } 

    public void startGpsCallbackAgain(Context activityContext){ 
     locationManager = (LocationManager) activityContext.getSystemService(Context.LOCATION_SERVICE); 
     locationListener = new mylocationlistener(); 
     locationManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
     location = locationManager 
     .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (location != null) { 
      ConfigClass.latitudeValue = location.getLatitude(); 
      ConfigClass.longitudeValue = location.getLongitude(); 
     } 
    } 

} 

這是我的代碼

首先startGpsCallBack,然後stop,然後調用startGPSCallBackAgain方法 檢查它,讓我知道這是否解決了您的問題

+0

我想使用GPS_PROVIDER值。請問我可以使用gps嗎?以及爲什麼它返回空值。 –

+0

GPS提供將永遠不會給你的位置,直到通知欄上的gps符號穩定。所以你必須首先通過網絡提供商計算上次知道的位置,然後通過GPS獲取當前位置(需要時間) –

+0

如果您有示例,請上傳至我。 –

2

嘗試此代碼獲取當前位置:

LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      LocationListener mlocListener = new MyLocationListener(); 
      mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 
        10, mlocListener); 


public class MyLocationListener implements LocationListener { 

     public void onProviderDisabled(String provider) 
     { 

      AlertDialog.Builder dlgAlert = new AlertDialog.Builder(
        VisualCV.this); 
       dlgAlert.setMessage("Gps Disabled "); 
       dlgAlert.setTitle("Message"); 
       dlgAlert.setPositiveButton("OK", null); 
       dlgAlert.setCancelable(true); 
       dlgAlert.create().show(); 

     } 

     public void onProviderEnabled(String provider) 
     { 
      Toast.makeText(getApplicationContext(), "Gps enabled", Toast.LENGTH_SHORT).show(); 
     } 

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

     } 

     public void onLocationChanged(Location loc) 
     { 
      // TODO Auto-generated method stub 
      Latitud = loc.getLatitude(); 
      longtitude = loc.getLongitude(); 
      lang = String.format("%.4f", longtitude);//LONGITUDE 

      lati = String.format("%.4f", Latitud);//LATITUDE 




     } 
    } 
+0

我想使用GPS_PROVIDER而不是NETWORK_PROVIDER –

+0

你想要當前位置還是什麼? –

+0

嘗試使用此代碼..u會得到當前拉長。 –

1

試試這個。

@Override 
      public void onCreate(Bundle savedInstanceState) 
      { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main); 

       /* Use the LocationManager class to obtain GPS locations */ 
       LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

       LocationListener mlocListener = new MyLocationListener(); 
       mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 

      } 

      /* Class My Location Listener */ 
      public class MyLocationListener implements LocationListener 
      { 

       public void onLocationChanged(Location loc) 
       { 
       loc.getLatitude(); 
       loc.getLongitude(); 

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

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

       public void onProviderDisabled(String provider) 
       { 
       Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show(); 
       } 

       public void onProviderEnabled(String provider) 
       { 
       Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); 
       } 

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

       } 
      }