Last active
November 21, 2022 19:22
-
-
Save NizarETH/8013b59266a2a57192743f18d5a12d33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1- layout MainActivity | |
2- google_maps_key | |
3- Gradle | |
4- Manifest | |
5- Fragment layout | |
6- Fragment | |
PS : Don't forget Dexter check | |
========================================================================= | |
Layout MainActivity | |
========================================================================= | |
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<Button | |
android:id="@+id/open_map" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="open Map!" | |
/> | |
<FrameLayout | |
android:id="@+id/frame" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@color/purple_200" | |
/> | |
</RelativeLayout> | |
========================================================================= | |
google_maps_key | |
========================================================================= | |
<string name="google_maps_key">AIzaSyBkPhhD_a0Mu2JNgtFCTguOElLSMk0Uiz4</string> | |
========================================================================= | |
Gradle | |
========================================================================= | |
plugins { | |
id 'com.android.application' | |
id 'org.jetbrains.kotlin.android' | |
} | |
android { | |
namespace 'com.ofppt.mylocation' | |
compileSdk 32 | |
defaultConfig { | |
applicationId "com.ofppt.mylocation" | |
minSdk 26 | |
targetSdk 32 | |
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 | |
} | |
kotlinOptions { | |
jvmTarget = '1.8' | |
} | |
} | |
dependencies { | |
implementation 'androidx.core:core-ktx:1.7.0' | |
implementation 'com.karumi:dexter:6.2.3' | |
implementation 'com.google.maps.android:android-maps-utils:2.3.0' | |
implementation 'com.google.android.gms:play-services-maps:18.0.2' | |
implementation 'com.google.android.gms:play-services-location:19.0.1' | |
implementation 'androidx.appcompat:appcompat:1.5.1' | |
implementation 'com.google.android.material:material:1.7.0' | |
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' | |
testImplementation 'junit:junit:4.13.2' | |
androidTestImplementation 'androidx.test.ext:junit:1.1.4' | |
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' | |
} | |
========================================================================= | |
Manifest | |
========================================================================= | |
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools"> | |
<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.ACCESS_BACKGROUND_LOCATION" /> | |
<uses-feature | |
android:glEsVersion="0x00020000" | |
android:required="true" /> | |
<application | |
android:allowBackup="true" | |
android:dataExtractionRules="@xml/data_extraction_rules" | |
android:fullBackupContent="@xml/backup_rules" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/Theme.MyLocation" | |
tools:targetApi="31"> | |
<activity | |
android:name=".MainActivity" | |
android:exported="true"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<meta-data | |
android:name="android.app.lib_name" | |
android:value="" /> | |
<meta-data | |
android:name="com.google.android.maps.v2.API_KEY" | |
android:value="@string/google_maps_key"/> | |
<meta-data | |
android:name="com.google.android.gms.version" | |
android:value="@integer/google_play_services_version" /> | |
</application> | |
<queries> | |
<intent> | |
<action android:name="android.intent.action.VIEW" /> | |
<data android:scheme="geo" /> | |
</intent> | |
</queries> | |
</manifest> | |
======================================================= | |
Fragment layout | |
======================================================= | |
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<androidx.fragment.app.FragmentContainerView | |
android:id="@+id/mapView" | |
android:name="com.google.android.gms.maps.SupportMapFragment" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@color/white" | |
/> | |
</LinearLayout> | |
======================================================= | |
Fragment | |
======================================================= | |
import android.annotation.SuppressLint | |
import android.location.Location | |
import android.os.Bundle | |
import android.os.Looper | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.fragment.app.Fragment | |
import com.google.android.gms.location.* | |
import com.google.android.gms.maps.* | |
import com.google.android.gms.maps.model.CameraPosition | |
import com.google.android.gms.maps.model.LatLng | |
import com.google.android.gms.maps.model.MarkerOptions | |
import com.google.android.gms.tasks.Task | |
class MapFragment : Fragment() { | |
private var fusedLocationClient: FusedLocationProviderClient? = null | |
private var v : View? = null | |
private val mLocationCallback: LocationCallback = object : LocationCallback() { | |
@SuppressLint("MissingPermission") | |
override fun onLocationResult(locationResult: LocationResult) { | |
val location = locationResult.lastLocation | |
if (location != null) { | |
var latitude = location.latitude.toString() | |
var longitude = location.longitude.toString() | |
val mapFragment = childFragmentManager.findFragmentById(R.id.mapView) as SupportMapFragment? | |
mapFragment?.getMapAsync(OnMapReadyCallback { map: GoogleMap -> | |
map.setMapType(GoogleMap.MAP_TYPE_NORMAL) | |
map.setMyLocationEnabled(false) | |
map.getUiSettings().setZoomControlsEnabled(true) | |
map.getUiSettings().setMyLocationButtonEnabled(true) | |
map.getUiSettings().setCompassEnabled(true) | |
map.getUiSettings().setRotateGesturesEnabled(true) | |
map.getUiSettings().setZoomGesturesEnabled(true) | |
val cameraPosition = CameraPosition.Builder() | |
.target(LatLng(latitude.toDouble(), longitude.toDouble())).zoom(16.0f) | |
.build() | |
map.animateCamera( | |
CameraUpdateFactory.newCameraPosition( | |
cameraPosition | |
) | |
) | |
}) | |
} | |
} | |
} | |
override fun onResume() { | |
super.onResume() | |
} | |
@SuppressLint("MissingPermission") | |
private fun requestNewLocationData() { | |
val mLocationRequest = LocationRequest() | |
mLocationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY | |
mLocationRequest.interval = 0 | |
mLocationRequest.fastestInterval = 0 | |
mLocationRequest.numUpdates = 1 | |
fusedLocationClient = LocationServices.getFusedLocationProviderClient(requireActivity()) | |
fusedLocationClient!!.requestLocationUpdates( | |
mLocationRequest, mLocationCallback, | |
Looper.myLooper()!! | |
) | |
} | |
@SuppressLint("MissingPermission") | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
v = inflater.inflate(R.layout.map, container, false) | |
fusedLocationClient = LocationServices.getFusedLocationProviderClient( | |
requireActivity() | |
) | |
fusedLocationClient!!.lastLocation.addOnCompleteListener { task: Task<Location?> -> | |
val location = task.result | |
if (location == null) { | |
requestNewLocationData() | |
} else { | |
val mapFragment = childFragmentManager.findFragmentById(R.id.mapView) as SupportMapFragment? | |
mapFragment?.getMapAsync(OnMapReadyCallback { map: GoogleMap -> | |
map.setMapType(GoogleMap.MAP_TYPE_NORMAL) | |
map.setMyLocationEnabled(false) | |
map.getUiSettings().setZoomControlsEnabled(true) | |
map.getUiSettings().setMyLocationButtonEnabled(true) | |
map.getUiSettings().setCompassEnabled(true) | |
map.getUiSettings().setRotateGesturesEnabled(true) | |
map.getUiSettings().setZoomGesturesEnabled(true) | |
val cameraPosition = CameraPosition.Builder() | |
.target(LatLng(location.latitude.toDouble(),location. longitude.toDouble())).zoom(16.0f) | |
.build() | |
map.animateCamera( | |
CameraUpdateFactory.newCameraPosition( | |
cameraPosition | |
) | |
) | |
map.addMarker( | |
MarkerOptions() | |
.position(LatLng(location.latitude, location.longitude)) | |
.title("Marker is here !") | |
) | |
}) | |
} | |
} | |
return v | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment