地理位置使用GooglePlayService进行协调

时间:2016-01-24 17:34:46

标签: android google-maps android-studio nullpointerexception google-play-services

我已经从google(不记得URL)复制了以下代码,该代码在命令点击列表器事件中正常运行,我想知道如何从我的MainActivity使用/调用相同的java类(GoogleplayServiceLocation)类



package com.dbprox.tagpic;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

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


/**
 * Created by user on 24/1/2016.
 */
public class GoogleplayServiceLocation extends Activity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,LocationListener{
    TextView TxtViewlat;
    TextView TxtViewlon;


  //  private final Context context;
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST=1000;
    private Location mLastLocation;
    private GoogleApiClient mGoogleApliClient;
    private boolean mRequestLocationUpdates=false;
    private LocationRequest mLocationRequest;
    private static int UPDATE_INTERVAL=10000;

    private static int FASTEST_INTTERVAL=5000;
    private static int DISPLACEMENT =10;




    @Override
    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(checkPlayServices()){
            buildGoogleApiClient();
            createLocationRequest();
            displayLocation();
        }

    }

    public void GoogleplayServiceLocations (Context context)
    {
       // this.context=context;
        if(checkPlayServices()){
            buildGoogleApiClient();
            createLocationRequest();
            displayLocation();
        }


    }


    @Override
    protected void onStart()
    {
    super.onStart();
        if(mGoogleApliClient!=null)
        {mGoogleApliClient.connect();}
    }
    @Override
    protected void onResume()
    {
        super.onResume();
        checkPlayServices();
        if(mGoogleApliClient.isConnected() && mRequestLocationUpdates ){

            startLocationUpdates();

        }

    }
    @Override
    protected void onStop()
    {
        super.onStop();
        if(mGoogleApliClient.isConnected())
        {
            mGoogleApliClient.disconnect();
        }
    }
    @Override
    protected void onPause(){
        super.onPause();
        stopLocationUpdates();

    }
    private void displayLocation(){
        mLastLocation= LocationServices.FusedLocationApi.getLastLocation(mGoogleApliClient);
        if(mLastLocation!=null) {
            double latitude = mLastLocation.getLatitude();
            double longitude = mLastLocation.getLongitude();

            Toast.makeText(getApplicationContext(),
                    latitude + " - " + longitude , Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(),"Can'rt connect to location", Toast.LENGTH_LONG).show();

        }
    }

    private void togglePeriodLocationUpdates(){
        if(!mRequestLocationUpdates) {
        mRequestLocationUpdates=true;
            startLocationUpdates();
        }
        else {
            mRequestLocationUpdates=false;
            stopLocationUpdates();
        }
    }

    protected synchronized void buildGoogleApiClient(){
        mGoogleApliClient=new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();


    }
    protected void createLocationRequest(){
        mLocationRequest=new LocationRequest();
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
        displayLocation();
    }
    private boolean checkPlayServices() {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Toast.makeText(getApplicationContext(), "This device is not supported", Toast.LENGTH_LONG).show();
                finish();
            }
            return true;
        }
        return false;
    }
    protected void startLocationUpdates(){
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApliClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);

    }
    protected void stopLocationUpdates(){
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApliClient, (com.google.android.gms.location.LocationListener) this);
    }


    @Override
    public void onLocationChanged(Location location) {
    mLastLocation=location;
    Toast.makeText(getApplicationContext(),"Location Changed",Toast.LENGTH_LONG).show();
    displayLocation();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }


    @Override
    public void onConnected(Bundle bundle) {
        displayLocation();
        if(mRequestLocationUpdates){
           startLocationUpdates();
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        mGoogleApliClient.connect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.d("TAG", "Connection Failed: " + connectionResult.getErrorCode());
    }





}




0 个答案:

没有答案