LocationPicker

Additional

Language
Java
Version
2.0 (Jul 22, 2020)
Created
Jul 2, 2019
Updated
Jul 22, 2020
Owner
Shivpujan yadav (shivpujan12)
Contributors
Shivpujan yadav (shivpujan12)
Rohan Arora (RohanArora13)
2
Activity
Badge
Generate
Download
Source code
APK file

Advertisement

Introduction

LocationPicker is a simple and easy to use library that can be integrated into your project. The project is build with androidx. All libraries and APIs used in the project are up-to-date as of now You can get latitude,longitude and full address for selected location Please follow below instruction to know how to use it in your project






Getting Started

Gradle Integration Add it in your root build.gradle at the end of repositories: step 1:

  allprojects {
  repositories {
   ...
   maven { url 'https://jitpack.io' }
  }
 }

step 2: Add the dependency

  dependencies {
         implementation 'com.github.shivpujan12:LocationPicker:2.0'
 }
  1. Configure your app in Google API Console to get API Key and enable services.

    https://console.cloud.google.com/apis/dashboard

    1. Enable below services in API Console.
      Google Maps Android API Google Places API for Android Declare the following things in manifest in AndroidManifest.xml file <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permissionandroid:name="android.permission.INTERNET" /> <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" />

      inside

      <application> tag add <meta-data> as shown below <applicationandroid:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme"> <activityandroid:name=".MainActivity"> <intent-filter> <actionandroid:name="android.intent.action.MAIN" /> <categoryandroid:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> .... .... <meta-dataandroid:name="com.google.android.geo.API_KEY"android:value="@string/your_api_key" /> </application>

      Note: Create the 'your_api_key' string resource and add your api key there

      To use the LocationPicker in your activity add the following code:

      i) Inside onCreate method intialize your api key as below:

      MapUtility.apiKey = getResources().getString(R.string.your_api_key);

      Note: Create the 'your_api_key' string resource and add your api key there

      ii) Start Location picker request by putting below code in your view

      Intent i =newIntent(MainActivity.this, LocationPickerActivity.class); startActivityForResult(i, ADDRESS_PICKER_REQUEST);
    2. Handle your onActivityResult for getting address, latitude and longitude as:

      @Overrideprotectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
                  super.onActivityResult(requestCode, resultCode, data);
      
                  if (requestCode ==ADDRESS_PICKER_REQUEST) {
                      try {
                          if (data !=null&& data.getStringExtra(MapUtility.ADDRESS) !=null) {
                              // String address = data.getStringExtra(MapUtility.ADDRESS);double currentLatitude = data.getDoubleExtra(MapUtility.LATITUDE, 0.0);
                              double currentLongitude = data.getDoubleExtra(MapUtility.LONGITUDE, 0.0);
                              Bundle completeAddress =data.getBundleExtra("fullAddress");
                          /* data in completeAddress bundle                    "fulladdress"                    "city"                    "state"                    "postalcode"                    "country"                    "addressline1"                    "addressline2"*/
                              txtAddress.setText(newStringBuilder().append("addressline2: ").append
                                      (completeAddress.getString("addressline2")).append("\ncity: ").append
                                      (completeAddress.getString("city")).append("\npostalcode: ").append
                                      (completeAddress.getString("postalcode")).append("\nstate: ").append
                                      (completeAddress.getString("state")).toString());
      
                              txtLatLong.setText(newStringBuilder().append("Lat:").append(currentLatitude).append
                                      ("  Long:").append(currentLongitude).toString());
      
                          }
                      } catch (Exception ex) {
                          ex.printStackTrace();
                      }
                  }
          }