Kamis, 01 November 2018

Map JSON AND Marker


package com.example.komunitas;


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

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
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.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
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 android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Maps extends FragmentActivity{
   private Marker previousMarker = null;
   int jd=0;
   private SupportMapFragment mapFragment;
   String tkode="",tnama="",tmail="",thp="",tlati="",tlongi="",talamat="",tangkatan="",tstatus="",tgambar="";
   String skodelogin="",snamalogin="",smaillogin="";
   String myLati="-6.352953";
   String myLongi="106.831389";
   String myPosisi="UI";
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
JSONArray myJSON = null;
String ip="";
ArrayList<HashMap<String, String>> arrayList;
private static final String TAG_SUKSES = "sukses";
private static final String TAG_record = "record";
   String[]ark;
String[]arkode_anggota;
String[]artgl;
String[]aralamat;
String[]arlatitude;
String[]arlongitude;
String[]arjam;



   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.maps);
       Intent i= this.getIntent();
      myLati= i.getStringExtra("myLati");
      myLongi= i.getStringExtra("myLongi");
      myPosisi= i.getStringExtra("myPosisi");

      skodelogin= i.getStringExtra("skodelogin");
      snamalogin= i.getStringExtra("snamalogin");
      smaillogin= i.getStringExtra("smaillogin");
        
        arrayList = new ArrayList<HashMap<String, String>>();
      ip=jParser.getIP();
         
      new load().execute();
      
      
   }
   class load extends AsyncTask<String, String, String> {
      @Override      protected void onPreExecute() {
         super.onPreExecute();
         pDialog = new ProgressDialog(Maps.this);
         pDialog.setMessage("Load data. Silahkan Tunggu...");
         pDialog.setIndeterminate(false);
         pDialog.setCancelable(false);
         pDialog.show();
      }
      protected String doInBackground(String... args) {
         List<NameValuePair> params = new ArrayList<NameValuePair>();
         JSONObject json = jParser.makeHttpRequest(ip+"lokasi/lokasi_show.php", "GET", params);
         Log.d("show: ", json.toString());
         try {
            int sukses = json.getInt(TAG_SUKSES);
            if (sukses == 1) {
               myJSON = json.getJSONArray(TAG_record);
               jd=myJSON.length();

arkode_anggota=new String[jd];
artgl=new String[jd];
aralamat=new String[jd];
arlatitude=new String[jd];
arlongitude=new String[jd];
arjam=new String[jd];
               ark=new String[jd];

               for (int i = 0; i < jd; i++) {
                  JSONObject c = myJSON.getJSONObject(i);
                  ark[i]=c.getString("kode_anggota");
                  arkode_anggota[i]= c.getString("nama_anggota")+" /"+ c.getString("angkatan");
                  artgl[i]= c.getString("tgl");
                  aralamat[i]= c.getString("alamat");
                  arlatitude[i]= c.getString("latitude");
                  arlongitude[i]= c.getString("longitude");
                  arjam[i]= c.getString("jam");
                  
                  
               }
            } 
         } 
         catch (JSONException e) {e.printStackTrace();}
         return null;
      }

      protected void onPostExecute(String file_url) {
         pDialog.dismiss();
         runOnUiThread(new Runnable() {
            public void run() {
                setupMap();
            }
         });}
   }
   private void setupMap(){
         mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.maps));

            mapFragment.getMapAsync(new OnMapReadyCallback() {
               @Override               public void onMapReady(GoogleMap map) {
                  double lat=-6.352953;
                  double lon=106.831389;

                  for(int i=0;i<jd;i++){
                     if(skodelogin.equalsIgnoreCase(ark[i])){}
                     else {
                        lat = -6.352953;
                        lon = 106.831389;
                        try {
                           lat = Double.parseDouble(arlatitude[i]);
                           lon = Double.parseDouble(arlongitude[i]);
                        } catch (Exception e) {
                           lat = -6.352953;
                           lon = 106.831389;
                        }
                        map.addMarker(new MarkerOptions().position(new LatLng(lat, lon)).title(arkode_anggota[i]).snippet(artgl[i] + " " + arjam[i] + " wib " + aralamat[i]));
                     }
                  }

                  try{
                     lat=Double.parseDouble(myLati);
                     lon=Double.parseDouble(myLongi);
                  }
                  catch(Exception e){
                     lat=-6.352953;
                     lon=106.831389;
                  }
                  //BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.admin);                  BitmapDescriptor icon =BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE);
                  map.addMarker(new MarkerOptions().position(new LatLng(lat, lon)).title("Posisi Anda").snippet(myPosisi).icon(icon));
                  map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat,lon),10));


//==============
//                map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {//                   @Override//                   public boolean onMarkerClick(Marker marker) {//                      String locAddress = marker.getTitle();//                      //fillTextViews(locAddress);//                      if (previousMarker != null) {//                         previousMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));//                      }//                      marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));//                      previousMarker = marker;//////                      if(locAddress.equalsIgnoreCase("Posisi Anda")){}//                      else{//                         Intent i = new Intent(getApplicationContext(), Detail_anggota.class);//                         i.putExtra("pk", locAddress);//                         i.putExtra("myLati", myLati);//                         i.putExtra("myLongi", myLongi);//                         i.putExtra("myPosisi", myPosisi);//                         i.putExtra("skodelogin", skodelogin);//                         i.putExtra("snamalogin", snamalogin);//                         i.putExtra("smaillogin", smaillogin);//                         startActivityForResult(i, 100);////                         LayoutInflater inflater = getLayoutInflater();//                         View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.custom_toast_layout_id));//                         ImageView image = (ImageView) layout.findViewById(R.id.image);//                         image.setImageResource(R.drawable.logochat);//                         TextView text = (TextView) layout.findViewById(R.id.text);//                         text.setText("Hai "+locAddress);////                         Toast toast = new Toast(getApplicationContext());//                         toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);//                         toast.setDuration(Toast.LENGTH_LONG);//                         toast.setView(layout);//                         toast.show();////                      }//                      return true;//                   }//                });//==============               }
            });

         }

}



maps.xml
<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"    >

    <fragment        android:id="@+id/maps"        android:layout_width="match_parent"        android:layout_height="match_parent"        class="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>



PERMISSION:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.VIBRATE"/>

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />





Tidak ada komentar:

Posting Komentar