From ea351e5789fb0379da83f72ecf39b4fa2426e501 Mon Sep 17 00:00:00 2001 From: ed Date: Fri, 11 Jun 2021 00:13:19 +0200 Subject: [PATCH] First version map, Localication of device with blue dot --- .gitignore | 15 ++ .idea/.gitignore | 3 + .idea/compiler.xml | 6 + .idea/gradle.xml | 21 +++ .idea/jarRepositories.xml | 25 +++ .idea/misc.xml | 9 + app/.gitignore | 1 + app/build.gradle | 41 +++++ app/proguard-rules.pro | 21 +++ .../maps0123/ExampleInstrumentedTest.java | 26 +++ app/src/debug/res/values/google_maps_api.xml | 24 +++ app/src/main/AndroidManifest.xml | 41 +++++ .../de/edotzlaff/maps0123/MapsActivity.java | 69 +++++++ .../main/java/de/edotzlaff/maps0123/map.java | 131 +++++++++++++ .../drawable-v24/ic_launcher_foreground.xml | 30 +++ .../res/drawable/ic_launcher_background.xml | 170 +++++++++++++++++ app/src/main/res/layout/activity_map.xml | 15 ++ app/src/main/res/layout/activity_maps.xml | 16 ++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3593 bytes .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 5339 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2636 bytes .../res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 3388 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4926 bytes .../res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 7472 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7909 bytes .../res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 11873 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10652 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 16570 bytes app/src/main/res/values-night/themes.xml | 16 ++ app/src/main/res/values/colors.xml | 10 + app/src/main/res/values/strings.xml | 4 + app/src/main/res/values/themes.xml | 16 ++ .../release/res/values/google_maps_api.xml | 20 ++ .../edotzlaff/maps0123/ExampleUnitTest.java | 17 ++ build.gradle | 24 +++ gradle.properties | 19 ++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 54329 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 172 ++++++++++++++++++ gradlew.bat | 84 +++++++++ settings.gradle | 2 + 43 files changed, 1064 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/misc.xml create mode 100644 app/.gitignore create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/androidTest/java/de/edotzlaff/maps0123/ExampleInstrumentedTest.java create mode 100644 app/src/debug/res/values/google_maps_api.xml create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/de/edotzlaff/maps0123/MapsActivity.java create mode 100644 app/src/main/java/de/edotzlaff/maps0123/map.java create mode 100644 app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 app/src/main/res/layout/activity_map.xml create mode 100644 app/src/main/res/layout/activity_maps.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/values-night/themes.xml create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/themes.xml create mode 100644 app/src/release/res/values/google_maps_api.xml create mode 100644 app/src/test/java/de/edotzlaff/maps0123/ExampleUnitTest.java create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100644 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..61a9130 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..18de3ac --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,21 @@ + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..a5f05cd --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d5d35ec --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..507f5fe --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,41 @@ +plugins { + id 'com.android.application' +} + +android { + compileSdkVersion 30 + buildToolsVersion "30.0.3" + + defaultConfig { + applicationId "de.edotzlaff.maps0123" + minSdkVersion 24 + targetSdkVersion 30 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } +} + +dependencies { + + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'com.google.android.material:material:1.3.0' + implementation 'com.google.android.gms:play-services-maps:17.0.1' + implementation 'androidx.constraintlayout:constraintlayout:2.0.4' + testImplementation 'junit:junit:4.+' + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' + implementation "com.google.android.gms:play-services-location:15.0.1" //Import to find the location from the device +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/de/edotzlaff/maps0123/ExampleInstrumentedTest.java b/app/src/androidTest/java/de/edotzlaff/maps0123/ExampleInstrumentedTest.java new file mode 100644 index 0000000..dd556ac --- /dev/null +++ b/app/src/androidTest/java/de/edotzlaff/maps0123/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package de.edotzlaff.maps0123; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("de.edotzlaff.maps0123", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/app/src/debug/res/values/google_maps_api.xml b/app/src/debug/res/values/google_maps_api.xml new file mode 100644 index 0000000..03bf08f --- /dev/null +++ b/app/src/debug/res/values/google_maps_api.xml @@ -0,0 +1,24 @@ + + + AIzaSyCCnjanFAzxuH91tD206IjaAU4_ymSgug0 + \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..cb453e3 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/de/edotzlaff/maps0123/MapsActivity.java b/app/src/main/java/de/edotzlaff/maps0123/MapsActivity.java new file mode 100644 index 0000000..5acfd4e --- /dev/null +++ b/app/src/main/java/de/edotzlaff/maps0123/MapsActivity.java @@ -0,0 +1,69 @@ +package de.edotzlaff.maps0123; + +import androidx.fragment.app.FragmentActivity; + +import android.app.Dialog; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.Toast; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.GoogleApiAvailability; +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; + +public class MapsActivity extends FragmentActivity { + + private static final String TAG = "MainActivity"; + private static final int ERROR_DIALOG_REQUEST = 9001; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_map); //IMPORTANT: MapsActivity führt zur activity_map.xml (nicht zur activity_maps) + + if (isServiceOK()){ + init(); + } + } + + private void init(){ + Button btnMap = (Button) findViewById(R.id.btnMap); + btnMap.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(MapsActivity.this, map.class); + startActivity(intent); + } + }); + + } + + public boolean isServiceOK(){ + Log.d(TAG, "isServicesOK(): checking google services version"); + + int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(MapsActivity.this); + + if (available== ConnectionResult.SUCCESS){ + Log.d(TAG,"isServicesOK: Google Play Services is working"); + return true; + } + else if (GoogleApiAvailability.getInstance().isUserResolvableError(available)){ + Log.d(TAG, "isServicesOK(): an error occured but we can fix it"); + Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(MapsActivity.this, available, ERROR_DIALOG_REQUEST); + dialog.show(); + } + else { + Toast.makeText(this, "You can`t make map request", Toast.LENGTH_SHORT).show(); + } + return false; + } + +} \ No newline at end of file diff --git a/app/src/main/java/de/edotzlaff/maps0123/map.java b/app/src/main/java/de/edotzlaff/maps0123/map.java new file mode 100644 index 0000000..73e02a2 --- /dev/null +++ b/app/src/main/java/de/edotzlaff/maps0123/map.java @@ -0,0 +1,131 @@ +package de.edotzlaff.maps0123; + +import android.Manifest; +import android.content.pm.PackageManager; +import android.location.Location; +import android.os.Bundle; +import android.util.Log; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; + +import com.google.android.gms.location.FusedLocationProviderClient; +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 com.google.android.gms.tasks.OnCompleteListener; +import com.google.android.gms.tasks.Task; + +public class map extends AppCompatActivity implements OnMapReadyCallback { + + + private static final String TAG = "MainActivity"; + private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION; + private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234; + + //vars + private Boolean mLocationPermissionsGranted = false; + private GoogleMap mMap; + private FusedLocationProviderClient mFusedLocationProviderClient; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_maps); + + getLocationPermission(); + } + + private void getLocationPermission() { + String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION}; + if (ContextCompat.checkSelfPermission(this.getApplicationContext(), FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { + mLocationPermissionsGranted = true; + initMap(); + } else { + ActivityCompat.requestPermissions(this, permissions, LOCATION_PERMISSION_REQUEST_CODE); + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + mLocationPermissionsGranted = false; + + switch (requestCode) { + case LOCATION_PERMISSION_REQUEST_CODE: { + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + mLocationPermissionsGranted = true; + //initalize or map + initMap(); + } + } + } + } + + private void initMap() { + SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); + mapFragment.getMapAsync(this); + } + + public void onMapReady(GoogleMap googleMap) { + Toast.makeText(this, "Map is ready", Toast.LENGTH_SHORT).show(); + mMap = googleMap; + + if (mLocationPermissionsGranted) { + getDeviceLocation(); + + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) + != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, + Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + + return; + } + mMap.setMyLocationEnabled(true); + } + // Add a marker in Sydney and move the camera + //LatLng sydney = new LatLng(-50, 151); + //mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); + //mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); + + } + + private void getDeviceLocation(){ + mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); + + try { + if (mLocationPermissionsGranted){ + final Task location = mFusedLocationProviderClient.getLastLocation(); + + location.addOnCompleteListener(new OnCompleteListener() { + + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()){ + + Location currentLocation = (Location) task.getResult(); + moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()),15f); + + } + else{ + Toast.makeText(map.this, "Current Location unavailable", Toast.LENGTH_SHORT).show(); + } + } + }); + } + }catch (SecurityException e){ + Log.e(TAG,"Device Location not found" + e.getMessage()); + } + } + + private void moveCamera(LatLng latlng, float zoom){ + Log.d(TAG,"Latitude: "+latlng.latitude+"Longitude: "+latlng.longitude); + mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, zoom)); + } + +} \ No newline at end of file diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_map.xml b/app/src/main/res/layout/activity_map.xml new file mode 100644 index 0000000..b76ca31 --- /dev/null +++ b/app/src/main/res/layout/activity_map.xml @@ -0,0 +1,15 @@ + + + +