android google-maps-android-api-2 android-fusedlocation

El punto azul y el círculo no se muestran en MyLocation usando la API de ubicación fusionada de Android



google-maps-android-api-2 android-fusedlocation (3)

Estaba usando LocationManager para rastrear la ubicación actual del usuario, ahora después de cambiar el administrador de ubicación a la API FusedLocation, el punto azul y el círculo no se muestran incluso después de configurar map.setMyLocationEnabled(true) . Puedo ver el icono de ubicación actual en la esquina superior derecha de mi fragmento de mapa, pero hacer clic en él no hace nada. Revertí mi código a LocationManager ahora puedo ver el punto azul que apunta a mi ubicación actual. qué podría estar mal usando la API de ubicación fusionada.


Debe agregar los siguientes permisos en manifiesto:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-feature android:glEsVersion="0x00020000" android:required="true" />


Para api api-23 o superior

Ver esta respuesta ...

Para api-22 y menores:

Este código funciona para mí, tiene el punto / círculo azul MyLocation , y también coloca un Marker en la ubicación actual utilizando el Proveedor de ubicación fusionada.

Aquí está el código de actividad completo que utilicé:

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; import android.location.Location; 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.LocationRequest; import com.google.android.gms.location.LocationServices; import com.google.android.gms.location.LocationListener; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.OnMapReadyCallback; public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, OnMapReadyCallback { LocationRequest mLocationRequest; GoogleApiClient mGoogleApiClient; LatLng latLng; GoogleMap mGoogleMap; SupportMapFragment mFragment; Marker mCurrLocation; @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 googleMap) { mGoogleMap = googleMap; mGoogleMap.setMyLocationEnabled(true); buildGoogleApiClient(); mGoogleApiClient.connect(); } @Override public void onPause() { super.onPause(); //Unregister for location callbacks: if (mGoogleApiClient != null) { LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); } } 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)); mCurrLocation = 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) { //remove previous current location marker and add new one at current position if (mCurrLocation != null) { mCurrLocation.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)); mCurrLocation = mGoogleMap.addMarker(markerOptions); Toast.makeText(this,"Location Changed",Toast.LENGTH_SHORT).show(); //If you only need one location, unregister the listener //LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); } }

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <fragment class="com.google.android.gms.maps.SupportMapFragment" android:id="@+id/map" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>

Resultado:


MarkerOptions().position(new LatLng( location.getLatitude(), location.getLongitude()));

Prueba esto,

if (location!=null) { googleMap.clear(); LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); CameraPosition cameraPosition = new CameraPosition.Builder() .target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(14).build(); googleMap.animateCamera(CameraUpdateFactory .newCameraPosition(cameraPosition)); // create markerOptions MarkerOptions markerOptions = new MarkerOptions().position(new LatLng( location.getLatitude(), location.getLongitude())); // ROSE color icon markerOptions.icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_ROSE)); markerOptions.position(latLng); // adding markerOptions Marker marker = googleMap.addMarker(markerOptions); dropPinEffect(marker); }