ICP 5 - Hiresh12/CS5551_Team6_1_17_ICP_1 GitHub Wiki

Team Details

Team-6

  1. Hiresh Jakkala Bhaskar(class id - 17)
  2. Bharath Kumar Eilane(class id - 12)

Objective

  1. To create an Android application to display the user's current location on google map with a marker on the map along with longitude and latitude.

Google API Key

To Access Google Geocoding API, we need to get API key first. Goto the following URL https://developers.google.com/maps/documentation/android-api/current-place-tutorial

  • Select/Create a project to generate the API Key and add billing details. Google will generate a API Key (Refer below image) Copied the key and added it to my project,
    <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key" />

Code Explanation

UI


<Button android:id="@+id/main_btn_record" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onLocationClick" android:text="Display Location" android:layout_marginTop="54dp" android:layout_below="@+id/main_btn_photo" android:layout_centerHorizontal="true"></Button>

  • Button (Display Location) is placed in the Homepage to show the location to the user. BottonOnClickEvent (onLocationClick) is assigned to that button.

  • Once the button is clicked, below code will redirect the app from main activity to map activity.
    public void onLocationClick(View v) { //This code redirects to the photo activity. Intent redirect = new Intent(MainActivity.this, MyMapsActivity.class); startActivity(redirect); }

  • In Map Activity, Fragment is used to display the google map to the user.
    For Google map, we need to add "android:name="com.google.android.gms.maps.SupportMapFragment"" in Frangments tag. <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.example.rohithkumar.cameramapsapplication.MyMapsActivity" />

  • After the app redirected to map activity, we need to check for Device location access , if we don't have access, request user to give permission to access device location. Refer below code,
    if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1); }

  • getMapAsync method is used to intialize the google map, this method will wait till the map is properly initialized and provides the Map instance via a callback.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this);

  • onMapReady event will be triggered once the map is initialized properly. In this we need to use location service to get the device's current location.
    public void onMapReady(GoogleMap googleMap) { mMap=googleMap;})

  • LocationManager class is used to get the device's location. LocationManager has getLastKnownLocation method which will return Location object.
    getLatitude() and getLongitude() of Location class are used to get the coordinates. locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria crit = new Criteria(); crit.setAccuracy(Criteria.ACCURACY_FINE); Location lc=locationManager.getLastKnownLocation(locationManager.getBestProvider(crit,true)); latitude=lc.getLatitude(); longitude=lc.getLongitude(); LatLng sydney = new LatLng(latitude, longitude);

  • Reverse geocoding is the process of converting coordinated in to human readable address. getFromLocation method of GeoCoder class is used to get the address that belongs to the above resulted coordinates.
    Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); String _Location=""; try { List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1); if(null!=listAddresses&&listAddresses.size()>0){ _Location = listAddresses.get(0).getAddressLine(0); } }

  • addMarker() method is used to add marker and title to the map.
    mMap.addMarker(new MarkerOptions().position(sydney) .title(_Location.substring(0,_Location.indexOf(','))) .snippet("Latitude :"+Double.toString(latitude)+"\n Longitude :"+Double.toString(longitude))); mMap.setInfoWindowAdapter(new CustominfoWindow(MyMapsActivity.this)); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,15));

  • Custom Layout is used to display the title of the marker.
    `

      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/title"
          android:layout_gravity="center_horizontal"
          android:ellipsize="end"
          android:maxLines="1"
          android:textColor="#000"
          android:textSize="14sp"
          android:textStyle="bold"/>
    
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:ellipsize="end"
          android:maxLines="10"
          android:textSize="14sp"
          android:textColor="#000"
          android:id="@+id/snippet"/>
    

    `

`private void rendowWindowText(Marker marker, View view){

    String title = marker.getTitle();
    TextView tvTitle = (TextView) view.findViewById(R.id.title);

    if(!title.equals("")){
        tvTitle.setText(title);
    }

    String snippet = marker.getSnippet();
    TextView tvSnippet = (TextView) view.findViewById(R.id.snippet);

    if(!snippet.equals("") && snippet!=null){
        tvSnippet.setText(snippet);
    }
}`

Output

Conclusion

Finally Android app has been developed to display user's current location in google map.

GitHub Link

https://github.com/Hiresh12/CS5551_Team6_1_17_ICP_1/tree/master/Documents/ICP_5

Video Link

https://youtu.be/KX13Q4YvyjU

References

https://developer.android.com/reference/android/location/Geocoder
https://developer.android.com/reference/android/location/LocationManager

⚠️ **GitHub.com Fallback** ⚠️