2012-01-06 134 views
2

我已經實現了獲取當前位置緯度經度的示例應用程序。如果我將我的應用程序在仿真器中進行了午餐,並且我從eclipse的Emulator Controls發送了緯度和經度,那麼我將從仿真器控件獲取當前位置的經度和緯度。如果我午餐在實際的設備相同的應用程序,然後我不能夠獲得當前位置的經度和緯度如何獲取android移動設備當前位置的經緯度?

我已經實現代碼獲取當前位置的緯度和經度如下:

 LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

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


    public class MyLocationListener implements LocationListener 
{ 

    @Override 
    public void onLocationChanged(Location loc) 
    { 

    loc.getLatitude(); 
    loc.getLongitude(); 

    Log.v("11111","Latitude :"+loc.getLatitude()); 
    Log.v("22222","Longitude :"+ loc.getLongitude()); 

    } 

}

從上面的代碼,我沒有得到當前的位置在真正的Android設備的經緯度。

如何獲取當前位置實際設備的經緯度?

請任何機構幫助我

+0

你有沒有得到任何錯誤?如果是的話,然後張貼logobo – 2012-01-06 11:40:39

+0

不,我沒有得到任何error.but我沒有得到任何 – 2012-01-06 11:44:48

+0

,因爲當你在設備上運行這個應用程序,你有改變(「GPS_PROVIDER」)到(「NETWORK_PROVIDER」)。所以在這之後,你會得到輸出... – 2012-02-14 05:42:20

回答

2

普拉薩德試試這個代碼..by使用這個我成功地讓Lat Long網

 private Location location = null; 
     private LocationManager locationManager = null; 
    locationManager = (LocationManager) context.getSystemService        (Context.LOCATION_SERVICE); 

    Criteria locationCritera = new Criteria(); 
    locationCritera.setAccuracy(Criteria.ACCURACY_FINE); 
    locationCritera.setAltitudeRequired(false); 
    locationCritera.setBearingRequired(false); 
    locationCritera.setCostAllowed(true); 
    locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT); 

    String providerName = locationManager.getBestProvider(locationCritera, 
      true); 
    location = locationManager.getLastKnownLocation(providerName); 
    locationListener = new MyLocationListener(); 

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
      0, locationListener); 

    currentLocation = context.getSharedPreferences(PREFS_NAME, 0); 
    editor = currentLocation.edit(); 
} 
public String getCurrentLatitude() { 
    try { 
     if (!currentLocation.getString("currentLatitude", "") 
       .equalsIgnoreCase("")) 
      return currentLocation.getString("currentLatitude", ""); 
     else if (location.getLatitude() != 0.0) 
      return Double.toString(location.getLatitude()); 
     else 
      return "0.0"; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return "0.0"; 
} 

public String getCurrentLongitude() { 
    try { 


     if (!currentLocation.getString("currentLongitude", "") 
       .equalsIgnoreCase("")) 
      return currentLocation.getString("currentLongitude", ""); 
     else if (location.getLongitude() != 0.0) 
      return Double.toString(location.getLongitude()); 
     else 
      return "0.0"; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return "0.0"; 
} 
+1

確保在設備中啓用gps – 2012-01-06 11:52:19

+0

MyLocationListener()類的代碼類 – Kailas 2014-06-02 06:24:44

1

你試圖在模擬器或設備。如果您嘗試使用設備,那麼您的設備應該靠近窗戶或在敞開的地方。

+0

我試圖在我的實驗室中的真實設備上。它不是在我的實驗室工作嗎?這不是一個答案,它是comment.plz不要將它作爲答案發布。 – 2012-01-06 11:43:35

+0

它可以在指導基地回答親愛的。我想知道你爲什麼會出錯。你在開放的地方嘗試嗎? – Android 2012-01-06 11:51:12

1

在您的MyLocationListener中重寫這些方法。以便您可以跟蹤您的GPS狀態。檢查並看看你能得到什麼問題

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) 

     { 

     } 
4

GPS_PROVIDER不能在綁定的地方工作。因此,如果啓用了gps_provider但您獲得了空位置,則可以將提供程序從gps替換爲NETWORK_PROVIDER。

1

在真實設備上,你將不得不耐心等待來自衛星的修復。這可能需要幾分鐘的時間,即使天空清晰。

對於外部測試,最好建議您的應用程序中有一個簡單的文本框,將其初始化爲「沒有GPS修復程序」之類的內容,然後發送包含經緯度座標的字符串位置變化。

1

也許這更清楚。我將條件更改爲更高效以後如果有可能

double longitude = 0; 
    double latitude = 0; 

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    if (lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null) { 
     Location location = lm 
       .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     longitude = location.getLongitude(); 
     latitude = location.getLatitude(); 
    } else { 
     Location location = lm 
       .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
     longitude = location.getLongitude(); 
     latitude = location.getLatitude(); 
    } 
0

有兩種類型的位置提供商, GPS位置提供 網絡位置提供商

package com.shir60bhushan.gpsLocation; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.widget.TextView; 

import android.util.Log; 

public class MainActivity extends Activity implements LocationListener{ 
protected LocationManager locationManager; 
protected LocationListener locationListener; 
protected Context context; 
TextView txtLat; 
String lat; 
String provider; 
protected String latitude,longitude; 
protected boolean gps_enabled,network_enabled; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
txtLat = (TextView) findViewById(R.id.textview1); 

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
} 
@Override 
public void onLocationChanged(Location location) { 
txtLat = (TextView) findViewById(R.id.textview1); 
txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); 
} 

@Override 
public void onProviderDisabled(String provider) { 
Log.d("Latitude","disable"); 
} 

@Override 
public void onProviderEnabled(String provider) { 
Log.d("Latitude","enable"); 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
Log.d("Latitude","status"); 
} 
} 

這個鏈接可以幫助你更多信息 http://androidtutorials60.blogspot.in/2013/09/gps-current-location-of-device.html

相關問題