Wednesday, August 3, 2016

Google Map Android



MapActivity.java

package com.veer.myapplication;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.veer.myapplication.Entity.EntityGoogleMap;
import com.veer.myapplication.Thread.ThreadGoogleMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    /**     * ATTENTION: This was auto-generated to implement the App Indexing API.     * See https://g.co/AppIndexing/AndroidStudio for more information.     */    private GoogleApiClient client;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        // ATTENTION: This was auto-generated to implement the App Indexing API.        // See https://g.co/AppIndexing/AndroidStudio for more information.        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }


    /**     * Manipulates the map once available.     * This callback is triggered when the map is ready to be used.     * This is where we can add markers or lines, add listeners or move the camera. In this case,     * we just add a marker near Sydney, Australia.     * If Google Play services is not installed on the device, the user will be prompted to install     * it inside the SupportMapFragment. This method will only be triggered once the user has     * installed Google Play services and returned to the app.     */    @Override    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

        mMap.addMarker(new MarkerOptions().position(new LatLng(67, 34)).title("You are here."));
        mMap.addMarker(new MarkerOptions().position(new LatLng(56, 77)).title("You are here."));


    }

    @Override    protected void onResume() {
        super.onResume();
        new ThreadGoogleMap(new MyGoogleMapLocationHandler()).execute();
    }

    @Override    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.        // See https://g.co/AppIndexing/AndroidStudio for more information.        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.                "Maps Page", // TODO: Define a title for the content shown.                // TODO: If you have web page content that matches this app activity's content,                // make sure this auto-generated web page URL is correct.                // Otherwise, set the URL to null.                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.                Uri.parse("android-app://com.veer.myapplication/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.        // See https://g.co/AppIndexing/AndroidStudio for more information.        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.                "Maps Page", // TODO: Define a title for the content shown.                // TODO: If you have web page content that matches this app activity's content,                // make sure this auto-generated web page URL is correct.                // Otherwise, set the URL to null.                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.                Uri.parse("android-app://com.veer.myapplication/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }

    private class MyGoogleMapLocationHandler extends Handler {
        @Override        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mMap.clear();
            ArrayList<EntityGoogleMap> entityGooglemaps = (ArrayList<EntityGoogleMap>) msg.obj;
            for (int i = 0; i < entityGooglemaps.size(); i++) {

               EntityGoogleMap entityGooglemap = entityGooglemaps.get(i);
                LatLng latLng = new LatLng(entityGooglemap.getLat(),entityGooglemap.getSlng());

                //Log.d("Latlog", String.valueOf(entityGooglemap.getSpeed()));                drawMarker(latLng, entityGooglemap.getPhone(),entityGooglemap.getShip_add());
                //mMap.addMarker(new MarkerOptions().position(latLng).title(entityGooglemap.getgpsTime()));
               // mMap.addMarker(new MarkerOptions().position(new LatLng(entityGooglemaps.get(i).getLatitude(),entityGooglemaps.get(i).getLongitude())).title(entityGooglemaps.get(i).getgpsTime()));            }
        }
    }

    private void drawMarker(LatLng point,String name,String username){
        // Creating an instance of MarkerOptions        MarkerOptions markerOptions = new MarkerOptions();

        // Setting latitude and longitude for the marker        markerOptions.position(point);
markerOptions.title(name);
        String id = mMap.addMarker(markerOptions).getId();
        // Adding marker on the Google Map
        mMap.addMarker(markerOptions);
        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override            public void onInfoWindowClick(Marker marker) {
                Toast.makeText(getApplicationContext(),marker.getId(),Toast.LENGTH_SHORT).show();
            }
        });
    }

}





ThreadGoogleMap.java


package com.veer.myapplication.Thread;

/** * Created by aabit on 7/28/16. */
import android.os.AsyncTask;
import android.os.Message;

import com.veer.myapplication.Entity.*;
import com.veer.myapplication.WebDataTransfer.GMap;

import java.util.ArrayList;

public class ThreadGoogleMap extends AsyncTask<Object,Object,ArrayList<EntityGoogleMap>> {

    android.os.Handler handler;

    public ThreadGoogleMap(android.os.Handler handler) {
        this.handler = handler;
    }

    @Override    protected ArrayList<EntityGoogleMap> doInBackground(Object... params) {


        return GMap.locationfind();
    }

    @Override    protected void onPostExecute(ArrayList<EntityGoogleMap> entityGooglemap) {
        super.onPostExecute(entityGooglemap);
        Message msg = new Message();
        msg.obj = entityGooglemap;
        handler.sendMessage(msg);
    }
}




GMap.java

package com.veer.myapplication.WebDataTransfer;

import android.util.Log;

import com.veer.myapplication.Entity.EntityGoogleMap;
import com.veer.myapplication.Utils.Session;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

/** * Created by aabit on 7/28/16. */public class GMap {

        public static ArrayList<EntityGoogleMap> locationfind(){
            URL url = null;
            HttpURLConnection urlConnection = null;

            ArrayList<EntityGoogleMap> entityGooglemapArrayList = new ArrayList<>();

            String LOCATION_URL = "http://yoursite.com/API/dispatch.php?dsa="+ Session.userts;
                Log.d("kmDistance =>", "locationfind => " + LOCATION_URL);
                try {
                url = new URL(LOCATION_URL);

                urlConnection = (HttpURLConnection) url.openConnection();
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());


                JSONObject jsonObject = new JSONObject(readStream(in));
                JSONArray jsonArray = jsonObject.getJSONArray("dispach");

                for (int i=0 ; i<jsonArray.length(); i++){

                    JSONObject jsonLocation = jsonArray.getJSONObject(i);

                    EntityGoogleMap entityGooglemap = new EntityGoogleMap(jsonLocation.getString("sale_code"),
                            jsonLocation.getString("sale_date"),
                            jsonLocation.getString("grand_total"),
                            jsonLocation.getString("phone"),
                            jsonLocation.getString("ship_add"),
                            jsonLocation.getDouble("lat"),
                            jsonLocation.getDouble("slng"));
                    entityGooglemapArrayList.add(entityGooglemap);

                }
                return entityGooglemapArrayList;


            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return entityGooglemapArrayList;
        }
    public static String readStream(InputStream is){
        BufferedReader reader = null;
        String webPage = "",data="";

        try {
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

            while ((data = reader.readLine()) != null){
                webPage += data + "\n";
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Log.e(webPage,"   Dharambir singh");        return webPage;
    }
}


Gradle

compile 'org.apache.httpcomponents:httpclient:4.3.5'compile 'com.google.android.gms:play-services-appindexing:8.1.0'compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'

No comments:

Post a Comment

Dharamart.blogspot.in