Monday, May 5, 2014

Android:decodepoly(String str) function to decode the encoded polyline string

We know that JSON is the most popular data exchange format in internet world.While making android app we can use the different APIs provided by Google.To access those API, we use Parser Class which parse the encoded json format of data into String.
While using the Direction API of Google,the decodepoly(String str) function is used to decode the json data.While parsing we get a encoded string polyline,the that string must be passed to the decode function decodepoly(String str):

The code for the polyline decoder decodepoly(String str) is as following:

private List<LatLng> decodePoly(String encoded) {

        List<LatLng> poly = new ArrayList<LatLng>();
        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;

        while (index < len) {
            int b, shift = 0, result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;

            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;

            LatLng p = new LatLng((((double) lat / 1E5)),
                    (((double) lng / 1E5)));
            poly.add(p);
        }

        return poly;

    }
Note:This function returns the List of LatLng value using which we can draw route in Google Map

Saturday, May 3, 2014

Android:Find the Latitude and Longitude value of any place

You can find the latitude and longitude value of any place by entering the name of the place in the editText.

Note:While entering the name of the place you must enter the detail name of the place.
For example:Enter "nepal,pokhara,bagar" instead of only "bagar"

1)The layout file activity_main.xml is like following:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.longitudelatitude.MainActivity"
    tools:ignore="MergeRootFrame" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:onClick="find_Click"
        android:text="Find Latitude and Longitude" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button1" />

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button2" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:onClick="lim_Click"
        android:text="Look In Map" />
   
</RelativeLayout>

2)The MainActivity.java file is like the following.You can also look that place in the google map after clicking the button Look In Map

Android:Current Location Latitude and Longitude Value


The following complete program is able to show your current location.And it also show your(device's) current location if it is moved by small distance.This program checks the position of your android device in every 20 secs interval.
I hope this will help for the learner:
1) The layout contains one text view to show the latitude and longitude value and a map fragment to show the marker at your current location:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.currentlocation.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:textSize="20sp" />

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/textView1" />

</RelativeLayout>
2)The main activity contains a method called onLocationChanged() which is the main method to show the location update:

Saturday, March 29, 2014

Android:Displaying Google Map

Displaying Google Map in android project using Google Map API V2:

If you want to use Google Map API V2 to show Google Map in your android app, then you must follow the following steps:
  1. Open SDK manager and install Google Play Services from SDK manager and import that google-play-services_lib(i.e. library project) into your workspace(File->Import->Android->Existing Android code Into Workspace->Browse the google-play-services_lib from your sdk->extra->google->google-play-services->libproject)
  2. After importing the google-play-services_lib, you must use it in your project,for this: right click on your project->properties->android(in left)->Add->choose google-play-services_lib->Apply->OK
  3. Obtain Google API key. For this follow the steps following steps:
    • Create New Project(File->New->Android Application Project)
    •  Then Goto(Window->Preferences->Android->Build) and copy the SHA1 fingerprint
    • Open Google API Console and Register your project and Get API key for your android project(https://code.google.com/apis/console and create new project if you are new and click on API Access and then Create New Android Key and Paste that copied SHA1 fingerprint then semi-colon(;) and then your project's package name(com.example.secondmap))
  4. Your layout file(main_activity.xml) must contain this code: 
 <?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>
5.Your  activity file(MainActivity.java ) must contain this code: