Libraries:
implementation 'com.google.android.gms:play-services-maps:16.0.0'implementation 'com.google.code.gson:gson:2.8.0'implementation 'com.mcxiaoke.volley:library:1.0.19'
MainActivity.java
package com.example.hemanthkarvymaps;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
ListView ListView;
private VolleyListViewAdapter adapter;
private List<CommonBean> VolleyList = new ArrayList<CommonBean>();
private ProgressDialog mprocessingdialog;
Button Track;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Track = (Button)findViewById(R.id.Track);
ListView = (ListView) findViewById(R.id.ListView);
adapter = new VolleyListViewAdapter(MainActivity.this, VolleyList);
ListView.setAdapter(adapter);
Track.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MapActivity.class);
intent.putExtra("Name", "Current Location");
intent.putExtra("Long", "78.3326902");
intent.putExtra("Late", "17.4208575");
startActivity(intent);
// String uri = String.format(Locale.ENGLISH, "geo:%f,%f", 17.4208575, 78.3326902);
// Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
// startActivity(intent);
// String strUri = "http://maps.google.com/maps?q=loc:" + 17.4208575 + "," + 78.3326902 + " (" + "Label which you want" + ")";
// Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(strUri));
//
// intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
//
// startActivity(intent);
}
});
new MainActivity.GetListAsync().execute();
}
private class GetListAsync extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mprocessingdialog = new ProgressDialog(MainActivity.this);
mprocessingdialog.setTitle("Please Wait..");
mprocessingdialog.setMessage("Loading");
mprocessingdialog.setIndeterminate(false);
mprocessingdialog.setCancelable(false);
mprocessingdialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
JSONObject params = new JSONObject();
try {
params.put("authuser", "kfcauser");
params.put("authpass", "UzJGeWRubEFNVEl6Um05eVpYZ2tOVFkzUTNWeWNtVnVZM2tqSkRjNE9VRndjQT09");
params.put("username","seenu1233@gmail.com");
params.put("password", "1233");
} catch (Exception e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.POST, "http://karvyforex.com:8004/CheckLogin", params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("rlog", response.toString());
String Res = "{\n" +
" \"data\": [\n" +
" {\n" +
" \"branchcode\": \"--------\",\n" +
" \"Long\": \"78.3405474\",\n" +
" \"Name\": \"1Wipro\",\n" +
" \"Late\": \"17.4258143\",\n" +
" \"status\": \"3\"\n" +
" },\n" +
" {\n" +
" \"branchcode\": \"--------\",\n" +
" \"Long\": \"78.3453714\",\n" +
" \"Name\": \"2Botanical \",\n" +
" \"Late\": \"17.4457105\",\n" +
" \"status\": \"3\"\n" +
" },\n" +
" {\n" +
" \"branchcode\": \"--------\",\n" +
" \"Long\": \"78.3513936\",\n" +
" \"Name\": \"3DLF\",\n" +
" \"Late\": \"17.4453795\",\n" +
" \"status\": \"3\"\n" +
" },\n" +
" \n" +
" {\n" +
" \"branchcode\": \"--------\",\n" +
" \"Long\": \"78.491684\",\n" +
" \"Name\": \"4HYD\",\n" +
" \"Late\": \"17.387140\",\n" +
" \"status\": \"7\"\n" +
" }\n" +
" ],\n" +
" \"message\": \"success\",\n" +
" \"status\": \"200\"\n" +
"}";
try {
JSONObject jsonObj = new JSONObject(Res);
JSONArray jsonArray = jsonObj.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
CommonBean commonBean = new CommonBean();
commonBean.setName(obj.optString("Name"));
commonBean.setLate(obj.optString("Late"));
commonBean.setLong(obj.optString("Long"));
VolleyList.add(commonBean);
}
mprocessingdialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
CommonBean historyBean = VolleyList.get(i);
// Toast.makeText(HistoryActivity.this.getApplicationContext(), historyBean.getName() + "\n" + historyBean.getThumbnailUrl(), Toast.LENGTH_SHORT).show();
// Intent intent = new Intent(MainActivity.this, MapActivity.class);
//
// intent.putExtra("Name", historyBean.getName());
// intent.putExtra("Long", historyBean.getLong());
// intent.putExtra("Late", historyBean.getLate());
// startActivity(intent);
}
});
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("rlog", "Error: " + error.getMessage());
}
});
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(jsonObjReq);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
mprocessingdialog.dismiss();
}
}, 10000);
}
}
}
CommonBean.java
package com.example.hemanthkarvymaps;
public class CommonBean {
private String Name;
private String Long;
private String Late;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getLong() {
return Long;
}
public void setLong(String aLong) {
Long = aLong;
}
public String getLate() {
return Late;
}
public void setLate(String late) {
Late = late;
}
}
VolleyListViewAdapter.java
package com.example.hemanthkarvymaps;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class VolleyListViewAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
List<CommonBean> VolleyList;
List<CommonBean> searchlist;
// ImageView thumbNail;
// ArrayList<HistoryBean> arraylist;
public VolleyListViewAdapter(Activity activity, List<CommonBean> VolleyList) {
this.activity = activity;
this.VolleyList = VolleyList;
// this.arraylist = new ArrayList<HistoryBean>();
// this.arraylist.addAll(HistoryBeanList);
}
public class ViewHolder {
TextView Name;
TextView AccountType;
TextView Date;
Button success;
Button Processing;
Button Fail;
}
@Override
public int getCount() {
return VolleyList.size();
}
@Override
public Object getItem(int location) {
return VolleyList.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
holder = new ViewHolder();
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.volley_listview_item, null);
// thumbNail = (ImageView) convertView.findViewById(R.id.flag);
holder.Name = (TextView) convertView.findViewById(R.id.Name);
holder.AccountType = (TextView) convertView.findViewById(R.id.AccountType);
holder.Date = (TextView) convertView.findViewById(R.id.Date);
holder.success = (Button) convertView.findViewById(R.id.success);
CommonBean commonBean = VolleyList.get(position);
holder.success.setTag(VolleyList.get(position));
holder.success.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.success.setTag(VolleyList.get(position));
CommonBean commonBean = VolleyList.get(position);
Intent intent = new Intent(activity, MapActivity.class);
intent.putExtra("Name", commonBean.getName());
intent.putExtra("Long", commonBean.getLong());
intent.putExtra("Late", commonBean.getLate());
activity.startActivity(intent);
}
});
// final ImageView imageView = (ImageView) convertView.findViewById(R.id.unchecked);
// Glide.with(activity).load(m.getThumbnailUrl()).transform(new CircleTransform(activity)).into(thumbNail);
// holder.name.setText("GBP 1000.00");
holder.Name.setText(commonBean.getName());
// holder.AccountNumber.setText(historybean.getAccout_num());
// holder.Date.setText(historybean.getTxn_date());
holder.AccountType.setText(commonBean.getLong());
holder.Date.setText(commonBean.getLate());
return convertView;
}
}
MapActivity.java
package com.example.hemanthkarvymaps;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
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.MarkerOptions;
import java.util.ArrayList;
import static java.lang.Float.parseFloat;
public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
String Name;
String Long;
String Late;
ImageView current;
ArrayList<LatLng> locations = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
// 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);
current = (ImageView) findViewById(R.id.current);
current.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LatLng sydney = new LatLng(17.4208575, 78.3326902);
mMap.addMarker(new MarkerOptions().position(sydney).title("Current Loaction"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f));
}
});
locations.add(new LatLng(17.4258143, 78.3405474));
locations.add(new LatLng(17.4457105, 78.3453714));
locations.add(new LatLng(17.4453795, 78.3513936));
locations.add(new LatLng(17.387140, 78.491684));
Bundle b = getIntent().getExtras();
if (b != null) {
Name = b.getString("Name");
Long = b.getString("Long");
Late = b.getString("Late");
Toast.makeText(MapActivity.this.getApplicationContext(), Name + "\n" + Long + "\n" + Late, Toast.LENGTH_SHORT).show();
}
}
/**
* 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(17.4208575, 78.3326902);
if (Name.equalsIgnoreCase("Current Location")) {
for (LatLng location : locations) {
mMap.addMarker(new MarkerOptions()
.position(location)
.title(""));
}
LatLng sydney = new LatLng(17.387140, 78.491684);
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10.0f));
} else {
LatLng sydney = new LatLng(parseFloat(Late), parseFloat(Long));
mMap.addMarker(new MarkerOptions().position(sydney).title("Current Loaction"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f));
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/ListView"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_margin="5dp"
android:scrollbars="none" />
<Button
android:id="@+id/Track"
android:layout_width="match_parent"
android:text="Track"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />
</LinearLayout>
volley_listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/flag"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_margin="5dp"
app:srcCompat="@mipmap/ic_launcher" />
<!--<de.hdodenhof.circleimageview.CircleImageView-->
<!--android:id="@+id/flag"-->
<!--android:layout_width="80dp"-->
<!--android:layout_height="60dp"-->
<!--android:layout_gravity="center"-->
<!--android:layout_margin="5dp"-->
<!--android:src="@drawable/user"-->
<!--app:civ_border_color="@color/skyblue" />-->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:id="@+id/L1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="@+id/AccountType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp" />
<TextView
android:id="@+id/Date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/L2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:orientation="horizontal">
<Button
android:id="@+id/success"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:padding="0dp"
android:text="Navigate"
android:textAllCaps="false"
android:textSize="20sp"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
</TableRow>
</TableRow>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp" />
</LinearLayout>
activity_map.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapActivity" />
<ImageView
android:id="@+id/current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/ic_dialog_map" />
</RelativeLayout>