Skip to content

Instantly share code, notes, and snippets.

@JacquesInnocent
Last active December 17, 2022 05:02
Show Gist options
  • Save JacquesInnocent/8c6cad11070b19a9f0b9215ec5c55377 to your computer and use it in GitHub Desktop.
Save JacquesInnocent/8c6cad11070b19a9f0b9215ec5c55377 to your computer and use it in GitHub Desktop.
Google Maps current location activity.
<Relative Layoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_patent"
android:layout_height="match_patent"
xmlns="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/tools">
<LinearLayout
android:id="@+id/lll"
android:layout_height = "wrap_content"
android:layout_width = "match_patent"
android:orientation = "horizontal"
android:weightSum = "5"
<EditText
android:id = "@+id/etLocationEntry"
android:layout_width = "0dp"
android:layout_height = "wrap_content"
android:layout_weight = "4"
/>
<Button
android:id = "@+id/enSearch"
android:layout_width = "0dp"
android:layout_height = "wrap_content"
android:layout_weight = "1"
android:text = "Go"
/>
</LinearLayout>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.map.myMap.MapsActivity"
tools:layout="@layout/abc_activity_chooser_view_list_item" />
//change .map.myMap in line 2 to your company name and app name input in the begining of activity creation.
package com.map.myMap;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
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 implements OnMapReadyCallback {
private GoogleMap mMap;
privaate final static int MY_PERMISSION_FINE_LOCATION = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
if (ActivityCompact.checkSelfPermission (this, Manifest.permission.ACCESS_FINE_LOCATION) == packageManager.PERMISSION_GRANTED)
mMap.setMyLocationEnabled=(true);
} else {
if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.M) {
requestPermissions(new String[] Manifest.permission.ACCESS_FINE_LOCATION, MY_PERMISSION_FINE_LOCATION)};
}
}
@Override
public void onRequestPermissionsResult (int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult (requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSION_FINE_LOCATION:
if (grantResults[0] == packageManager.PERMISSION_GRANTED) {
if (ActivityCompact.checkSelfPermission (this, Manifest.permission.ACCESS_FINE_LOCATION) == packageManager.PERMISSION_GRANTED)
mMap.setMyLocationEnabled=(true);
}else {
Toast.MakeText (getApplicationContext(), "This app requires location permissions to be granted", Toast.LENGTH_LONG).show();
finish();
}
break;
}
}
//Below is the .xml codes for creating the functionality buttons.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment