Android LocationClient 类已弃用,但在文档中使用
Posted
技术标签:
【中文标题】Android LocationClient 类已弃用,但在文档中使用【英文标题】:Android LocationClient class is deprecated but used in documentation 【发布时间】:2014-08-28 00:24:51 【问题描述】:如果我们查看LocationClient
的文档,我们可以看到该类已被弃用。
但documentation to get the current location中使用了已弃用的类。
我认为这有点误导还是我查看的文档不正确?
【问题讨论】:
与往常一样,他们不推荐使用 api,但不会同时更新文档。您必须等到文档更新或尝试找到一个工作示例 @GeorgeMathewK 你能标出正确答案吗?) 【参考方案1】:Google 再次发布了一个新的 API,但他们没有更新文档:$ 在花了一些时间试图弄清楚它是如何工作的之后,我得到了它,这里有一个使用新的/最新的位置服务 API 的完整示例。 .. 享受开发:)
import android.location.Location;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
public class MainActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener
private final String TAG = "MyAwesomeApp";
private TextView mLocationView;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
mLocationView = new TextView(this);
setContentView(mLocationView);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
@Override
protected void onStart()
super.onStart();
// Connect the client.
mGoogleApiClient.connect();
@Override
protected void onStop()
// Disconnecting the client invalidates it.
mGoogleApiClient.disconnect();
super.onStop();
@Override
public void onConnected(Bundle bundle)
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(1000); // Update location every second
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
@Override
public void onConnectionSuspended(int i)
Log.i(TAG, "GoogleApiClient connection has been suspend");
@Override
public void onConnectionFailed(ConnectionResult connectionResult)
Log.i(TAG, "GoogleApiClient connection has failed");
@Override
public void onLocationChanged(Location location)
mLocationView.setText("Location received: " + location.toString());
不要忘记将此权限添加到您的 AndroidManifest.xml 文件中:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
注意:如果您只需要获取最后一个位置(无需更新),您可以使用来自 OnConnected 的LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient)
【讨论】:
是否有类似的代码示例说明如何使用 LocationServices.GeofencingAPI? 好答案。只需将它与一些旧组件/东西合并,一切都会像魅力一样工作。 请注意,如果您只想获取最后一个位置而不进行更新,您可以使用来自 OnConnected 的LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
您将如何停止正在进行的位置请求?
FusedLocationApi 已弃用【参考方案2】:
有些文档在 Google 中是旧的(您提到的一些示例仍然使用已弃用的 LocationClient
)。
您必须按照位置服务示例中的说明使用新的 GoogleApiClient:
private GoogleApiClient mGoogleApiClient;
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build()
当新客户端连接时,您可以使用融合位置 api,例如:
LocationServices.FusedLocationApi.requestLocationUpdates(theNewClient,
locationRequest, locationListener);
【讨论】:
你能告诉我如何得到纬度和经度吗? FusedLocationApi 已弃用【参考方案3】:这似乎在developer blog 中有所提及。对于 LocationClient,您可以将它与 LocationServices 结合使用,然后将我们带到 GeofencingApi。
【讨论】:
新的 Google Play API 发布六个月后,文档尚未更新。 Google 的官方 LocationProvider 应用程序似乎仍在使用此包中不再存在的 LocationClient 导入 com.google.android.gms.location.LocationClient;【参考方案4】:LocationClient 已删除。 GoogleApiClient 是用于 Google Play 服务位置 API 的 api。
常见场景的示例代码为here,培训课程were updated 即将推出。
【讨论】:
【参考方案5】:根据documentation更新码..
package iwannado.com.myapplicationforsha1key;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Geocoder;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.identity.intents.Address;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.List;
public class MapWithMapViewActivity extends AppCompatActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener
private static final String TAG = MapWithMapViewActivity.class.getCanonicalName();
private GoogleMap mMap = null;
private MapView mMapView = null;
private EditText loatcationEditText = null;
private GoogleApiClient mGoogleApiClient = null;
private Location mLocationRequest = null;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_with_map_view);
loatcationEditText = (EditText) findViewById(R.id.loatcation_edit_text);
mMapView = (MapView) findViewById(R.id.mapView);
/*
* The method is used to create mapView
* */
mMapView.onCreate(savedInstanceState);
/*
*The method Return Google map
* */
mMapView.getMapAsync(this);
gotoCurrentLoactionGooglePlayService();
@Override
protected void onResume()
super.onResume();
mMapView.onResume();
@Override
protected void onPause()
super.onPause();
mMapView.onPause();
@Override
public void onLowMemory()
super.onLowMemory();
mMapView.onLowMemory();
@Override
protected void onStart()
super.onStart();
mGoogleApiClient.connect();
@Override
protected void onStop()
super.onStop();
mGoogleApiClient.disconnect();
@Override
protected void onSaveInstanceState(Bundle outState)
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
@Override
protected void onDestroy()
super.onDestroy();
mMapView.onDestroy();
@Override
public void onMapReady(GoogleMap googleMap)
mMap = googleMap;
private void gotoCurrentLoactionGooglePlayService()
/*working with new google api for laction..
http://***.com/a/25173057
* */
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
@Override
public void onConnected(@Nullable Bundle bundle)
/*
* Follow this documentation.. https://developer.android.com/training/location/retrieve-current.html
*
* Please add Runtime permission here for android 6
* */
mLocationRequest = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLocationRequest != null)
LatLng latLng = new LatLng(mLocationRequest.getLatitude(),mLocationRequest.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Programmatically Current Loaction"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
Toast.makeText(this,"getLatitude() = "+String.valueOf(mLocationRequest.getLatitude())+"\n getLongitude() = "+String.valueOf(mLocationRequest.getLongitude()),Toast.LENGTH_LONG).show();
@Override
public void onConnectionSuspended(int i)
Log.i(TAG, "GoogleApiClient connection has been suspend");
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
Log.i(TAG, "GoogleApiClient connection has failed");
@Override
public void onLocationChanged(Location location)
Toast.makeText(this,"Location received: " + location.toString(),Toast.LENGTH_LONG).show();
【讨论】:
【参考方案6】:您只需将其添加到您的代码中,
private FusedLocationProviderClient mFusedLocationClient;
private Location mLastLocation;
oncreate()
.
.
FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>()
@Override
public void onSuccess(Location location)
mLastLocation =location;
if (mLastLocation!= null)
LogUtils.logE(TAG, "Lat: " + mLastLocation.getLatitude() + "Long: " + mLastLocation.getLongitude());
);
.
.
根据文档更新代码..
【讨论】:
以上是关于Android LocationClient 类已弃用,但在文档中使用的主要内容,如果未能解决你的问题,请参考以下文章
找不到类 com.google.android.gms.location.LocationClient (android)
无法在 Android Studio 中解析符号“LocationClient”
Android GooglePlay LocationClient 为空,尽管在 onCreate() 中初始化