Я новичок в Android Разработка, и я пытаюсь разработать приложение, которое будет отображать карту Google с текущим местоположением пользователей. Мне удалось включить карту и добавить маркеры, но у меня возникли проблемы с получением текущего местоположения. Я добавил разрешения на местоположение в.
activity_location. xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Android Манифест
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alicearmstrong.coffeysloyaltyprojectv1">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<uses-library android:name="org.apache.http.legacy" android:required="false" />
<activity
android:name=".NavigationMainOwner"
android:label="@string/title_activity_navigation_main_owner" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/api_map_key" />
....
LocationFragment. java
public class LocationFragment extends Fragment implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private LocationViewModel locationViewModel;
private GoogleMap gMap;
private MapView mapView;
private Location currentLocation;
private int LOCATION_PERMISSION_REQUEST_CODE = 1234;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
locationViewModel = ViewModelProviders.of( this ).get( LocationViewModel.class );
View view = inflater.inflate( R.layout.fragment_location_customer, container, false );
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated( view, savedInstanceState );
mapView = (MapView) view.findViewById( R.id.map );
if (mapView != null)
{
mapView.onCreate( null );
mapView.onResume();
mapView.getMapAsync( this );
checkLocationPermission();
}
}
@Override
public void onMapReady(GoogleMap googleMap)
{
MapsInitializer.initialize( getContext() );
gMap = googleMap;
LatLng coffeys = new LatLng( 54.572720, -5.959151 );
gMap.addMarker( new MarkerOptions().position( coffeys ).title( "Coffey's Butchers" ) );
gMap.moveCamera( CameraUpdateFactory.newLatLngZoom( coffeys, 12 ) );
}
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// GPS may be turned off
if (location == null) {
return;
}
Double lat = location.getLatitude();
Double lng = location.getLongitude();
currentLocation = location;
Toast.makeText( getActivity(), "Updated Location: " + lat + lng, Toast.LENGTH_SHORT ).show();
}
};
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
public boolean checkLocationPermission()
{
if (ContextCompat.checkSelfPermission( getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION )
!= PackageManager.PERMISSION_GRANTED)
{
// Asking user if explanation is needed
if (ActivityCompat.shouldShowRequestPermissionRationale( getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION ))
{
//Prompt the user once explanation has been shown
requestPermissions( new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE
);
} else {
// No explanation needed, we can request the permission.
requestPermissions( new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE );
}
return false;
}
else
{
return true;
}
}
}
Любая помощь в правильном направлении будет принята с благодарностью! Большое спасибо.