首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >融合定位Api定位

融合定位Api定位
EN

Stack Overflow用户
提问于 2017-07-19 22:37:26
回答 1查看 1.2K关注 0票数 0

我使用融合的位置api来获取纬度和经度,但问题是它说google api客户端还没有连接。我不知道iI哪里错了,请帮帮我。

我的启动活动,其中iI正在使用融合的Api ::

注::GPS Tracker是我在这里使用的一个类,它使用的是位置管理器,我还没有在程序中使用过它。我只是用它来检查Gps是否打开或关闭。

我也添加了清单文件中的所有权限。

代码语言:javascript
复制
package com.example.na462.sunshine;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.*;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Handler;
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.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import java.net.URL;

import static android.R.attr.animation;
import static com.example.na462.sunshine.R.id.MainList;

public class SplashActivity extends Activity implements Animation.AnimationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
Animation animation, TextAnimation;
ImageView imageView;
ImageView ErrorImage;
LocationRequest locationRequest;
TextView AnimationText;
Button ErrorReload;
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
private GoogleApiClient googleApiClient;
LinearLayout Layout;
Receiver receiver;
boolean Connection;
GPSTracker gpsTracker;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    locationRequest = new LocationRequest();
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    locationRequest.setInterval(60 * 1000);
    locationRequest.setFastestInterval(15 * 1000);


    AnimationText = (TextView) findViewById(R.id.TextAnimation);
    ErrorReload = (Button) findViewById(R.id.ErrorReload);
    imageView = (ImageView) (SplashActivity.this).findViewById(R.id.Animation);
    Layout = (LinearLayout) findViewById(R.id.Error);
    ErrorImage = (ImageView) findViewById(R.id.Nointernet);
    gpsTracker = new GPSTracker(SplashActivity.this);
    gpsTracker.getLocation();
    receiver = new Receiver();
    Connection = receiver.internetconnection(SplashActivity.this);

    if (!Connection || !GPSEnabled.GPS) {
        imageView.setVisibility(View.INVISIBLE);
        Layout.setVisibility(View.VISIBLE);
        ErrorImage.setVisibility(View.VISIBLE);
        AnimationText.setVisibility(View.INVISIBLE);

    } else {
        AnimationText.setVisibility(View.VISIBLE);
        imageView.setVisibility(View.VISIBLE);
        Layout.setVisibility(View.INVISIBLE);
        ErrorImage.setVisibility(View.INVISIBLE);

        new Handler().postDelayed(new Runnable() {
            public void run() {
                googleApiClient.disconnect();
                Intent i = new Intent(SplashActivity.this, ScrollingActivity.class);
                startActivity(i);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                finish();
            }
        }, 8000);


        animation = AnimationUtils.loadAnimation(this, R.anim.fade_in);
        animation.setAnimationListener(this);
        imageView.startAnimation(animation);


        TextAnimation = AnimationUtils.loadAnimation(this, R.anim.blink);
        TextAnimation.setAnimationListener(this);
        AnimationText.startAnimation(TextAnimation);

        googleApiClient.connect();



    }
}


@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {

}

@Override
public void onAnimationRepeat(Animation animation) {

}

// Its an onclick Method For Retry if there isnt any conncection or GPS Estabished



@Override
protected void onStart() {
    super.onStart();

}

@Override
protected void onResume() {
    super.onResume();
    if(googleApiClient.isConnected()) {
        onLocationUpdates();
    }
}

@Override
protected void onStop() {
    super.onStop();
    googleApiClient.disconnect();
}

@Override
protected void onPause() {
    super.onPause();
    LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,this);
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    onLocationUpdates();
}

private void onLocationUpdates() {
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void onLocationChanged(Location location) {
    ValuesToPass.Latitude = location.getLatitude();
    ValuesToPass.Longitude = location.getLongitude();
    CordinatesLoc.Latitude = Math.round(location.getLatitude());
    CordinatesLoc.Longitude = Math.round(location.getLongitude());
}
}
EN

回答 1

Stack Overflow用户

发布于 2017-07-19 22:51:15

谷歌最近发布了一个新的应用程序接口,它使得这个过程变得非常简单,你不再需要GoogleApiClient了。我强烈建议您改用此API。

请参考下面的示例了解如何使用它。

代码语言:javascript
复制
FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(context);
client.requestLocationUpdates(locationRequest, pendingIntent)
        .addOnCompleteListener(task -> {
            Log.d("requestLocationUpdates", "result: " + task.getResult());
        })
        .addOnFailureListener(e -> {
            String errorMessage;
            if (e instanceof ApiException) {
                errorMessage = ((ApiException) e).getStatusMessage();
            } else {
                errorMessage = e.getMessage();
            }
            Log.d("requestLocationUpdate", "failed: " + errorMessage);
        });

在您的例子中,您可以用this替换context,以引用您的活动,而不是上下文引用。

pendingIntent是定义如何处理接收到的位置更新的PendingIntent实例。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45193852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档