2013-10-04 65 views
2

我想獲得latitudelongitude但有時network is available但我沒有得到latitudelongitude的價值。我正在使用MyLocationListener類,並把條件所有,但一段時間value沒有得到。獲取當前的經緯度字符串

protected void showCurrentLocation() 
{ 
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

    if (location != null) 
    { 
     counter++;   
     latitude=location.getLatitude(); 
     longitude=location.getLongitude(); 
     altitude=location.getAltitude(); 
    } 
} 

private class MyLocationListener implements LocationListener 
{ 
    @Override 
    public void onLocationChanged(Location location) 
    { 
     counter++; 
     latitude=location.getLatitude(); 
     longitude=location.getLongitude(); 
     altitude=location.getAltitude(); 
    } 

    @Override 
    public void onStatusChanged(String s, int i, Bundle b) 
    { 
    } 
    @Override 
    public void onProviderDisabled(String s) 
    { 
    } 
    @Override 
    public void onProviderEnabled(String s) 
    { 
    } 
} 
+0

實際上你想要什麼? – RAAAAM

+0

你在哪裏調用MyLocationListener類? – SKT

回答

0

看,這是一個非常普遍的問題。爲了擁有精確的經度和緯度,您必須使用GPS,並且必須在移動設備上激活GPS,但GPS也有一些限制。 GPS在開闊的天空下運行效率最高。你不會在建築物內有清晰的範圍。因此,請在開闊的天空下測試您的代碼並檢查響應。

5

得到經度緯度這裏最好的辦法:

/** PROCESS for Get Longitude and Latitude **/ 
locationManager  = (LocationManager) getSystemService(LOCATION_SERVICE); 

// Define a listener that responds to location updates 

LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     // Called when a new location is found by the network location provider. 

     longitude = String.valueOf(location.getLongitude()); 
     latitude = String.valueOf(location.getLatitude()); 
     Log.d("msg", "changed Loc : "+longitude + ":"+latitude); 
    } 

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

    public void onProviderEnabled(String provider) {} 

    public void onProviderDisabled(String provider) {} 
}; 

// getting GPS status 
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

// check if GPS enabled  
if(isGPSEnabled){ 

    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

    if(location != null) 
    { 
     longitude = String.valueOf(location.getLongitude()); 
     latitude = String.valueOf(location.getLatitude()); 
     Log.d("msg", "Loc : "+longitude + ":"+latitude); 

     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
    }else 
    { 
     /* 
     Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_FINE); 
     criteria.setCostAllowed(true); 
     String provider = locationManager.getBestProvider(criteria, true); 
     */ 

     location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

     if(location != null) 
     { 
      longitude = String.valueOf(location.getLongitude()); 
      latitude = String.valueOf(location.getLatitude()); 

      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
     }else 
     { 
      longitude = "0.00"; 
      latitude = "0.00"; 
     } 
    } 
} 

如果設備無法使用GPS然後它會採取弗朗NetworkProvider否則它會帶0,0爲我的要求得到currentLocation但您可以根據您的要求進行修改

可能會對您有所幫助。

快樂編碼︰D

+1

Hi Pratik,一些設備沒有獲得當前經緯度。我正在使用'LocationManager.GPS_PROVIDER'和'LocationManager.NETWORK_PROVIDER' –