我正在使用新的Google API来检索当前位置,并且我获得了当前位置。但是,如果默认情况下未在应用程序中启用位置服务,并且如果我使用location intend启用任何位置服务,则客户端不会以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
{
}
}
但我找不到位置。当应用程序崩溃时,它会显示错误:
客户端未连接等待连接
有关如何解决此问题的任何想法?
答案 0 :(得分: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
答案 1 :(得分: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