Site icon ParallelCodes

Android Add Markers to Google Maps and Get LatLng Address

In this post we will see how we can add Google Maps markers and get its location and latitude in Android application. You can integrate Google maps in your android application for different purposes like assisting your users to guide to a particular address, to show certain important locations, to get Latitude and Longitudes of a location and further more. In this application I have integrated Google Android maps API to mark locations on Google maps and read its Latitude and Longitude and Google location info of the Map Markers.
We will also change google map style to Normal and Satellite mode.

DOWNLOAD EXAMPLE

So let’s begin.

Android Add Markers to Google Maps and Get LatLng Address:

Make a Layout file in your Android application with name mapsmarkers.xml and edit it as below.

res>layout>mapsmarkers.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">

<TextView
android:id="@+id/txtMarkerText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="left|center_vertical"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:text="Touch anywhere on Map's Marker to get its address and LatLng."
android:textColor="@color/colorAccent"
android:textSize="16sp"
android:textStyle="bold" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">

<Button
android:id="@+id/btntype1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="@color/colorPrimaryDark"
android:padding="3dp"
android:text="Satellite"
android:textColor="#fff" />

<Button
android:id="@+id/btntype2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="@color/colorPrimaryDark"
android:padding="3dp"
android:text="Normal"
android:textColor="#fff" />

</LinearLayout>

<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8" />
</LinearLayout>

Make a class file in your project package with name MapsMarkers.java and edit it as below:

MapsMarkers.java:

package com.map.parallelcodes;

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;
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.MapStyleOptions;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class MapsMarkers extends AppCompatActivity implements OnMapReadyCallback {

GoogleMap mMap;
Geocoder geo;
TextView txtMarkers;
Button btntype1,btntype2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapsmarkers);
txtMarkers = (TextView) findViewById(R.id.txtMarkerText);
final SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
btntype1 = (Button) findViewById(R.id.btntype1);
btntype2 = (Button) findViewById(R.id.btntype2);
btntype1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mMap != null)
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
});

btntype2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mMap != null)
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
});
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (mMap != null) {
geo = new Geocoder(MapsMarkers.this, Locale.getDefault());

mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
try {
if (geo == null)
geo = new Geocoder(MapsMarkers.this, Locale.getDefault());
List<Address> address = geo.getFromLocation(latLng.latitude, latLng.longitude, 1);
if (address.size() > 0) {
mMap.addMarker(new MarkerOptions().position(latLng).title("Name:" + address.get(0).getCountryName()
+ ". Address:" + address.get(0).getAddressLine(0)));
txtMarkers.setText("Name:" + address.get(0).getCountryName()
+ ". Address:" + address.get(0).getAddressLine(0));
}
} catch (IOException ex) {
if (ex != null)
Toast.makeText(MapsMarkers.this, "Error:" + ex.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}
});

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
txtMarkers.setText(marker.getTitle().toString() + " Lat:" + marker.getPosition().latitude + " Long:" + marker.getPosition().longitude);
return false;
}
});
}

}
}

Upon pressing the Android Buttons, maps style will change:

Normal : Change the Google Map mode to Normal view.

Satellite: Change the Google Map mode to Satellite view.

On selecting any place on Google maps in this Android application, it will make a Google markers on the selected location and display its Country name and its local Address.

DOWNLOAD EXAMPLE


Exit mobile version