Custom Map in Android Java

52 Views

In this tutorial, you’ll learn how to build a simple Android application that uses the Google Map API to search for a specific address and show its details with the aid of the Geocoder instance.

PLEASE NOTE: This tutorial has been written using Android Studio 3.5.x and Java

The Android project

Let’s start by creating a new project. Open Android Studio and click + Start a new Android Studio project.

Select the Empty Activity and click the Next button.

On the popup window, type a name for your project – like Map.

When you set an Android Studio’s project name, it’s always good to leave no spaces between words.

Type the package name you want for your application, select the location of the files and click the Finish button.

When Android Studio will be done on creating your project, open the Manifest.xml file and make it look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          package="com.xstutorials.map">

    <!-- permissions -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-feature android:name="android.hardware.camera"/>
    <uses-permission android:name="android.permission.CAMERA"/>

    <permission android:name="com.xstutorials.map.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
    <uses-permission android:name="com.xscoder.map.permission.MAPS_RECEIVE"/>


    <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"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light"
        tools:ignore="GoogleAppIndexingWarning">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>



        <!-- Google Maps -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key"/>


        <!-- Other Activities -->
        <activity android:name=".LocationDetails"/>

    </application>

</manifest>

In this file, we set the needed permissions for Internet and Location, as well as the MainActivity and an additional screen called LocationDetails.

We also implement the meta-data for the Google Maps API Key. You must obtain a key from your Google Cloud Platform, so in case you don’t have an Android Google Map API Key yet, visit this link to get one.

Once you got it, open the strings.xml file - it's inside the res/values folder in Android Studio – and paste it as it follows:

<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR_API_KEY_HERE</string>

Open the build.gradle (Module:app) file and place these 2 lines inside the dependencies class:

implementation 'com.google.android.gms:play-services-maps:17.0.0'
 implementation 'com.google.android.gms:play-services-location:17.0.0'

Those dependencies are needed to make our app work fine on getting a location and displaying a custom Map.

The Home screen design

You start by may downloading a few png images used for this tutorial here, and drag them inside the drawable folder of your project. You can open such folder by doing a right-click over the drawable file and select Reveal in Finder.

Now open the activity_main.xml file with the Text tab and paste the following code inside it:

<?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">


    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="#ffef0a">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center_vertical"
            android:text="Map "
            android:textAlignment="center"
            android:textColor="#333"
            android:textStyle="bold"/>

        <Button
            android:id="@+id/setLocationButt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="10dp"
            android:background="@android:color/transparent"
            android:text="Set Location"
            android:textAllCaps="false"
            android:textSize="11sp"
            android:textStyle="bold"/>
    </RelativeLayout>

    <EditText
        android:id="@+id/searchTxt"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/relativeLayout"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:backgroundTint="@android:color/transparent"
        android:ems="10"
        android:hint="Type an address or city name and hit Search "
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:maxLines="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:singleLine="true"
        android:textColor="#333"
        android:textSize="11sp"/>


    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/distanceTxt"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/searchTxt"/>

    <SeekBar
        android:id="@+id/distanceSeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:progressTint="#ffef0a"
        android:thumbTint="#ffef0a"/>

    <TextView
        android:id="@+id/distanceTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/distanceSeekBar"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="50 Km around your location"
        android:textAlignment="center"
        android:textColor="#333"
        android:textSize="12sp"/>

    <Button
        android:id="@+id/currentLocationButt"
        android:layout_width="44dp"
        android:layout_height="44dp"
        android:layout_alignBottom="@+id/mapView"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="60dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/curr_location_butt" />

    <Button
        android:id="@+id/satelliteButt"
        android:layout_width="44dp"
        android:layout_height="44dp"
        android:layout_above="@+id/currentLocationButt"
        android:layout_alignEnd="@+id/currentLocationButt"
        android:layout_marginBottom="10dp"
        android:background="@drawable/satellite_butt" />

</RelativeLayout>

Coding the MainActivity

Want to make some Java coding? All right, then open the MainActivity.java file and start by adding the OnMapReadyCallback and LocationListener implementations to the class declaration’s row:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, LocationListener {

Then, right below that line, declare some Views we’ll need to make this app work:

EditText searchTxt;
 TextView distanceTxt;
 Button mapTypeButt, currentLocButt, setLocButt;
 MapView mapView;
 GoogleMap gMap;
 Bundle mBundle;

Right below those instances, let’s now declare a few useful variables:

Context ctx = this;
 boolean mapIsSatellite = false;
 Location currentLocation;
 LocationManager locationManager;
 float zoom = 14;
 double radius = 1000;
 LatLng coords;

 // THESE GPS COORDINATES ARE THE ONES TO NEW YORK CITY. YOU CAN CHANGE THEM AS YOU WISH TO SET A DEFAULT LOCATION IN CASE YOUR DEVICE DOESN'T GET YOUR CURRENT LOCATION
 LatLng NEW_YORK_LOCATION = new LatLng(40.7143528, -74.0059731);


 // YOU CAN EDIT THIS VALUE AS YOU WISH, THIS IS THE DEFAULT DISTANCE RADIUS AROUND YOUR CHOOSEN LOCATION
 double distance = 50.0;

 // Multiple permissions array
 public static final int MULTIPLE_PERMISSIONS = 10;
 String[] permissions= new String[]{
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
 };

As you may see, we have a variable called NEW_YORK_LOCATION that stores a couple of float numbers, they are the GPS coordinates of New York City and the application will use those coordinates in case the built-in GPS antenna of your device won’t get your current location – it may happen on some low-cost Android devices.

Another important array is the permissions one. We’ll add some code later that will process it and make the app ask you for Location permission.

Now we have to add a public function that will help us firing an alert with a custom message by using a single line of code, so paste this code below the previous ones, and above the onCreate() function:

public static void simpleAlert(String mess, Context ctx) {
        AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
        alert.setMessage(mess).setTitle(R.string.app_name).setPositiveButton("OK", null).setIcon(R.drawable.logo);
        alert.create().show();
 }

Last variable, the TAG one, we’ll use it to Log and debug our application:

public static String TAG = "log-";

If you need to call some functions as soon as the Activity shows up, you can use the onStart() method. Let’s add it in our code now and use it to call a function that will check the device’s permissions:

@Override
 protected void onStart() {
    super.onStart();

    // Check permissions and call function
    if (checkPermissions()) { getCurrentLocation();
    } else { checkPermissions(); }
 }

You’ll get an error, seeing the instances in red color, but don’t worry, because we’ll add the necessary function later.

We have to initialize our Views inside the onCreate() method, so place this code in it:

distanceTxt = findViewById(R.id.distanceTxt);
 distanceTxt.setText((int) distance + " Km around your location");
 mapTypeButt = findViewById(R.id.satelliteButt);
 currentLocButt = findViewById(R.id.currentLocationButt);
 setLocButt = findViewById(R.id.setLocationButt);

One more thing to initialize, the most important for this app, is the mapView:

mapView = findViewById(R.id.mapView);
 MapsInitializer.initialize(ctx);
 mapView.onCreate(mBundle);

We have an EditText in our activity_main.xml layout that we can use to search for a specific address on the map. So now let’s put down the code to make it work, still inside the onCreate() function:

searchTxt = findViewById(R.id.searchTxt);
 searchTxt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String addressStr = searchTxt.getText().toString();
                    dismissKeyboard();

                    Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
                    List<android.location.Address> addresses;
                    try {
                        addresses = geocoder.getFromLocationName(addressStr, 1);
                        if (addresses.size() > 0) {

                            // Set new location's coords
                            double latitude = addresses.get(0).getLatitude();
                            double longitude = addresses.get(0).getLongitude();
                            currentLocation= new Location("provider");
                            currentLocation.setLatitude(latitude);
                            currentLocation.setLongitude(longitude);

                            // Set new location's coordinates
                            coords = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());

                            // Refresh MapView
                            refreshMapView();
                        }

                    } catch (IOException e) { e.printStackTrace(); }

                    return true;
                }
                return false;
            }
 });

We are going to initialize and perform a click listener for the SeekBar that handles the distance radius from the selected location and draws a yellow circle around it. Paster the following code inside the onCreate() method:

SeekBar distSeekBar = findViewById(R.id.distanceSeekBar);
        distSeekBar.setProgress((int) distance);
        distSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
                // Set minimum value to 5 Km
                if (progress >= 5) {
                    seekBar.setProgress(progress);
                } else {
                    seekBar.setProgress(5);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                gMap.clear();

                distance = seekBar.getProgress();
                distanceTxt.setText(distance + " Km around your location");

                // Set Map zoom math
                radius = distance * 1609;
                double scale = radius / 500;
                zoom = (int) (14 - Math.log(scale) / Math.log(2));

                // Refresh the Map
                refreshMapView();
 }});

There’s a couple of Buttons that overlay the mapView in our main screen, one is the mapTypeButt, the other one is the currentLocButt. We can set a click listener for both Buttons into onCreate():

mapTypeButt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mapIsSatellite = !mapIsSatellite;
                if (mapIsSatellite) { gMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                } else { gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); }
}});

currentLocButt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Call function
                getCurrentLocation();
}});

The mapTypebutt click listener changes the look of our mapView from Normal to Satellite.
The currentLocButt calls a function that will get your current location.

We’ll finish editing the onCreate() function with the last click listener for the setLocButt, the Button that will open a new screen and show us the full address of the selected location.

setLocButt.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              if (currentLocation != null) {
                  double lat = currentLocation.getLatitude();
                  double lng = currentLocation.getLongitude();

                  Intent i = new Intent(ctx, LocationDetails.class);
                  Bundle extras = new Bundle();
                  extras.putDouble("latitude", lat);
                  extras.putDouble("longitude", lng);
                  i.putExtras(extras);
                  startActivity(i);
              }
 }});

Outside the onCreate() method, let’s place the following code, which will use LocationManager to get your current position and it will refresh the mapView to display it:

protected void getCurrentLocation() {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_LOW);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        assert locationManager != null;
        String provider = locationManager.getBestProvider(criteria, true);

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                &&
                ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        assert provider != null;
        currentLocation = locationManager.getLastKnownLocation(provider);

        // Current Location found!
        if (currentLocation != null) {

            // Set coordinates
            coords = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());

            // Refresh MapView
            refreshMapView();

        // Try to get Current Location again
        } else { locationManager.requestLocationUpdates(provider, 1000, 0, this); }
    }


    @Override
    public void onLocationChanged(Location location) {
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                &&
                ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        locationManager.removeUpdates(this);
        currentLocation = location;

        // Current Location found!
        if (currentLocation != null) {

            // Set coordinates
            coords = new LatLng(location.getLatitude(), location.getLongitude());

            // Refresh MapView
            refreshMapView();

        // NO LOCATION FOUND!
        } else {
            simpleAlert("Failed to get your Location.\nGo into Settings and make sure Location Service is enabled. Meanwhile, the default location will be set", ctx);

            // Set Default Location
            currentLocation = new Location("provider");
            currentLocation.setLatitude(NEW_YORK_LOCATION.latitude);
            currentLocation.setLongitude(NEW_YORK_LOCATION.longitude);

            // Set coordinates
            coords = new LatLng(location.getLatitude(), location.getLongitude());

            // Refresh MapView
            refreshMapView();
        }
    }

    @Override public void onStatusChanged(String provider, int status, Bundle extras) {}
    @Override public void onProviderEnabled(String provider) {}
    @Override public void onProviderDisabled(String provider) {}

The code above first performs a search of your location, and in case it won’t find it, it’ll show a pin on New York.
You can’t run the app now to test it out yet, we’re missing a few more functions. Add this one right below the above code:

void refreshMapView() { mapView.getMapAsync(this); }

The next method will add a marker over the Map, draw a circle around the pin and set an Info Window popup that will show up when you’ll tap on the pin.

void addPinOnMap() {

        // Zoom the Google Map
        CameraUpdate camUpdate = CameraUpdateFactory.newLatLngZoom(coords, zoom);
        gMap.animateCamera(camUpdate);


        // Add a circle to the location
        Circle circle = gMap.addCircle(new CircleOptions()
                .center(coords)
                .radius(radius)
                .strokeColor(Color.YELLOW)
                .strokeWidth(1)
                .fillColor(Color.parseColor("#30FFEF0A")));
        circle.setCenter(coords);


        // Show Pin in the Map
        gMap.addMarker(new MarkerOptions()
                // Position
                .position(coords)
                // Info window Title
                .title("Hey, I'm here!")
                // Info window Subtitle
                .snippet("Click me to get Directions on Google Map!")
                // Marker icon
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pin))
        );



        //-----------------------------------------------
        // MARK - TAP ON THE INFO WINDOW TO OPEN GOOGLE MAPS APP - FOR DRIVING DIRECTIONS
        //-----------------------------------------------
        gMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                if (currentLocation != null) {
                    double lat = currentLocation.getLatitude();
                    double lng = currentLocation.getLongitude();
                    String uri = String.format(Locale.ENGLISH, "geo:%f,%f", lat, lng);
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                    startActivity(intent);
                }
        }});
 }

We’re almost done. Let’s add the onMapReady() function that will initialize our mapView based on the retrieved coordinates:

@Override public void onMapReady(GoogleMap googleMap) {
        gMap = googleMap;
        Log.i(TAG, "COORDINATES ON MAP READY: " + coords);

        // Check permissions
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }

        //-----------------------------------------------
        // MARK - GOOGLE MAP OPTIONS
        //-----------------------------------------------
        gMap.setMyLocationEnabled(true);
        gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        gMap.moveCamera(CameraUpdateFactory.newLatLng(coords));
        gMap.animateCamera(CameraUpdateFactory.zoomTo(10));

        // Add pin on Map
        addPinOnMap();
 }

In order to reset the mapView when exiting the screen, we have to implement 3 more short functions: onResume(), onPause() and onDestroy().

@Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
 }

The next method will simply dismiss the keyboard after searching for an address in the EditText:

void dismissKeyboard() {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        assert imm != null;
        imm.hideSoftInputFromWindow(searchTxt.getWindowToken(), 0);
}

We’re going to add the very last function now, as mentioned earlier, which is the code that will show the Permissions alert at app startup:

private  boolean checkPermissions() {
        int result;
        List<String> listPermissionsNeeded = new ArrayList<>();
        for (String p : permissions) {
            result = ContextCompat.checkSelfPermission(this, p);
            if (result != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(p);
            }
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[0]), MULTIPLE_PERMISSIONS );
            return false;
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == MULTIPLE_PERMISSIONS) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "ALL PERMISSIONS GRANTED!");

                // Call function
                getCurrentLocation();

            } else {
                StringBuilder perStr = new StringBuilder();
                for (String per : permissions) {
                    perStr.append("\n").append(per);
                }
            }
        }
 }

Done with the MainActivity.java code. Do not run the app yet, let’s design the Location Details screen first and implement its code, then you’ll be ready to test this app on your real device.

Design the Location Details screen

Create a new Empty Activity by doing a right-click on the package name of your project – in the left-side files list – select New -> Activity -> Empty Activity.

In the Activity Name field, type a no-spaces name like LocationDetails and click the Finish button.

Let Android Studio sync Gradle before opening any layout of java file. This will also declare your new Activity into the Manifest file.

Once the software is ready and the yellow notification on the top of the screen disappeared, enter the Text tab of the location_details.xml file and replace ConstraintLayout with RelativeLayout:

<?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=".LocationDetails">

</RelativeLayout>

Now, above the </RelativeLayout> closure, paste the following code:

<RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="#ffef0a">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center_vertical"
            android:text="Address"
            android:textAlignment="center"
            android:textColor="#333"
            android:textStyle="bold"/>

        <Button
            android:id="@+id/ldBackButt"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/relativeLayout"
            android:layout_alignTop="@+id/relativeLayout"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:background="@android:color/transparent"
            android:text="Back "
            android:textAllCaps="false"
            android:textSize="12sp"
            android:textStyle="bold"/>
    </RelativeLayout>

    <TextView
        android:id="@+id/ldAddressTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativeLayout"
        android:layout_centerHorizontal="true"
        android:layout_marginEnd="30dp"
        android:layout_marginStart="30dp"
        android:layout_marginTop="20dp"
        android:text="ADDRESS:"
        android:textAlignment="center"
        android:textColor="#333"/>

This will create our layout screen.

Coding the LocationDetails

Inside the LocationDetails.java file, right above the onCreate() function, declare the following Views:

Context ctx = this;
 Button backButt;
 TextView addressTxt;

Then, into the onCreate() method, we first need to get the double values sent from our previous Activity, which are the latitude and longitude coordinates of the selected location on the map:

// Get data
 Bundle extras = getIntent().getExtras();
 assert extras != null;
 double lat = extras.getDouble("latitude");
 double lng = extras.getDouble("longitude");

Let’s initialize our Views:

backButt = findViewById(R.id.ldBackButt);
 addressTxt = findViewById(R.id.ldAddressTxt);

The code below will perform a Geocoder search of addresses based on the passed GPS coordinates, and it’ll show the first known address in our addressTxt TextView:

try { 
    Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
    List<Address> addresses;
    addresses = geocoder.getFromLocation(lat, lng, 1);
    if (Geocoder.isPresent()) {
                if (addresses != null) {
                    Address returnAddress = addresses.get(0);
                    String address = returnAddress.getAddressLine(0);
                    String city = returnAddress.getLocality();
                    String country = returnAddress.getCountryName();
                    String zipCode = returnAddress.getPostalCode();

                    // Show address
                    addressTxt.setText("ADDRESS:\n" + address + ", " + city + " - " + country + " - " + zipCode);

     } else { simpleAlert("The geocoder for your selected location is not present.", ctx); }
  } else { simpleAlert("The geocoder for your selected location is not present.", ctx); }
} catch (IOException e) { simpleAlert("The geocoder for your selected location is not present.", ctx); }

Almost done, only one small piece of code is missing, the click listener of our backButt button:

backButt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) { finish(); }});

Wow, we can finally run and test our application. If you’ve done everything correctly, you should see something like this:

Conclusion

That’s all for this tutorial, you have learned how to create a simple Android application that shows a Google Map View and displays a custom marker on a current or searched location.

Hope you enjoyed this article, feel free to post comments about it. You can also download the full Android Studio project of this article, just click the link below:

Download the Android Studio project

Buy me a coffee - XScoder - thanks for your support
Your support will be highly appreciated 😉