Refactored the entire library
I also moved the directions listfragment, overlay, and other required classes from FMC into the library Change-Id: Iba27e29d89e864dbeca3a2670aed552a8be4f2b8 Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
Binary file not shown.
22
LocationLib/res/layout/list_row.xml
Executable file
22
LocationLib/res/layout/list_row.xml
Executable file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
<TextView
|
||||
android:id="@+id/TextView01"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="20dip"
|
||||
android:textColor="#000000"/>
|
||||
<TextView
|
||||
android:id="@+id/TextView02"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="20dip"
|
||||
android:textColor="#000000"
|
||||
android:layout_gravity="right"
|
||||
android:paddingRight="10dip"
|
||||
/>
|
||||
</LinearLayout>
|
||||
7
LocationLib/res/layout/listview.xml
Normal file
7
LocationLib/res/layout/listview.xml
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@android:id/list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" >
|
||||
|
||||
</ListView>
|
||||
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* @author Twenty Codes, LLC
|
||||
* @author ricky barrette
|
||||
* @date Sep 22, 2010
|
||||
*/
|
||||
package com.TwentyCodes.android.fragments;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.Html;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.TwentyCodes.android.location.R;
|
||||
import com.TwentyCodes.android.overlays.DirectionsOverlay;
|
||||
|
||||
/**
|
||||
* this is a custom listview adaptor that wills a listview that has 2 textviews in each row.
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public class DirectionsAdapter extends BaseAdapter {
|
||||
|
||||
private LayoutInflater mInflater;
|
||||
private DirectionsOverlay mDirections;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new DirectionsAdapter
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public DirectionsAdapter(Context context, DirectionsOverlay directions) {
|
||||
mInflater = LayoutInflater.from(context);
|
||||
mDirections = directions;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the size of the main list
|
||||
* @see android.widget.Adapter#getCount()
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@Override
|
||||
public int getCount() {
|
||||
return mDirections.getDirections().size() + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* (non-Javadoc)
|
||||
* @see android.widget.Adapter#getItem(int)
|
||||
* @param position
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@Override
|
||||
public Object getItem(int position) {
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the current position in the list
|
||||
* @see android.widget.Adapter#getItemId(int)
|
||||
* @param position
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* inflates the row from xml, and sets the textviews to their intended vales
|
||||
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
|
||||
* @param position
|
||||
* @param convertView
|
||||
* @param parent
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
ViewHolder holder;
|
||||
if (convertView == null) {
|
||||
convertView = mInflater.inflate(R.layout.list_row, null);
|
||||
holder = new ViewHolder();
|
||||
holder.text = (TextView) convertView.findViewById(R.id.TextView01);
|
||||
holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);
|
||||
|
||||
convertView.setTag(holder);
|
||||
} else {
|
||||
holder = (ViewHolder) convertView.getTag();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the copyrights on the bottom of the directions list
|
||||
*/
|
||||
if (position == mDirections.getDirections().size()){
|
||||
holder.text.setText(mDirections.getCopyrights());
|
||||
holder.text2.setText("");
|
||||
} else {
|
||||
holder.text.setText(Html.fromHtml(mDirections.getDirections().get(position)));
|
||||
holder.text2.setText(mDirections.getDurations().get(position) +" : "+ mDirections.getDistances().get(position));
|
||||
}
|
||||
return convertView;
|
||||
}
|
||||
|
||||
/**
|
||||
* this class will hold the TextViews
|
||||
* @author ricky barrette
|
||||
*/
|
||||
class ViewHolder {
|
||||
TextView text;
|
||||
TextView text2;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* DirectionsListFragment.java
|
||||
* @date Nov 25, 2011
|
||||
* @author ricky barrette
|
||||
* @author Twenty Codes, LLC
|
||||
*/
|
||||
package com.TwentyCodes.android.fragments;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.support.v4.app.ListFragment;
|
||||
import android.view.View;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ListView;
|
||||
|
||||
import com.TwentyCodes.android.location.OnDirectionSelectedListener;
|
||||
import com.TwentyCodes.android.overlays.DirectionsOverlay;
|
||||
import com.google.android.maps.GeoPoint;
|
||||
|
||||
/**
|
||||
* This fragment will be used to display directions to the user.
|
||||
* When a specific direction is clicked, the corrispoding geopoint is returned via listener
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public class DirectionsListFragment extends ListFragment {
|
||||
|
||||
private OnDirectionSelectedListener mListener;
|
||||
private ArrayList<GeoPoint> mPoints;
|
||||
|
||||
/**
|
||||
* Creates a new Directions List Fragment
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public DirectionsListFragment() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Directions List Fragment
|
||||
* @param listener
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public DirectionsListFragment(OnDirectionSelectedListener listener) {
|
||||
this();
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all content in the listview
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void clear() {
|
||||
this.setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, new ArrayList<String>()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a list item is clicked.
|
||||
* Checks to see if the list item is a direction, if to it reports the selected direction's geopoint to the listener
|
||||
* (non-Javadoc)
|
||||
* @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
|
||||
*/
|
||||
@Override
|
||||
public void onListItemClick(ListView l, View w, int position, long id) {
|
||||
if(position < mPoints.size())
|
||||
if(mListener != null)
|
||||
mListener.onDirectionSelected(mPoints.get(position));
|
||||
}
|
||||
|
||||
/**
|
||||
* (non-Javadoc)
|
||||
* @see android.support.v4.app.Fragment#onStart()
|
||||
*/
|
||||
@Override
|
||||
public void onStart() {
|
||||
this.setListShown(true);
|
||||
super.onStart();
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the directions from the provided DirectionsOverlay object
|
||||
* @param directions
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void setDirections(final DirectionsOverlay directions) {
|
||||
mPoints = directions.getPoints();
|
||||
this.setListAdapter(new DirectionsAdapter(getActivity(), directions));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the text to be displayed while the list is empty
|
||||
* @param text
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void SetEmptyText(String text){
|
||||
this.setEmptyText(text);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
* @author ricky barrette
|
||||
* @author Twenty Codes, LLC
|
||||
*/
|
||||
package com.TwentyCodes.android.location;
|
||||
package com.TwentyCodes.android.fragments;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
@@ -12,6 +12,10 @@ import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.TwentyCodes.android.location.MapView;
|
||||
import com.TwentyCodes.android.location.R;
|
||||
import com.TwentyCodes.android.location.R.id;
|
||||
import com.TwentyCodes.android.location.R.layout;
|
||||
import com.google.android.maps.GeoPoint;
|
||||
import com.google.android.maps.Overlay;
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
* @author ricky barrette
|
||||
* @author Twenty Codes, LLC
|
||||
*/
|
||||
package com.TwentyCodes.android.SkyHook;
|
||||
package com.TwentyCodes.android.fragments;
|
||||
|
||||
import com.TwentyCodes.android.location.CompassListener;
|
||||
import com.TwentyCodes.android.location.GeoPointLocationListener;
|
||||
import com.TwentyCodes.android.location.MapFragmentBase;
|
||||
import com.TwentyCodes.android.location.MapView;
|
||||
import com.TwentyCodes.android.overlays.SkyHookUserOverlay;
|
||||
import com.google.android.maps.GeoPoint;
|
||||
|
||||
/**
|
||||
@@ -88,7 +88,7 @@ public class SkyHoookUserOverlayMapFragment extends MapFragmentBase implements G
|
||||
|
||||
/**
|
||||
* (non-Javadoc)
|
||||
* @see com.TwentyCodes.android.location.MapFragmentBase#onMapViewCreate(com.TwentyCodes.android.location.MapView)
|
||||
* @see com.TwentyCodes.android.fragments.MapFragmentBase#onMapViewCreate(com.TwentyCodes.android.location.MapView)
|
||||
*/
|
||||
@Override
|
||||
public void onMapViewCreate(MapView map) {
|
||||
@@ -104,7 +104,7 @@ public class SkyHoookUserOverlayMapFragment extends MapFragmentBase implements G
|
||||
|
||||
/**
|
||||
* (non-Javadoc)
|
||||
* @see com.TwentyCodes.android.location.MapFragmentBase#onPause()
|
||||
* @see com.TwentyCodes.android.fragments.MapFragmentBase#onPause()
|
||||
*/
|
||||
@Override
|
||||
public void onPause() {
|
||||
@@ -115,7 +115,7 @@ public class SkyHoookUserOverlayMapFragment extends MapFragmentBase implements G
|
||||
|
||||
/**
|
||||
* (non-Javadoc)
|
||||
* @see com.TwentyCodes.android.location.MapFragmentBase#onResume()
|
||||
* @see com.TwentyCodes.android.fragments.MapFragmentBase#onResume()
|
||||
*/
|
||||
@Override
|
||||
public void onResume() {
|
||||
@@ -4,8 +4,12 @@
|
||||
* @author ricky barrette
|
||||
* @author Twenty Codes, LLC
|
||||
*/
|
||||
package com.TwentyCodes.android.location;
|
||||
package com.TwentyCodes.android.fragments;
|
||||
|
||||
import com.TwentyCodes.android.location.CompassListener;
|
||||
import com.TwentyCodes.android.location.GeoPointLocationListener;
|
||||
import com.TwentyCodes.android.location.MapView;
|
||||
import com.TwentyCodes.android.overlays.UserOverlay;
|
||||
import com.google.android.maps.GeoPoint;
|
||||
|
||||
/**
|
||||
@@ -84,7 +88,7 @@ public class UserOverlayMapFragment extends MapFragmentBase implements GeoPointL
|
||||
|
||||
/**
|
||||
* (non-Javadoc)
|
||||
* @see com.TwentyCodes.android.location.MapFragmentBase#onMapViewCreate(com.TwentyCodes.android.location.MapView)
|
||||
* @see com.TwentyCodes.android.fragments.MapFragmentBase#onMapViewCreate(com.TwentyCodes.android.location.MapView)
|
||||
*/
|
||||
@Override
|
||||
public void onMapViewCreate(MapView map) {
|
||||
@@ -100,7 +104,7 @@ public class UserOverlayMapFragment extends MapFragmentBase implements GeoPointL
|
||||
|
||||
/**
|
||||
* (non-Javadoc)
|
||||
* @see com.TwentyCodes.android.location.MapFragmentBase#onPause()
|
||||
* @see com.TwentyCodes.android.fragments.MapFragmentBase#onPause()
|
||||
*/
|
||||
@Override
|
||||
public void onPause() {
|
||||
@@ -111,7 +115,7 @@ public class UserOverlayMapFragment extends MapFragmentBase implements GeoPointL
|
||||
|
||||
/**
|
||||
* (non-Javadoc)
|
||||
* @see com.TwentyCodes.android.location.MapFragmentBase#onResume()
|
||||
* @see com.TwentyCodes.android.fragments.MapFragmentBase#onResume()
|
||||
*/
|
||||
@Override
|
||||
public void onResume() {
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* OnDirectionSelectedListener.java
|
||||
* @date Mar 5, 2012
|
||||
* @author ricky barrette
|
||||
* @author Twenty Codes, LLC
|
||||
*/
|
||||
package com.TwentyCodes.android.location;
|
||||
|
||||
import com.google.android.maps.GeoPoint;
|
||||
|
||||
/**
|
||||
* A simple interfrace for a directions list fragment
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public interface OnDirectionSelectedListener {
|
||||
|
||||
/**
|
||||
* Called when the user selects a direction from a directions list
|
||||
* @param point
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void onDirectionSelected(GeoPoint point);
|
||||
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import com.google.android.maps.GeoPoint;
|
||||
* This interface will be used to pass the selected location from the dialogs to the listening instance
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public interface LocationSelectedListener {
|
||||
public interface OnLocationSelectedListener {
|
||||
|
||||
public void onLocationSelected(GeoPoint point);
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
* @author ricky barrette
|
||||
* @author Twenty Codes, LLC
|
||||
*/
|
||||
package com.TwentyCodes.android.location;
|
||||
package com.TwentyCodes.android.overlays;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
@@ -15,7 +15,11 @@ import android.graphics.Matrix;
|
||||
import android.graphics.Point;
|
||||
import android.util.TypedValue;
|
||||
|
||||
import com.TwentyCodes.android.location.CompassListener;
|
||||
import com.TwentyCodes.android.location.CompassSensor;
|
||||
import com.TwentyCodes.android.location.GeoUtils;
|
||||
import com.TwentyCodes.android.location.R;
|
||||
import com.TwentyCodes.android.location.R.drawable;
|
||||
import com.google.android.maps.GeoPoint;
|
||||
import com.google.android.maps.MapView;
|
||||
import com.google.android.maps.Overlay;
|
||||
@@ -0,0 +1,352 @@
|
||||
/**
|
||||
* DirectionsOverlay.java
|
||||
* @date Nov 10, 2011
|
||||
* @author ricky barrette
|
||||
* @author Twenty Codes, LLC
|
||||
*/
|
||||
package com.TwentyCodes.android.overlays;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.util.Log;
|
||||
|
||||
import com.TwentyCodes.android.debug.Debug;
|
||||
import com.TwentyCodes.android.location.MapView;
|
||||
import com.google.android.maps.GeoPoint;
|
||||
|
||||
/**
|
||||
* This Overlay class will be used to display provided by the Google Directions API on a map
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public class DirectionsOverlay {
|
||||
|
||||
private static final String TAG = "DirectionsOverlay";
|
||||
ArrayList<PathOverlay> mPath;
|
||||
ArrayList<String> mDirections;
|
||||
private MapView mMapView;
|
||||
private OnDirectionsCompleteListener mListener;
|
||||
private String mCopyRights;
|
||||
private ArrayList<GeoPoint> mPoints;
|
||||
private ArrayList<String> mDistance;
|
||||
private ArrayList<String> mDuration;
|
||||
private ArrayList<String> mWarnings;
|
||||
|
||||
/**
|
||||
* Downloads and Creates a new DirectionsOverlay from the provided points
|
||||
* @param origin point
|
||||
* @param destination point
|
||||
* @author ricky barrette
|
||||
* @throws IOException
|
||||
* @throws ClientProtocolException
|
||||
* @throws IllegalStateException
|
||||
* @throws JSONException
|
||||
*/
|
||||
public DirectionsOverlay(MapView map, GeoPoint origin, GeoPoint destination, OnDirectionsCompleteListener listener) throws IllegalStateException, ClientProtocolException, IOException, JSONException {
|
||||
mMapView = map;
|
||||
mListener = listener;
|
||||
String json = downloadJSON(generateUrl(origin, destination));
|
||||
drawPath(json);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new DirectionsOverlay from the provided String JSON
|
||||
* @param json
|
||||
* @throws JSONException
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public DirectionsOverlay(MapView map, String json, OnDirectionsCompleteListener listener) throws JSONException{
|
||||
mListener = listener;
|
||||
mMapView = map;
|
||||
drawPath(json);
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads Google Directions JSON from the Internet
|
||||
* @param url
|
||||
* @return
|
||||
* @throws IllegalStateException
|
||||
* @throws ClientProtocolException
|
||||
* @throws IOException
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private String downloadJSON(String url) throws IllegalStateException, ClientProtocolException, IOException {
|
||||
if(Debug.DEBUG)
|
||||
Log.d(TAG, url);
|
||||
if(url == null)
|
||||
throw new NullPointerException();
|
||||
StringBuffer response = new StringBuffer();
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(new DefaultHttpClient().execute(new HttpGet(url)).getEntity().getContent()));
|
||||
String buff = null;
|
||||
while ((buff = br.readLine()) != null)
|
||||
response.append(buff);
|
||||
return response.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new DirectionsOverlay from the json provided
|
||||
* @param json of Google Directions API
|
||||
* @author ricky barrette
|
||||
* @return
|
||||
* @throws JSONException
|
||||
*/
|
||||
public void drawPath(String json) throws JSONException{
|
||||
if(Debug.DEBUG){
|
||||
Log.d(TAG, "drawPath");
|
||||
Log.d(TAG, json);
|
||||
}
|
||||
mPath = new ArrayList<PathOverlay>();
|
||||
mDirections = new ArrayList<String>();
|
||||
mPoints = new ArrayList<GeoPoint>();
|
||||
mDistance = new ArrayList<String>();
|
||||
mDuration = new ArrayList<String>();
|
||||
|
||||
//get first route
|
||||
JSONObject route = new JSONObject(json).getJSONArray("routes").getJSONObject(0);
|
||||
|
||||
mCopyRights = route.getString("copyrights");
|
||||
//route.getString("status");
|
||||
|
||||
JSONObject leg = route.getJSONArray("legs").getJSONObject(0);
|
||||
getDistance(leg);
|
||||
getDuration(leg);
|
||||
// mMapView.getOverlays().add(new PathOverlay(getGeoPoint(leg.getJSONObject("start_location")), 12, Color.GREEN));
|
||||
// mMapView.getOverlays().add(new PathOverlay(getGeoPoint(leg.getJSONObject("end_location")), 12, Color.RED));
|
||||
|
||||
leg.getString("start_address");
|
||||
leg.getString("end_address");
|
||||
|
||||
// JSONArray warnings = leg.getJSONArray("warnings");
|
||||
// for(int i = 0; i < warnings.length(); i++){
|
||||
// mWarnings.add(warnings.get)w
|
||||
// }w
|
||||
|
||||
/*
|
||||
* here we will parse the steps of the directions
|
||||
*/
|
||||
if(Debug.DEBUG)
|
||||
Log.d(TAG, "processing steps");
|
||||
JSONArray steps = leg.getJSONArray("steps");
|
||||
JSONObject step = null;
|
||||
for(int i = 0; i < steps.length(); i++){
|
||||
if(Debug.DEBUG)
|
||||
Log.d(TAG, "step "+i);
|
||||
|
||||
step = steps.getJSONObject(i);
|
||||
|
||||
if(Debug.DEBUG){
|
||||
Log.d(TAG, "start "+getGeoPoint(step.getJSONObject("start_location")).toString());
|
||||
Log.d(TAG, "end "+getGeoPoint(step.getJSONObject("end_location")).toString());
|
||||
}
|
||||
|
||||
// if(Debug.DEBUG)
|
||||
// mMapView.getOverlays().add(new PathOverlay(getGeoPoint(step.getJSONObject("start_location")), getGeoPoint(step.getJSONObject("end_location")), Color.MAGENTA));
|
||||
|
||||
decodePoly(step);
|
||||
|
||||
mDuration.add(getDuration(step));
|
||||
|
||||
mDistance.add(getDistance(step));
|
||||
|
||||
mDirections.add(step.getString("html_instructions"));
|
||||
// Log.d("TEST", step.getString("html_instructions"));
|
||||
mPoints.add(getGeoPoint(step.getJSONObject("start_location")));
|
||||
|
||||
}
|
||||
if(Debug.DEBUG)
|
||||
Log.d(TAG, "finished parsing");
|
||||
|
||||
if(mMapView != null){
|
||||
mMapView.getOverlays().addAll(mPath);
|
||||
mMapView.postInvalidate();
|
||||
}
|
||||
|
||||
if(mListener != null)
|
||||
mListener.onDirectionsComplete(DirectionsOverlay.this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param origin
|
||||
* @param destination
|
||||
* @return The Google API url for our directions
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private String generateUrl(GeoPoint origin, GeoPoint destination){
|
||||
return "http://maps.googleapis.com/maps/api/directions/json?&origin="+
|
||||
Double.toString(origin.getLatitudeE6() / 1.0E6)+
|
||||
","+
|
||||
Double.toString(origin.getLongitudeE6() / 1.0E6)+
|
||||
"&destination="+
|
||||
Double.toString(destination.getLatitudeE6() / 1.0E6)+
|
||||
","+
|
||||
Double.toString(destination.getLongitudeE6() / 1.0E6)+
|
||||
"&sensor=true&mode=walking";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deocodes googles polyline
|
||||
* @param encoded
|
||||
* @return a list of geopoints representing the path
|
||||
* @author Mark McClure http://facstaff.unca.edu/mcmcclur/googlemaps/encodepolyline/
|
||||
* @author ricky barrette
|
||||
* @throws JSONException
|
||||
*/
|
||||
private void decodePoly(JSONObject step) throws JSONException {
|
||||
if(Debug.DEBUG)
|
||||
Log.d(TAG, "decodePoly");
|
||||
|
||||
String encoded = step.getJSONObject("polyline").getString("points");
|
||||
int index = 0, len = encoded.length();
|
||||
int lat = 0, lng = 0;
|
||||
|
||||
GeoPoint last = null;
|
||||
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;
|
||||
|
||||
GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6), (int) (((double) lng / 1E5) * 1E6));
|
||||
|
||||
if(Debug.DEBUG){
|
||||
Log.d(TAG, "current = "+ p.toString());
|
||||
if(last != null)
|
||||
Log.d(TAG, "last = "+ last.toString());
|
||||
}
|
||||
|
||||
|
||||
if(last != null)
|
||||
mPath.add(new PathOverlay(last, p, Color.RED));
|
||||
|
||||
last = p;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a JSON location object into a GeoPoint
|
||||
* @param point
|
||||
* @return Geopoint parsed from the provided JSON Object
|
||||
* @throws JSONException
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private GeoPoint getGeoPoint(JSONObject point) throws JSONException{
|
||||
return new GeoPoint((int) (point.getDouble("lat")*1E6), (int) (point.getDouble("lng")*1E6));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param step
|
||||
* @return the duration of a step
|
||||
* @throws JSONException
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private String getDuration(JSONObject step) throws JSONException{
|
||||
return step.getJSONObject("duration").getString("text");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param step
|
||||
* @return the distance of a step
|
||||
* @throws JSONException
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private String getDistance(JSONObject step) throws JSONException{
|
||||
return step.getJSONObject("distance").getString("text");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the array of PathOverlays
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public ArrayList<PathOverlay> getPath(){
|
||||
return mPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the directions overlay from the map view
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void removePath() {
|
||||
if(mMapView.getOverlays().removeAll(mPath));
|
||||
}
|
||||
|
||||
/**
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public interface OnDirectionsCompleteListener{
|
||||
public void onDirectionsComplete(DirectionsOverlay directionsOverlay);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public ArrayList<String> getDirections() {
|
||||
return mDirections;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public ArrayList<GeoPoint> getPoints() {
|
||||
return mPoints;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public ArrayList<String> getDurations(){
|
||||
return mDuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public ArrayList<String> getDistances(){
|
||||
return mDistance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public String getCopyrights(){
|
||||
return mCopyRights;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public ArrayList<String> getWarnings() {
|
||||
return mWarnings;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* PathOverlay.java
|
||||
* @date Nov 11, 2011
|
||||
* @author ricky barrette
|
||||
* @author Twenty Codes, LLC
|
||||
*/
|
||||
package com.TwentyCodes.android.overlays;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.RectF;
|
||||
|
||||
import com.google.android.maps.GeoPoint;
|
||||
import com.google.android.maps.MapView;
|
||||
import com.google.android.maps.Overlay;
|
||||
import com.google.android.maps.Projection;
|
||||
|
||||
/**
|
||||
* This overlay class is used to draw a path and points on a map
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public class PathOverlay extends Overlay {
|
||||
|
||||
private static final int PATH = 0;
|
||||
private static final int POINT = 1;
|
||||
private GeoPoint mStart;
|
||||
private GeoPoint mEnd;
|
||||
private int mColor;
|
||||
private int mMode;
|
||||
private int mRadius;
|
||||
|
||||
/**
|
||||
* Creates a new PathOverlay in path mode
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public PathOverlay(GeoPoint start, GeoPoint end, int color) {
|
||||
mStart = start;
|
||||
mEnd = end;
|
||||
mColor = color;
|
||||
mMode = PATH;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new PathOverlay in point mode. This is used to draw end points.
|
||||
* @param point
|
||||
* @param radius
|
||||
* @param color
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public PathOverlay(GeoPoint point, int radius, int color){
|
||||
mMode = POINT;
|
||||
mRadius = radius;
|
||||
mStart = point;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param canvas canvas to be drawn on
|
||||
* @param mapView
|
||||
* @param shadow
|
||||
* @param when
|
||||
*/
|
||||
@Override
|
||||
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
|
||||
Projection projection = mapView.getProjection();
|
||||
Paint paint = new Paint();
|
||||
paint.setColor(mColor);
|
||||
paint.setAntiAlias(true);
|
||||
Point point = new Point();
|
||||
projection.toPixels(mStart, point);
|
||||
|
||||
switch (mMode){
|
||||
case POINT:
|
||||
RectF oval = new RectF(point.x - mRadius, point.y - mRadius, point.x + mRadius, point.y + mRadius);
|
||||
canvas.drawOval(oval, paint);
|
||||
case PATH:
|
||||
Point point2 = new Point();
|
||||
projection.toPixels(mEnd, point2);
|
||||
paint.setStrokeWidth(5);
|
||||
paint.setAlpha(120);
|
||||
canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
|
||||
}
|
||||
super.draw(canvas, mapView, shadow);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
* @author ricky barrette
|
||||
*/
|
||||
|
||||
package com.TwentyCodes.android.location;
|
||||
package com.TwentyCodes.android.overlays;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
@@ -12,6 +12,8 @@ import android.graphics.Paint.Style;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.RectF;
|
||||
|
||||
import com.TwentyCodes.android.location.GeoUtils;
|
||||
import com.TwentyCodes.android.location.OnLocationSelectedListener;
|
||||
import com.google.android.maps.GeoPoint;
|
||||
import com.google.android.maps.MapView;
|
||||
import com.google.android.maps.Overlay;
|
||||
@@ -30,7 +32,7 @@ public class RadiusOverlay extends Overlay{
|
||||
private float mRadius = 0;
|
||||
private int mColor = Color.GREEN;
|
||||
private GeoPoint mRadiusPoint;
|
||||
private LocationSelectedListener mListener;
|
||||
private OnLocationSelectedListener mListener;
|
||||
|
||||
/**
|
||||
* Creates a new RadiusOverlay
|
||||
@@ -150,7 +152,7 @@ public class RadiusOverlay extends Overlay{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void setLocationSelectedListener(LocationSelectedListener listener) {
|
||||
public void setLocationSelectedListener(OnLocationSelectedListener listener) {
|
||||
this.mListener = listener;
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,11 @@
|
||||
* @author ricky barrette
|
||||
* @date Oct 2, 2010
|
||||
*/
|
||||
package com.TwentyCodes.android.SkyHook;
|
||||
package com.TwentyCodes.android.overlays;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.TwentyCodes.android.location.UserOverlayBase;
|
||||
import com.TwentyCodes.android.SkyHook.SkyHook;
|
||||
import com.google.android.maps.MapView;
|
||||
|
||||
/**
|
||||
@@ -38,7 +38,7 @@ public class SkyHookUserOverlay extends UserOverlayBase{
|
||||
/**
|
||||
* Called when the location provider needs to be disabled
|
||||
* (non-Javadoc)
|
||||
* @see com.TwentyCodes.android.location.UserOverlayBase#onMyLocationDisabled()
|
||||
* @see com.TwentyCodes.android.overlays.UserOverlayBase#onMyLocationDisabled()
|
||||
*/
|
||||
@Override
|
||||
public void onMyLocationDisabled() {
|
||||
@@ -48,7 +48,7 @@ public class SkyHookUserOverlay extends UserOverlayBase{
|
||||
/**
|
||||
* Called when the location provider needs to be enabled
|
||||
* (non-Javadoc)
|
||||
* @see com.TwentyCodes.android.location.UserOverlayBase#onMyLocationEnabled()
|
||||
* @see com.TwentyCodes.android.overlays.UserOverlayBase#onMyLocationEnabled()
|
||||
*/
|
||||
@Override
|
||||
public void onMyLocationEnabled() {
|
||||
@@ -3,10 +3,11 @@
|
||||
* @author ricky barrette
|
||||
* @date Dec 28, 2010
|
||||
*/
|
||||
package com.TwentyCodes.android.location;
|
||||
package com.TwentyCodes.android.overlays;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.TwentyCodes.android.location.AndroidGPS;
|
||||
import com.google.android.maps.MapView;
|
||||
|
||||
/**
|
||||
@@ -4,7 +4,7 @@
|
||||
* @author ricky barrette
|
||||
* @author Twenty Codes, LLC
|
||||
*/
|
||||
package com.TwentyCodes.android.location;
|
||||
package com.TwentyCodes.android.overlays;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.ProgressDialog;
|
||||
@@ -22,7 +22,12 @@ import android.os.Handler;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
|
||||
import com.TwentyCodes.android.location.CompassListener;
|
||||
import com.TwentyCodes.android.location.GeoPointLocationListener;
|
||||
import com.TwentyCodes.android.location.GeoUtils;
|
||||
import com.TwentyCodes.android.location.R;
|
||||
import com.TwentyCodes.android.location.R.drawable;
|
||||
import com.TwentyCodes.android.location.R.string;
|
||||
import com.TwentyCodes.android.debug.Debug;
|
||||
import com.google.android.maps.GeoPoint;
|
||||
import com.google.android.maps.MapView;
|
||||
Reference in New Issue
Block a user