From b3be07836104e97dc5f6753b931e43ace0c7e3d3 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Mon, 14 Dec 2020 14:35:53 +0100 Subject: [PATCH] added mapview and osm map --- app/src/main/AndroidManifest.xml | 2 + .../nextlocation/fragments/HomeFragment.java | 97 +++++++++++++++++++ app/src/main/res/layout/fragment_home.xml | 9 +- 3 files changed, 104 insertions(+), 4 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 465afef..f5def4b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + diff --git a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java index 7610b2a..4314101 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java @@ -1,20 +1,49 @@ package com.a1.nextlocation.fragments; +import android.Manifest; +import android.content.Context; +import android.content.pm.PackageManager; +import android.location.Location; +import android.location.LocationManager; import android.os.Bundle; +import androidx.annotation.NonNull; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.a1.nextlocation.R; +import org.osmdroid.api.IMapController; +import org.osmdroid.config.Configuration; +import org.osmdroid.util.GeoPoint; +import org.osmdroid.views.MapView; +import org.osmdroid.views.overlay.compass.CompassOverlay; +import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider; +import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider; +import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay; + +import java.util.ArrayList; + public class HomeFragment extends Fragment { + private final String userAgent = "com.ai.nextlocation.fragments"; + private MapView mapView; + private final int REQUEST_PERMISSIONS_REQUEST_CODE = 1; + private final String TAG = HomeFragment.class.getCanonicalName(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + requestPermissionsIfNecessary( + // if you need to show the current location request FINE_LOCATION permission + Manifest.permission.ACCESS_FINE_LOCATION, + // WRITE_EXTERNAL_STORAGE is required in order to show the map + Manifest.permission.WRITE_EXTERNAL_STORAGE); } @@ -23,5 +52,73 @@ public class HomeFragment extends Fragment { Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_home, container, false); + + + } + + private void initMap(@NonNull View view) { + // set the user agent + Configuration.getInstance().setUserAgentValue(userAgent); + + // create the map view + mapView = (MapView) view.findViewById(R.id.mapView); + mapView.setDestroyMode(false); + mapView.setTag("mapView"); + mapView.setMultiTouchControls(true); + + // get the location provider + GpsMyLocationProvider gpsMyLocationProvider = new GpsMyLocationProvider(this.requireContext()); + + // add the compass overlay + CompassOverlay compassOverlay = new CompassOverlay(requireContext(),new InternalCompassOrientationProvider(requireContext()),mapView); + compassOverlay.enableCompass(); + mapView.getOverlays().add(compassOverlay); + + // add the location overlay + MyLocationNewOverlay mLocationOverlay = new MyLocationNewOverlay(gpsMyLocationProvider, mapView); + mLocationOverlay.enableFollowLocation(); + mLocationOverlay.enableMyLocation(); + mapView.getOverlays().add(mLocationOverlay); + + // add the zoom controller + IMapController mapController = mapView.getController(); + mapController.setZoom(15.0); + + // add location manager and set the start point + LocationManager locationManager = (LocationManager) requireActivity().getSystemService(Context.LOCATION_SERVICE); + + try { + Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); + GeoPoint startPoint = new GeoPoint(location.getLatitude(), location.getLongitude()); + mapController.setCenter(startPoint); + + } catch (SecurityException e) { + Log.d(TAG, "onViewCreated: exception while getting location: " + e.getLocalizedMessage()); + + requestPermissionsIfNecessary( + // if you need to show the current location request FINE_LOCATION permission + Manifest.permission.ACCESS_FINE_LOCATION, + // WRITE_EXTERNAL_STORAGE is required in order to show the map + Manifest.permission.WRITE_EXTERNAL_STORAGE); + + } + + } + private void requestPermissionsIfNecessary(String... permissions) { + ArrayList permissionsToRequest = new ArrayList<>(); + if (this.getContext() != null) + for (String permission : permissions) { + if (ContextCompat.checkSelfPermission(this.getContext(), permission) + != PackageManager.PERMISSION_GRANTED) { + // Permission is not granted + permissionsToRequest.add(permission); + } + } + if (permissionsToRequest.size() > 0 && this.getActivity() != null) { + ActivityCompat.requestPermissions( + this.getActivity(), + permissionsToRequest.toArray(new String[0]), + REQUEST_PERMISSIONS_REQUEST_CODE); + } } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 5194fda..c90fc63 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -5,10 +5,11 @@ android:layout_height="match_parent" tools:context=".fragments.HomeFragment"> - - + android:layout_height="match_parent"> + + \ No newline at end of file