How to show my current location in Google Map Android using google API client

前端 未结 3 1561
难免孤独
难免孤独 2020-12-02 12:01

I want to show my location on a map and zoom in on it. I want to use the GoolgeAPIClient. The map renders and the the pointer is created but the location so wrong. I feel

3条回答
  •  一向
    一向 (楼主)
    2020-12-02 12:04

    For targeting api-23 and higher:

    See the answer here.

    For targeting api-22 and lower:

    Here is the full Activity code that does what you need, a modified version of my other answer here. It registers a location listener when the Activity loads, then when each location changed event occurs, it will add a Marker and also animate the map to center on the current location.

    The key here is to wait until the onMapReady() callback has occurred before you request location updates.

    Here is the full Activity code:

    public class MainActivity extends AppCompatActivity implements
            OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
    
        LocationRequest mLocationRequest;
        GoogleApiClient mGoogleApiClient;
    
        LatLng latLng;
        GoogleMap mGoogleMap;
        SupportMapFragment mFragment;
        Marker currLocationMarker;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            mFragment.getMapAsync(this);
        }
    
        @Override
        public void onMapReady(GoogleMap gMap) {
           mGoogleMap = gMap;
           mGoogleMap.setMyLocationEnabled(true);
    
           buildGoogleApiClient();
    
           mGoogleApiClient.connect();
    
        }
    
        protected synchronized void buildGoogleApiClient() {
            Toast.makeText(this,"buildGoogleApiClient",Toast.LENGTH_SHORT).show();
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }
    
        @Override
        public void onConnected(Bundle bundle) {
            Toast.makeText(this,"onConnected",Toast.LENGTH_SHORT).show();
            Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                    mGoogleApiClient);
            if (mLastLocation != null) {
                //place marker at current position
                //mGoogleMap.clear();
                latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(latLng);
                markerOptions.title("Current Position");
                markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                currLocationMarker = mGoogleMap.addMarker(markerOptions);
            }
    
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(5000); //5 seconds
            mLocationRequest.setFastestInterval(3000); //3 seconds
            mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
            //mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter
    
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    
    
    
        }
    
        @Override
        public void onConnectionSuspended(int i) {
            Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onLocationChanged(Location location) {
    
            //place marker at current position
            //mGoogleMap.clear();
            if (currLocationMarker != null) {
                currLocationMarker.remove();
            }
            latLng = new LatLng(location.getLatitude(), location.getLongitude());
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title("Current Position");
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
            currLocationMarker = mGoogleMap.addMarker(markerOptions);
    
            Toast.makeText(this,"Location Changed",Toast.LENGTH_SHORT).show();
    
             //zoom to current position:
             mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11));
    
            //If you only need one location, unregister the listener
            //LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    
        }
    
    }
    

提交回复
热议问题