2013-06-25 72 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

相关问题