2013-06-25 101 views
0

我正在使用新的Google API檢索當前位置,並獲取了當前位置。但是,如果位置服務默認情況下未在應用程序中啓用,並且如果我使用位置意向啓用任何位置服務,則客戶端不會在onActivityResult方法中重新連接,並且我無法獲取當前位置。使用Google Play服務檢索用戶當前位置

public void onConnected(Bundle bundle) { 

    // TODO Auto-generated method stub 
    System.out.println(R.string.connected); 
    System.out.println("error"); 
    startPeriodicUpdates(); 

    if (mUpdatesRequested) { 
     startPeriodicUpdates(); 
    } 

    if (!locationServiceEnabled) 
     return; 
    if(mLocationClient.isConnected()&& servicesConnected()) 
    { 
     fetchlocation = new FetchingLocation(getActivity()); 
     // mLocationClient = new LocationClient(getActivity(), this,this); 
     Location currentLocation = mLocationClient.getLastLocation(); 

     System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>" + 
      mLocationClient.getLastLocation()); 

     latitude=currentLocation.getLatitude(); 
     System.out.println(currentLocation.getLatitude()); 
     System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" 
      + String.valueOf(latitude)); 
    } 
    else 
    { 

    } 
} 

但我無法獲取位置。隨着應用程序崩潰,它顯示錯誤:

client not connected wait for connect

有關如何解決此問題的任何想法?

回答

0

你必須得到經度和緯度值,然後中庸之道使用該函數

public String ConvertPointToLocation(GeoPoint point) { 
    String address = ""; 
    Geocoder geoCoder = new Geocoder(
     getBaseContext(), Locale.getDefault()); 
    try { 
     List<Address> addresses = geoCoder.getFromLocation(
     point.getLatitudeE6()/1E6, 
     point.getLongitudeE6()/1E6, 1); 

     if (addresses.size() > 0) { 
     for (int index = 0; 
    index < addresses.get(0).getMaxAddressLineIndex(); index++) 
      address += addresses.get(0).getAddressLine(index) + " "; 
     } 
    } 
    catch (IOException e) {   
     e.printStackTrace(); 
    } 

    return address; 
    } 

請參閱本教程 http://www.codeproject.com/Articles/112044/GPSLocator-App-to-Find-Current-Nearest-Location-us

+0

我的實際isuue是,如果能像意圖意圖位置服務=新意圖( \t \t \t \t \t \t Settings.ACTION_LOCATION_SOURCE_SETTINGS); \t \t \t \t startActivityForResult(intent,1);在我回到應用程序onActivity結果canot獲取位置後,由於位置客戶端沒有連接。當嘗試重新連接但沒有使用 – RAHULRSANNIDHI

0
public void setLatLong(double lat, double lng) { 
    Longitude = lng; 
    Latitude = lat; 
} 

public double getLatitude() { 
    return Latitude; 
} 

public double getLongitude() { 
    return Longitude; 
} 

public void getLocation() { 

    locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
    Utility obj = new Utility(context); 
    if (obj.isGPSAvailable() && obj.isInternetAvailable()) 
    { 

     locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, locListener); 

     locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0, 0, locListener); 
    } 
    else 

     Toast.makeText(context, "Internet or GPS not available", 
       Toast.LENGTH_LONG).show(); 

    if (locManager != null) 
    { 
     Location loc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if(loc!=null) 
     { 
      setLatLong(loc.getLatitude(), loc.getLongitude()); 

     } 
     else 
     { 
      loc = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      if(loc!=null) 
       { 
        setLatLong(loc.getLatitude(), loc.getLongitude()); 

       } 
     } 
    } 
} 

使用上面的代碼檢索當前的位置,你可以得到更多的幫助@以下鏈接:

http://www.vogella.com/articles/AndroidGoogleMaps/article.html

相關問題