2

在android studio中,在使用GoogleApiClient时,用户授予权限后,当应用程序首次启动时,我没有收到任何位置更新,但是当我再次打开应用程序时,我开始接收更新。这在我修改代码以添加 setMyLocationEnabled() 后开始发生。我的代码如下。我只添加了主 Activity,其中有两个文本框和一个地图片段。

package b.tipsy.checkforlocation;
import android.Manifest;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.pm.PackageManager;
import android.location.Address;
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.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
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.api.GoogleApiClient;
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.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements 
GoogleApiClient.ConnectionCallbacks, 
GoogleApiClient.OnConnectionFailedListener, LocationListener, 
OnMapReadyCallback {
private GoogleApiClient mGoogleApiClient;
TextView latitute;
TextView longitute;
private LocationRequest mLocationRequest;
private Double myLatitute;
private Double myLongitute;
Location current_location;
GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SupportMapFragment mapFragment = (SupportMapFragment) 
    getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync((OnMapReadyCallback) this);
    latitute = (TextView) findViewById(R.id.lat);
    longitute = (TextView) findViewById(R.id.lon);



}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 0x1:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permission is granted", Toast.LENGTH_SHORT).show();
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
                    mMap.setMyLocationEnabled(true);
            }

    }


}

public synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();

}

@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.d("Just", "Connection established. Fetching location ..");
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        mMap.setMyLocationEnabled(true);
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setInterval(5000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }
}

@Override
public void onConnectionSuspended(int i) {
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.e("The error was", "The error was" + connectionResult.toString());
}

@Override
public void onLocationChanged(Location location) {
    Log.d("Just", "works!");
    latitute.setText(String.valueOf(location.getLatitude()));
    longitute.setText(String.valueOf(location.getLongitude()));
    LatLng latLng=new LatLng(location.getLatitude(),location.getLongitude());
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.addMarker(new MarkerOptions().position(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

}

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

}

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

@Override
protected void onResume() {
    super.onResume();
}

@Override
protected void onPause() {
    super.onPause();
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    buildGoogleApiClient();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0x1);
    }
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ) {
      Log.d("Just","In Map Ready");
        mMap.setMyLocationEnabled(true);

    }



}
}
4

0 回答 0