Android 使用谷歌play服务的棒棒糖版本中没有纬度和经度
我尝试了一个通过谷歌Play服务显示纬度和经度的应用程序,它在下面的棒棒糖版本中运行良好。但它在棒棒糖上显示“未检测到位置”。我使用了以下代码:Android 使用谷歌play服务的棒棒糖版本中没有纬度和经度,android,google-play-services,android-5.0-lollipop,android-location,Android,Google Play Services,Android 5.0 Lollipop,Android Location,我尝试了一个通过谷歌Play服务显示纬度和经度的应用程序,它在下面的棒棒糖版本中运行良好。但它在棒棒糖上显示“未检测到位置”。我使用了以下代码: public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { private GoogleApiClient mGoogle
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
private Location mLocation;
private static final String TAG = "MainActivity";
private TextView mLatitudeTextView;
private TextView mLongitudeTextView;
Geocoder geocoder;
List<Address> addresses;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLatitudeTextView = (TextView) findViewById((R.id.latitude_textview));
mLongitudeTextView = (TextView) findViewById((R.id.longitude_textview));
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLocation != null) {
mLatitudeTextView.setText(String.valueOf(mLocation.getLatitude()));
mLongitudeTextView.setText(String.valueOf(mLocation.getLongitude()));
} else {
Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Connection Suspended");
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode());
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
}
公共类MainActivity扩展AppCompative实现GoogleAppClient.ConnectionCallbacks、GoogleAppClient.OnConnectionFailedListener{
私人GoogleapClient MGoogleapClient;
私人位置;
私有静态最终字符串TAG=“MainActivity”;
私有文本视图mLatitudeTextView;
私有文本视图;
地理编码器;
列出地址;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLatitudeTextView=(TextView)findViewById((R.id.latitude\u TextView));
MLONGITEXTVIEW=(TextView)findViewById((R.id.longitude\u TextView));
mgoogleapclient=新的Googleapclient.Builder(此)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.addApi(LocationServices.API)
.build();
}
@凌驾
未连接的公共空间(捆绑包){
mLocation=LocationServices.FusedLocationApi.getLastLocation(mgoogleapClient);
如果(mLocation!=null){
mLatitudeTextView.setText(String.valueOf(mLocation.getLatitude());
mlongityTextView.setText(String.valueOf(mLocation.getLongitude());
}否则{
Toast.makeText(此“未检测到位置”,Toast.LENGTH_SHORT).show();
}
}
@凌驾
公共空间连接暂停(int i){
Log.i(标签“连接暂停”);
mGoogleApiClient.connect();
}
@凌驾
公共无效onConnectionFailed(ConnectionResult ConnectionResult){
Log.i(标记“连接失败。错误:”+connectionResult.getErrorCode());
}
@凌驾
受保护的void onStart(){
super.onStart();
mGoogleApiClient.connect();
}
@凌驾
受保护的void onStop(){
super.onStop();
if(mgoogleapClient.isConnected()){
mGoogleApiClient.disconnect();
}
}
}
我在gradle中包括compilecom.google.android.gms:play services:8.1.0
,在清单文件中包括android.permission.ACCESS\u rough\u LOCATION
请任何人帮助我解决此问题。尝试添加
如果没有,请尝试使用本教程:尝试添加
如果没有,请尝试使用本教程:使用此方法连接到Google API客户端:
GoogleApiClient apiClient=null;
LocationRequest mLocationRequest=null;
private void setLocationLocationRequest() {
try {
apiClient=new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(29000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
apiClient.connect();
}catch (Exception e){
Log.d("LS", e.getMessage() == null ? "" : e.getMessage());
}
}
有关更多详细信息,请选中并使用此方法连接到Google API客户端:
GoogleApiClient apiClient=null;
LocationRequest mLocationRequest=null;
private void setLocationLocationRequest() {
try {
apiClient=new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(29000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
apiClient.connect();
}catch (Exception e){
Log.d("LS", e.getMessage() == null ? "" : e.getMessage());
}
}
有关更多详细信息,请检查并确保清单文件中正确包含了所有内容。我尝试了本教程。也就是说,精细定位使用设备GPS、手机数据和WiFi获得最准确的位置,但这需要电池寿命。粗略定位使用设备蜂窝数据和WiFi获取位置。它不会精确到很好,但使用的电池电量要少得多,返回的位置精度相当于城市街区。请确保清单文件中包含的所有内容都正确。我尝试了本教程。也就是说,精细定位使用设备GPS、手机数据和WiFi获得最准确的位置,但这需要电池寿命。粗略定位使用设备蜂窝数据和WiFi获取位置。它不会像Fine那样精确,但使用的电池电量要少得多,返回的位置精度相当于一个城市街区。