Lost? Not anymore: Android Location Finder App Guide You
FREE Online Courses: Click, Learn, Succeed, Start Now!
Location-based apps have become essential in our day-to-day life. They are used in various fields, such as navigation, travel, tourism, and logistics. In this project, we will be creating a location finder Android app that uses the Mapmyindia API and SDK for Android. This Android Location Finder app will allow users to search for a place and get its location, along with the path on the map.
Additionally, the Android Location Finder app will also provide features such as showing traffic, non-traffic, and the fastest route to the destination for the three modes of travel, i.e., walking, Bike, and driving.
About Android Location Finder App
The objective of this project is to create a location finder Android app that uses the Mapmyindia API and SDK. By the end of this project, you will have a working app that can find the location of any place and display it on the map.
You will also be able to incorporate additional features, such as showing traffic, non-traffic, and the fastest route to the destination, based on the user’s preferred mode of travel.
Prerequisites for Location Finder App using Android
To follow this project, you should have a basic understanding of Android development and Java programming language. You should also have Android Studio installed on your computer and a Mapmyindia account to access their API and SDK.
Download Android Location Finder App Project
Please download the source code of the Android Location Finder Project from the following link: Android Location Finder App Project Code.
Steps to Create Location Finder Project App using Android
Following are the steps for developing the Android Location Finder Project:
Step 1: Create an account on Mapmyindia to use their map API. After creating the account, you will get a default project.
Step 2: Now add the dependency for mapmyindia android sdk in the app level gradle file.
implementation 'com.mapmyindia.sdk:mapmyindia-android-sdk:6.8.2'
Step 3: Now, to use the maps in the app, we need to set the credentials we get from the mapmyindia console to the app in the oncreate() method
MapmyIndiaAccountManager.getInstance().setRestAPIKey("Your-Rest-API key"); MapmyIndiaAccountManager.getInstance().setMapSDKKey("Your-Map-SDK-key"); MapmyIndiaAccountManager.getInstance().setAtlasClientId("Your-client-ID"); MapmyIndiaAccountManager.getInstance().setAtlasClientSecret("Your-Client secret-key"); MapmyIndiaAccountManager.getInstance().setAtlasGrantType("client_credentials");
Step 4: Create the main home page. It will show a search bar to the user. According to the word types, it will show suggestions in the box below.
Activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.mapbox.mapboxsdk.maps.MapView android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:animateLayoutChanges="true" android:background="#00FFFFFF" android:id="@+id/topLL"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginStart="20sp" android:layout_marginTop="20sp" android:layout_marginEnd="20sp" android:background="@drawable/gradiant3" android:drawablePadding="20sp" android:id="@+id/where" android:hint="Search here" android:paddingStart="15sp" android:paddingEnd="15sp" android:textColor="@color/black" android:textColorHint="#414242" android:textSize="20sp" android:drawableLeft="@drawable/map" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/autofill" android:layout_width="match_parent" android:layout_height="400dp" android:layout_marginTop="5dp" android:layout_marginBottom="5sp" android:layout_marginStart="20sp" android:visibility="visible" android:background="@drawable/gradiantimage" android:layout_marginEnd="20sp" android:scrollbars="vertical" /> </LinearLayout> </RelativeLayout> </FrameLayout>
MainActivity.java
package com.projectgurukul.locationfinder; import android.app.Activity; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; import androidx.annotation.NonNull; import androidx.annotation.RequiresApi; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.mapbox.mapboxsdk.MapmyIndia; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; import com.mmi.services.account.MapmyIndiaAccountManager; import com.mmi.services.api.autosuggest.MapmyIndiaAutoSuggest; Import com.mmi.services.api.autosuggest.model.AutoSuggestAtlasResponse; import com.projectgurukul.locationfinder.extras.SuggestedLocationRes; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public abstract class MainActivity extends Activity implements AutoFillAdapter.MyCallback { RecyclerView autofill_recycler; MapView mapView; public MapboxMap mapboxMap1; private RecyclerView.Adapter adapter; private ArrayList<String> placename = new ArrayList<>(); private ArrayList<String> placeaddress = new ArrayList<>(); private ArrayList<Double> latitude = new ArrayList<>(); private ArrayList<Double> longitude = new ArrayList<>(); private List<SuggestedLocationRes> autoFillRes = new ArrayList<SuggestedLocationRes>(); public abstract void onAppMapReady(MapboxMap mapboxMap); public abstract void onAutofillRowSelected(final double latitude, final double longitude); @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MapmyIndiaAccountManager.getInstance().setRestAPIKey("Key"); MapmyIndiaAccountManager.getInstance().setMapSDKKey("Key"); MapmyIndiaAccountManager.getInstance().setAtlasClientId("Key"); MapmyIndiaAccountManager.getInstance().setAtlasClientSecret("Key"); MapmyIndiaAccountManager.getInstance().setAtlasGrantType("Grant"); MapmyIndia.getInstance(this); setContentView(R.layout.activity_main); mapView = findViewById(R.id.mapView); mapView.getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(final MapboxMap mapboxMap) { mapboxMap1 = mapboxMap; mapboxMap.setMinZoomPreference(2.5); mapboxMap.setMaxZoomPreference(18.5); mapboxMap.getUiSettings().setRotateGesturesEnabled(false); mapboxMap.getUiSettings().setTiltGesturesEnabled(false); mapboxMap.setPadding(20, 20, 20, 20); mapboxMap.getUiSettings().setLogoMargins(0, 0, 0, 0); onAppMapReady(mapboxMap); } @Override public void onMapError(int i, String s) { } }); mapView.onCreate(savedInstanceState); EditText whereto = findViewById(R.id.where); whereto.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { if (whereto.getText().toString().length() > 1) { placename.clear(); placeaddress.clear(); latitude.clear(); longitude.clear(); MapmyIndiaAutoSuggest.builder() .query(whereto.getText().toString()) .build() .enqueueCall(new Callback<AutoSuggestAtlasResponse>() { @Override public void onResponse(@NonNull Call<AutoSuggestAtlasResponse> call, @NonNull Response<AutoSuggestAtlasResponse> response) { for (int i = 0; i < response.body().getSuggestedLocations().size(); i++) { placename.add(response.body().getSuggestedLocations().get(i).placeName); placeaddress.add(response.body().getSuggestedLocations().get(i).placeAddress); latitude.add(Double.parseDouble(response.body().getSuggestedLocations().get(i).latitude)); longitude.add(Double.parseDouble(response.body().getSuggestedLocations().get(i).longitude)); } autofill_recycler = findViewById(R.id.autofill); if (adapter == null) { adapter = new AutoFillAdapter(MainActivity.this, placename, placeaddress, MainActivity.this); autofill_recycler.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false)); autofill_recycler.setAdapter(adapter); } else { adapter.notifyDataSetChanged(); autofill_recycler.scrollToPosition(0); autofill_recycler.setVisibility(View.VISIBLE); } } @Override public void onFailure(@NonNull Call<AutoSuggestAtlasResponse> call, @NonNull Throwable t) { t.printStackTrace(); } }); if (s.toString().trim().length() == 0) { placename.clear(); placeaddress.clear(); latitude.clear(); longitude.clear(); if (adapter != null) adapter.notifyDataSetChanged(); autofill_recycler.setVisibility(View.GONE); } } } }); autofill_recycler = findViewById(R.id.autofill); autofill_recycler.addOnItemTouchListener(new RecyclerTouchListener(this, autofill_recycler, new ClickListener() { @Override public void onClick(View view, final int position) { autofill_recycler.setVisibility(View.GONE); onAutofillRowSelected(latitude.get(position), longitude.get(position)); } @Override public void onLongClick(View view, int position) { } })); } private static float round(float d, int decimalPlace) { BigDecimal bd = new BigDecimal(Float.toString(d)); bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP); return bd.floatValue(); } @Override protected void onStart() { super.onStart(); mapView.onStart(); } @Override protected void onStop() { super.onStop(); mapView.onStop(); } @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); } @Override protected void onPause() { super.onPause(); mapView.onPause(); } @Override protected void onResume() { super.onResume(); mapView.onResume(); } @Override public void onLowMemory() { super.onLowMemory(); mapView.onLowMemory(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } public interface ClickListener { void onClick(View view, int position); void onLongClick(View view, int position); } class RecyclerTouchListener implements RecyclerView.OnItemTouchListener { private ClickListener clicklistener; private GestureDetector gestureDetector; public RecyclerTouchListener(Context context, final RecyclerView recycleView, final ClickListener clicklistener) { this.clicklistener = clicklistener; gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent e) { return true; } @Override public void onLongPress(MotionEvent e) { View child = recycleView.findChildViewUnder(e.getX(), e.getY()); if (child != null && clicklistener != null) { clicklistener.onLongClick(child, recycleView.getChildAdapterPosition(child)); } } }); } @Override public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { View child = rv.findChildViewUnder(e.getX(), e.getY()); if (child != null && clicklistener != null && gestureDetector.onTouchEvent(e)) { clicklistener.onClick(child, rv.getChildAdapterPosition(child)); } return false; } @Override public void onTouchEvent(RecyclerView rv, MotionEvent e) { } @Override public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { } } }
Step 5: Creating Recycler View List. It will show the list of suggested places to the user according to the input provided.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rootLayout" android:layout_width="match_parent" android:layout_height="65sp" android:clickable="true" android:drawableStart="@drawable/placeholder" android:drawableLeft="@drawable/dot" android:elevation="20sp" android:foreground="?selectableItemBackground" android:orientation="horizontal" android:weightSum="1"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.9" android:gravity="center"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/location" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.1" android:orientation="vertical" android:weightSum="1"> <TextView android:id="@+id/placename" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginStart="10sp" android:layout_marginLeft="10sp" android:layout_marginEnd="15sp" android:layout_marginRight="15sp" android:layout_weight="0.5" android:ellipsize="end" android:gravity="bottom" android:singleLine="true" android:text="Project Gurukul" android:textColor="@color/black" android:textSize="17sp" /> <TextView android:id="@+id/placeaddress" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginStart="10sp" android:layout_marginLeft="10sp" android:layout_marginEnd="15sp" android:layout_marginRight="15sp" android:layout_weight="0.5" android:ellipsize="end" android:gravity="top" android:singleLine="true" android:text="Indore, Madhya Pradesh" android:textColor="#232323" android:textSize="13sp" /> </LinearLayout> </LinearLayout>
Step 6: Creating the Direction Layout. After the user selects the destination, this layout will show the path to the destination. It will also provide an option to select the mode of travelling and the type of route.
activity_direction_layout.xml <?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" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/direction_details_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/rg_resource_type" android:background="@drawable/gradiant7" android:gravity="center_vertical" android:minHeight="64dp" android:visibility="visible" android:layout_alignParentTop="true" android:padding="8dp" android:weightSum="1" android:orientation="horizontal"> <TextView android:id="@+id/tv_distance" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#2E2E2E" android:textSize="20sp" android:layout_weight="0.5" android:textStyle="bold" android:text="Distance" android:gravity="center_horizontal" /> <View android:layout_width="3dp" android:layout_height="match_parent" android:background="#000000"/> <TextView android:id="@+id/tv_duration" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="@color/black" android:layout_weight="0.5" android:layout_marginLeft="4dp" android:text="Duration" android:gravity="center_horizontal" /> </LinearLayout> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout_profile" android:layout_width="match_parent" android:layout_height="80dp" android:background="@drawable/gradiant6" android:layout_alignParentBottom="true" app:tabIndicatorColor="@null"> <com.google.android.material.tabs.TabItem android:id="@+id/tab_walk" android:layout_width="match_parent" android:layout_height="wrap_content" android:icon="@drawable/walk" android:text="Walk" /> <com.google.android.material.tabs.TabItem android:id="@+id/tab_bike" android:layout_width="match_parent" android:layout_height="wrap_content" android:icon="@drawable/bike" android:text="Bike" /> <com.google.android.material.tabs.TabItem android:id="@+id/tab_drive" android:layout_width="match_parent" android:layout_height="wrap_content" android:icon="@drawable/car" android:text="Drive" /> </com.google.android.material.tabs.TabLayout> <com.mapbox.mapboxsdk.maps.MapView android:id="@+id/map_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/rg_resource_type" android:layout_below="@id/direction_details_layout"> </com.mapbox.mapboxsdk.maps.MapView> <RadioGroup android:id="@+id/rg_resource_type" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:weightSum="3" android:minHeight="32dp" android:background="@drawable/gradiant8" android:layout_above="@id/tab_layout_profile"> <RadioButton android:id="@+id/rb_without_traffic" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:checked="true" android:textColor="@color/black" android:textSize="15sp" android:buttonTint="#E91E63" android:text="Non Traffic" /> <RadioButton android:id="@+id/rb_with_traffic" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@color/black" android:textSize="15sp" android:buttonTint="#E91E63" android:text="Traffic"/> <RadioButton android:id="@+id/rb_with_route_eta" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@color/black" android:textSize="15sp" android:buttonTint="#E91E63" android:text="Fastest Route" /> </RadioGroup> </RelativeLayout>
DirectionActivity.java
package com.projectgurukul.locationfinder; import android.Manifest; import android.content.pm.PackageManager; import android.location.Location; import android.os.Bundle; import android.view.View; import android.widget.LinearLayout; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import com.google.android.material.tabs.TabLayout; import com.mapbox.android.core.location.LocationEngine; import com.mapbox.android.core.location.LocationEngineListener; import com.mapbox.core.constants.Constants; import com.mapbox.geojson.Point; import com.mapbox.geojson.utils.PolylineUtils; import com.mapbox.mapboxsdk.annotations.IconFactory; import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.annotations.MarkerOptions; import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.geometry.LatLngBounds; import com.mapbox.mapboxsdk.location.LocationComponent; import com.mapbox.mapboxsdk.location.LocationComponentOptions; import com.mapbox.mapboxsdk.location.modes.CameraMode; import com.mapbox.mapboxsdk.location.modes.RenderMode; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; import com.mmi.services.api.directions.DirectionsCriteria; import com.mmi.services.api.directions.MapmyIndiaDirections; import com.mmi.services.api.directions.models.DirectionsResponse; import com.mmi.services.api.directions.models.DirectionsRoute; import com.projectgurukul.locationfinder.extras.DirectionPolylinePlugin; import com.projectgurukul.locationfinder.extras.Storage; import com.projectgurukul.locationfinder.extras.TransparentProgressDialog; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; import java.util.Objects; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public class DirectionActivity extends AppCompatActivity implements OnMapReadyCallback, LocationEngineListener { private MapboxMap mapmyIndiaMap; private LocationEngine locationEngine; private MapView mapView; private TransparentProgressDialog transparentProgressDialog; private String profile = DirectionsCriteria.PROFILE_DRIVING; private TabLayout profileTabLayout; private String resource = DirectionsCriteria.RESOURCE_ROUTE; private LinearLayout directionDetailsLayout; private TextView tvDistance, tvDuration; private DirectionPolylinePlugin directionPolylinePlugin; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_direction_layout); mapView = findViewById(R.id.map_view); profileTabLayout = findViewById(R.id.tab_layout_profile); RadioGroup rgResource = findViewById(R.id.rg_resource_type); directionDetailsLayout = findViewById(R.id.direction_details_layout); tvDistance = findViewById(R.id.tv_distance); tvDuration = findViewById(R.id.tv_duration); profileTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { if (mapmyIndiaMap == null) { if (profileTabLayout.getTabAt(0) != null) { Objects.requireNonNull(profileTabLayout.getTabAt(0)).select(); return; } } switch (tab.getPosition()) { case 0: profile = DirectionsCriteria.PROFILE_WALKING; rgResource.setVisibility(View.VISIBLE); break; case 1: profile = DirectionsCriteria.PROFILE_BIKING; rgResource.setVisibility(View.VISIBLE); break; case 2: profile = DirectionsCriteria.PROFILE_DRIVING; rgResource.setVisibility(View.VISIBLE); break; default: break; } getDirections(); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); rgResource.setOnCheckedChangeListener((radioGroup, i) -> { switch (radioGroup.getCheckedRadioButtonId()) { case R.id.rb_without_traffic: resource = DirectionsCriteria.RESOURCE_ROUTE; break; case R.id.rb_with_traffic: resource = DirectionsCriteria.RESOURCE_ROUTE_TRAFFIC; break; case R.id.rb_with_route_eta: resource = DirectionsCriteria.RESOURCE_ROUTE_ETA; break; default: break; } getDirections(); }); mapView.onCreate(savedInstanceState); mapView.getMapAsync(this); transparentProgressDialog = new TransparentProgressDialog(this, R.drawable.loading, ""); } @Override public void onMapReady(MapboxMap mapmyIndiaMap) { this.mapmyIndiaMap = mapmyIndiaMap; mapmyIndiaMap.setPadding(20, 20, 20, 20); mapmyIndiaMap.setCameraPosition(setCameraAndTilt()); getDirections(); } protected CameraPosition setCameraAndTilt() { return new CameraPosition.Builder().target(new LatLng( 22.719727, 75.858204)).zoom(14).tilt(0).build(); } private void progressDialogShow() { transparentProgressDialog.show(); } private void progressDialogHide() { transparentProgressDialog.dismiss(); } private void getDirections() { progressDialogShow(); MapmyIndiaDirections.builder() .origin(Point.fromLngLat(Storage.start_long, Storage.start_lat)) .destination(Point.fromLngLat(Storage.end_long, Storage.end_lat)) .profile(profile) .resource(resource) .steps(true) .alternatives(false) .overview(DirectionsCriteria.OVERVIEW_FULL).build().enqueueCall(new Callback<DirectionsResponse>() { @Override public void onResponse(@NonNull Call<DirectionsResponse> call, @NonNull Response<DirectionsResponse> response) { if (response.code() == 200) { if (response.body() != null) { DirectionsResponse directionsResponse = response.body(); List<DirectionsRoute> results = directionsResponse.routes(); if (results.size() > 0) { mapmyIndiaMap.clear(); MarkerOptions markerOptions = new MarkerOptions() .position(new LatLng(Storage.end_lat,Storage.end_long)) .icon(IconFactory.getInstance(DirectionActivity.this).fromResource(R.drawable.point)); markerOptions.setTitle("End Point"); markerOptions.setSnippet("Destination"); Marker marker = mapmyIndiaMap.addMarker(markerOptions); LocationComponentOptions options = LocationComponentOptions.builder(DirectionActivity.this) .trackingGesturesManagement(true) .accuracyColor(ContextCompat.getColor(DirectionActivity.this, R.color.purple_500)) .build(); LocationComponent locationComponent = mapmyIndiaMap.getLocationComponent(); if (ActivityCompat.checkSelfPermission(DirectionActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(DirectionActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } locationComponent.activateLocationComponent(DirectionActivity.this, options); locationComponent.setLocationComponentEnabled(true); locationEngine = locationComponent.getLocationEngine(); locationEngine.addLocationEngineListener(DirectionActivity.this); locationComponent.setCameraMode(CameraMode.TRACKING); locationComponent.setRenderMode(RenderMode.COMPASS); DirectionsRoute directionsRoute = results.get(0); if (directionsRoute != null && directionsRoute.geometry() != null) { drawPath(PolylineUtils.decode(directionsRoute.geometry(), Constants.PRECISION_6)); updateData(directionsRoute); } } } } else { Toast.makeText(DirectionActivity.this, response.message() + response.code(), Toast.LENGTH_LONG).show(); } progressDialogHide(); } @Override public void onFailure(@NonNull Call<DirectionsResponse> call, @NonNull Throwable t) { progressDialogHide(); t.printStackTrace(); } }); } private void updateData(@NonNull DirectionsRoute directionsRoute) { if (directionsRoute.distance() != null && directionsRoute.distance() != null) { directionDetailsLayout.setVisibility(View.VISIBLE); tvDuration.setText("ETA - (" + getFormattedDuration(directionsRoute.duration()) + ")"); tvDistance.setText("Distance - "+getFormattedDistance(directionsRoute.distance())); } } private String getFormattedDistance(double distance) { if ((distance / 1000) < 1) { return distance + "mtr."; } DecimalFormat decimalFormat = new DecimalFormat("#.#"); return decimalFormat.format(distance / 1000) + "Km."; } private String getFormattedDuration(double duration) { long min = (long) (duration % 3600 / 60); long hours = (long) (duration % 86400 / 3600); long days = (long) (duration / 86400); if (days > 0L) { return days + " " + (days > 1L ? "Days" : "Day") + " " + hours + " " + "hr" + (min > 0L ? " " + min + " " + "min." : ""); } else { return hours > 0L ? hours + " " + "hr" + (min > 0L ? " " + min + " " + "min" : "") : min + " " + "min."; } } private void drawPath(@NonNull List<Point> waypoints) { ArrayList<LatLng> listOfLatLng = new ArrayList<>(); for (Point point : waypoints) { listOfLatLng.add(new LatLng(point.latitude(), point.longitude())); } if(directionPolylinePlugin == null) { directionPolylinePlugin = new DirectionPolylinePlugin(mapmyIndiaMap, mapView, profile); directionPolylinePlugin.createPolyline(listOfLatLng); } else { directionPolylinePlugin.updatePolyline(profile, listOfLatLng); } LatLngBounds latLngBounds = new LatLngBounds.Builder().includes(listOfLatLng).build(); mapmyIndiaMap.animateCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 30)); } @Override public void onMapError(int i, String s) { } @Override protected void onStart() { super.onStart(); mapView.onStart(); } @Override protected void onStop() { super.onStop(); mapView.onStop(); } @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); } @Override protected void onPause() { super.onPause(); mapView.onPause(); } @Override protected void onResume() { super.onResume(); mapView.onResume(); } @Override public void onLowMemory() { super.onLowMemory(); mapView.onLowMemory(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } @Override public void onConnected() { } @Override public void onLocationChanged(Location location) { mapmyIndiaMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16)); } }
Android Location Finder Output
Summary
Congratulations! You have successfully created a location finder Android app that uses the Mapmyindia API and SDK. You can now search for any place and get its location displayed on the map.
Additionally, the Location Finder App also provides features such as showing traffic, non-traffic, and the fastest route to the destination for the three modes of travelling, i.e., Walking, Biking, and driving.
However, this is not the end; you can add more features according to your needs, such as incorporating voice navigation or integrating it with other apps.
If you are Happy with ProjectGurukul, do not forget to make us happy with your positive feedback on Google | Facebook