0

我正在使用新的 Google API 来检索当前位置,并且我得到了当前位置。但是,如果默认情况下未在应用程序中启用位置服务,并且如果我使用位置意图启用任何位置服务,则客户端不会重新连接,onActivityResult并且我无法获取当前位置。

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
    {

    }
}

但是我找不到位置。当应用程序崩溃时,它会显示错误:

客户端未连接等待连接

关于如何解决这个问题的任何想法?

4

2 回答 2

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

于 2013-06-25T11:51:55.097 回答
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

于 2013-06-25T10:03:06.483 回答