Thursday, January 15, 2015

Simple Android Application using Activities and Maps

This is a simple Android app that uses two activities. This app uses Google Maps API.


  1. Download Android studio and install
  2. Create a new blank activity - this is the launcher activity
  3. Login to google and enable Maps API by providing SHA-1 value of your client
    1. C:\Users\xyz>keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
    2. Register ssh-1 key in google API console along with application package name
    3. Get the key and save it in android manifest
  4. Launcher activity creates a new private class that extends AsyncTask
  5. This class is responsible to call the API to retrieve JSON rest call
  6. JSON output is parsed and we only retrieve few values we needed from the json hierarchy
  7. There is a second button that invokes a MapsActivity when clicked by passing latitude and longitude via intent
  8. This app uses free http://api.openweathermap.org weather API
Important file contents

google_maps_api.xml
<resources>
    <string name="google_maps_key_instructions" templateMergeStrategy="replace">
    <string name="google_maps_key" templateMergeStrategy="preserve">GoogleAPIKeyHere</string>
</resources>
MainActivity.java
package com.example.xyz.myapplication;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;


public class MainActivity3 extends ActionBarActivity  implements View.OnClickListener {

    public static final String RESTAPI_URL = "http://api.openweathermap.org/data/2.5/weather?q=";

    public static final String UNITS = "&units=metric";

    EditText result;
    EditText cityCountry;
    ProgressBar pb;
    Button button;

    String longitude;
    String latitude;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity3);

        pb = (ProgressBar) findViewById(R.id.progressBar2);
        pb.setVisibility(View.GONE);

        button = (Button) findViewById(R.id.show);
        button.setOnClickListener(this);

        Button mapButton = (Button) findViewById(R.id.showMap);
        mapButton.setOnClickListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main_activity3, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {

        if( v.getId() ==  R.id.showMap ) {
            Intent intent = new Intent(this, MapsActivity.class);
            intent.putExtra("lat", Double.parseDouble(latitude));
            intent.putExtra("long", Double.parseDouble(longitude));
            startActivity(intent);
        }
        //else

            result = (EditText)findViewById(R.id.result);
            cityCountry = (EditText)findViewById(R.id.cityCountry);
            pb = (ProgressBar) findViewById(R.id.progressBar2);
            pb.setVisibility(View.VISIBLE);

            //call web service
            AsyncTaskService service = new AsyncTaskService();
            service.execute(cityCountry.getText().toString());

    }


    private class AsyncTaskService extends AsyncTask<String, Integer, String> {

        String jSonContent;
        String resultToDisplay;

        @Override
        protected String doInBackground(String... params) {
            postData(params[0]);
            return null;
        }

        protected void onPostExecute(String serviceResult){
            pb.setVisibility(View.GONE);
            result.setText(resultToDisplay);
        }

        protected void onProgressUpdate(Integer... progress){
            pb.setProgress(progress[0]);
        }

        public void postData(String valueIWantToSend) {
            // HTTP Get
            BufferedReader reader = null;
            try {
                URL url = new URL(RESTAPI_URL+valueIWantToSend+UNITS);

                StringBuilder sb = new StringBuilder();
                String line = null;

                reader = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream()));
                // Read Server Response
                while((line = reader.readLine()) != null)
                {
                    // Append server response in string
                    sb.append(line + "");
                    System.out.println("Response:"+ line);
                }

                // Append Server Response To Content String
                jSonContent = sb.toString();

                parseJson(jSonContent);

            } catch (Exception e ) {
                System.out.println(e.getMessage());
            }
        }


        private void parseJson(String rawJson) {
            JSONObject jsonResponse;

            StringBuffer sb = new StringBuffer();
            try {
                jsonResponse = new JSONObject(rawJson);

                JSONObject coord = jsonResponse.getJSONObject("coord");
                longitude = coord.getString("lon");
                latitude = coord.getString("lat");

                sb.append("Latitude:" + latitude);
                sb.append("\nLongitude:" + longitude);

                JSONObject sys = jsonResponse.getJSONObject("sys");
                String countryCode = sys.getString("country");
                sb.append("\nCountry Code:" + countryCode);

                JSONObject main = jsonResponse.getJSONObject("main");
                String minTemp = main.getString("temp_min");
                String maxTemp = main.getString("temp_max");

                sb.append("\nMin Temp:" + minTemp);
                sb.append("\nMax Temp:" + maxTemp);

                String cityName = jsonResponse.getString("name");
                sb.append("\nCity Name:" + cityName);

                resultToDisplay = sb.toString();

            } catch (Exception e ) {

            }

        }

    }
}

MapsActivity.java

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    double latitude;
    double longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        Intent intent = getIntent();
        latitude = intent.getDoubleExtra("lat", 0);
        longitude = intent.getDoubleExtra("long", 0);

        System.out.println("******Latitude"+latitude);
        System.out.println("******Longitude:"+longitude);

        setUpMapIfNeeded();
        mMap.setMyLocationEnabled(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever
     * call {@link #setUpMap()} once when {@link #mMap} is not null.
     * <p/>
     * If it isn't installed {@link SupportMapFragment} (and
     * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
     * install/update the Google Play services APK on their device.
     * <p/>
     * A user can return to this FragmentActivity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
     * have been completely destroyed during this process (it is likely that it would only be
     * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
     * method in {@link #onResume()} to guarantee that it will be called.
     */
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p/>
     * This should only be called once and when we are sure that {@link #mMap} is not null.
     */
    private void setUpMap() {
        //mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));

        LatLng position = new LatLng(latitude, longitude);
        // create marker
        MarkerOptions marker = new MarkerOptions().position(position).title("Hello Maps ");

        // adding marker
        mMap.addMarker(marker);

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 4));
    }
}



No comments: