Output:
NOTE: Check your Android Manifest..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.test" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk android:minSdkVersion="8" /> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.WRITE_CONTACTS" /> | |
<uses-permission android:name="android.permission.READ_CONTACTS" /> | |
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > | |
</uses-permission> | |
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" > | |
</uses-permission> | |
<application | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" > | |
<activity | |
android:label="@string/app_name" | |
android:name=".JsonMapActivity" > | |
<intent-filter > | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<uses-library android:name="com.google.android.maps" > | |
</uses-library> | |
</application> | |
</manifest> |
My Layout.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<linearlayout android:layout_height="fill_parent" | |
android:layout_width="fill_parent" | |
android:orientation="vertical" | |
xmlns:android="http://schemas.android.com/apk/res/android"> | |
<button android:id="@+id/btnPutAddress" | |
android:layout_height="wrap_content" | |
android:layout_width="fill_parent" | |
android:text="Put New Address" > | |
</button> | |
<com .google.android.maps.mapview="" | |
android:apikey="your key" | |
android:id="@+id/mpBranch" | |
android:layout_height="fill_parent" | |
android:layout_width="fill_parent" /> | |
<linearlayout android:id="@+id/zoom" | |
android:layout_alignparentbottom="true" | |
android:layout_centerhorizontal="true" | |
android:layout_height="wrap_content" | |
android:layout_width="wrap_content"> | |
</linearlayout> | |
</linearlayout> |
1) Create class MyOverLay which extends Overlay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyOverLay extends Overlay | |
{ | |
private GeoPoint gp1; | |
private GeoPoint gp2; | |
//private int mRadius=6; | |
private int mode=0; | |
private int defaultColor; | |
private String text=""; | |
private Bitmap img = null; | |
Context mContext; | |
public MyOverLay(Context context,GeoPoint gp1,GeoPoint gp2,int mode) // GeoPoint is a int. (6E) | |
{ | |
this.gp1 = gp1; | |
this.gp2 = gp2; | |
this.mode = mode; | |
this.mContext = context; | |
defaultColor = 999; // no defaultColor | |
} | |
public MyOverLay(GeoPoint gp1,GeoPoint gp2,int mode, int defaultColor) | |
{ | |
this.gp1 = gp1; | |
this.gp2 = gp2; | |
this.mode = mode; | |
this.defaultColor = defaultColor; | |
} | |
public void setText(String t) | |
{ | |
this.text = t; | |
} | |
public void setBitmap(Bitmap bitmap) | |
{ | |
this.img = bitmap; | |
} | |
public int getMode() | |
{ | |
return mode; | |
} | |
@Override | |
public boolean draw | |
(Canvas canvas, MapView mapView, boolean shadow, long when) | |
{ | |
Projection projection = mapView.getProjection(); | |
if (shadow == false) | |
{ | |
Paint paint = new Paint(); | |
paint.setAntiAlias(true); | |
Point point = new Point(); | |
projection.toPixels(gp1, point); | |
// mode=1:start | |
if(mode==1) | |
{ | |
if(defaultColor==999) | |
paint.setColor(Color.BLUE); | |
else | |
paint.setColor(defaultColor); | |
// start point | |
} | |
// mode=2:path | |
else if(mode==2) | |
{ | |
if(defaultColor==999) | |
paint.setColor(Color.RED); | |
else | |
paint.setColor(defaultColor); | |
Point point2 = new Point(); | |
projection.toPixels(gp2, point2); | |
paint.setStrokeWidth(5); | |
paint.setAlpha(120); | |
canvas.drawLine(point.x, point.y, point2.x,point2.y, paint); | |
} | |
/* mode=3:end */ | |
else if(mode==3) | |
{ | |
/* the last path */ | |
if(defaultColor==999) | |
paint.setColor(Color.GREEN); | |
else | |
paint.setColor(defaultColor); | |
Point point2 = new Point(); | |
projection.toPixels(gp2, point2); | |
paint.setStrokeWidth(5); | |
paint.setAlpha(120); | |
canvas.drawLine(point.x, point.y, point2.x,point2.y, paint); | |
/* end point */ | |
} | |
} | |
return super.draw(canvas, mapView, shadow, when); | |
} | |
} |
2) Create another class CustomItemizedOverlay which extends ItemizedOverlay<OverlayItem>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> { | |
private final ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>(); | |
private Context context; | |
public CustomItemizedOverlay(Drawable defaultMarker) { | |
super(boundCenterBottom(defaultMarker)); | |
} | |
public CustomItemizedOverlay(Drawable defaultMarker, Context context) { | |
this(defaultMarker); | |
this.context = context; | |
} | |
@Override | |
protected OverlayItem createItem(int i) { | |
return mapOverlays.get(i); | |
} | |
@Override | |
public int size() { | |
return mapOverlays.size(); | |
} | |
public void addOverlay(OverlayItem overlay) { | |
mapOverlays.add(overlay); | |
this.populate(); | |
} | |
@Override | |
protected boolean onTap(int index) { | |
OverlayItem item = mapOverlays.get(index); | |
AlertDialog.Builder dialog = new AlertDialog.Builder(context); | |
dialog.setTitle(item.getTitle());dialog.setMessage(item.getSnippet()); | |
dialog.setNeutralButton("OK", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
dialog.dismiss(); | |
} | |
}); | |
dialog.show(); | |
return true; | |
} | |
} |
3) Now Create Main Activity which extends MapActivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JsonMapActivity extends MapActivity { | |
/** Called when the activity is first created. */ | |
static double srclongitute, destlongitute; | |
static double srclatitude, destlatitude; | |
static String distance_km = ""; | |
MapView mapView; | |
private JsonMapActivity _activity; | |
GeoPoint srcGeoPoint, destGeoPoint; | |
private static List<Overlay> mOverlays; | |
static String distance = ""; | |
JSONObject jsonObj; | |
static String _path, start_address, end_address; | |
Drawable srcdrawable; | |
static String GPsLoc; | |
Context context = this; | |
Button btnDialog; | |
LocationManager locationManager; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
jsonObj = getLocationInfo("GandhiNagar", "Surat"); | |
Toast.makeText(getApplicationContext(), "" + jsonObj.length(), 5) | |
.show(); | |
if (getLatLong(jsonObj)) { | |
Log.i("srcLati", "" + srclatitude); | |
Log.i("srcLungi", "" + srclongitute); | |
Log.i("destLati", "" + destlatitude); | |
Log.i("destLungi", "" + destlongitute); | |
} | |
_activity = this; | |
mapView = (MapView) findViewById(R.id.mpBranch); | |
mapView.setBuiltInZoomControls(true); | |
mapView.displayZoomControls(true); | |
mapView.setClickable(true); | |
srcGeoPoint = new GeoPoint((int) (srclatitude * 1E6), | |
(int) (srclongitute * 1E6)); | |
destGeoPoint = new GeoPoint((int) (destlatitude * 1E6), | |
(int) (destlongitute * 1E6)); | |
List<Overlay> mapOverlays = mapView.getOverlays(); | |
srcdrawable = getApplicationContext().getResources().getDrawable( | |
R.drawable.map_user);// // user | |
CustomItemizedOverlay srcitemizedOverlay = new CustomItemizedOverlay( | |
srcdrawable, this); | |
OverlayItem srcoverlayitem = new OverlayItem(srcGeoPoint, | |
"My Location", start_address); | |
Drawable destdrawable = this.getResources().getDrawable( | |
R.drawable.map_restaurant); // rest | |
CustomItemizedOverlay destitemizedOverlay = new CustomItemizedOverlay( | |
destdrawable, this); | |
OverlayItem destoverlayitem = new OverlayItem(destGeoPoint, | |
"FoodCorner Branch", end_address); | |
srcitemizedOverlay.addOverlay(srcoverlayitem); | |
destitemizedOverlay.addOverlay(destoverlayitem); | |
mapOverlays.add(srcitemizedOverlay); | |
mapOverlays.add(destitemizedOverlay); | |
connectAsyncTask _connectAsyncTask = new connectAsyncTask(); | |
_connectAsyncTask.execute(); | |
mOverlays = mapView.getOverlays(); | |
mapView.getController().animateTo(srcGeoPoint); | |
mapView.getController().setZoom(8); | |
} | |
@Override | |
protected boolean isRouteDisplayed() { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
public static JSONObject getLocationInfo(String origin, String dest) { | |
StringBuilder stringBuilder = new StringBuilder(); | |
try { | |
origin = origin.replaceAll(" ", "%20"); | |
dest = dest.replaceAll(" ", "%20"); | |
HttpPost httppost = new HttpPost( | |
"http://maps.googleapis.com/maps/api/directions/json?origin=" | |
+ origin + "&destination=" + dest + "&sensor=false"); | |
// http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&sensor=false | |
HttpClient client = new DefaultHttpClient(); | |
HttpResponse response; | |
stringBuilder = new StringBuilder(); | |
response = client.execute(httppost); | |
HttpEntity entity = response.getEntity(); | |
InputStream stream = entity.getContent(); | |
int b; | |
while ((b = stream.read()) != -1) { | |
stringBuilder.append((char) b); | |
} | |
} catch (ClientProtocolException e) { | |
} catch (IOException e) { | |
} | |
JSONObject jsonObject = new JSONObject(); | |
try { | |
jsonObject = new JSONObject(stringBuilder.toString()); | |
} catch (JSONException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return jsonObject; | |
} | |
// -------------------------------------------------------------------------- | |
// class Asych task | |
private class connectAsyncTask extends AsyncTask<Void, Void, Void> { | |
@Override | |
protected void onPreExecute() { | |
// TODO Auto-generated method stub | |
super.onPreExecute(); | |
// SHOW YOU PROGRESS BAR HERE | |
} | |
@Override | |
protected Void doInBackground(Void... params) { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Void result) { | |
// TODO Auto-generated method stub | |
super.onPostExecute(result); | |
if (_path != null) { | |
Overlay ol = new MyOverLay(_activity, srcGeoPoint, srcGeoPoint, | |
1); | |
mOverlays.add(ol); | |
List<GeoPoint> _geopoints = decodePoly(_path); | |
GeoPoint gp1; | |
GeoPoint gp2; | |
gp2 = _geopoints.get(0); | |
for (int i = 1; i < _geopoints.size(); i++) // the last one | |
// would be | |
// crash | |
{ | |
gp1 = gp2; | |
gp2 = _geopoints.get(i); | |
Overlay ol1 = new MyOverLay(gp1, gp2, 2, Color.BLUE); | |
mOverlays.add(ol1); | |
} | |
Overlay ol2 = new MyOverLay(_activity, destGeoPoint, | |
destGeoPoint, 3); | |
mOverlays.add(ol2); | |
// DISMISS PROGRESS BAR HERE | |
} else { | |
// showAlert AS "Unable to find the route" | |
} | |
Overlay ol2 = new MyOverLay(_activity, destGeoPoint, destGeoPoint, | |
3); | |
mOverlays.add(ol2); | |
// DISMISS PROGRESS BAR HERE | |
} | |
}// end of class | |
// -------------------------------------------------------------------------- | |
private List<GeoPoint> decodePoly(String encoded) { | |
List<GeoPoint> poly = new ArrayList<GeoPoint>(); | |
int index = 0, len = encoded.length(); | |
int lat = 0, lng = 0; | |
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)); | |
poly.add(p); | |
} | |
return poly; | |
} | |
// -------------------------------------------------- | |
public static boolean getLatLong(JSONObject jsonObject) { | |
try { | |
JSONObject jsonOb = ((JSONArray) jsonObject.get("routes")) | |
.getJSONObject(0); | |
JSONArray jsonArray = jsonOb.getJSONArray("legs"); | |
JSONObject j1 = jsonArray.getJSONObject(0); | |
srclatitude = j1.getJSONObject("start_location").getDouble("lat"); | |
srclongitute = j1.getJSONObject("start_location").getDouble("lng"); | |
destlatitude = j1.getJSONObject("end_location").getDouble("lat"); | |
destlongitute = j1.getJSONObject("end_location").getDouble("lng"); | |
start_address = j1.getString("start_address"); | |
end_address = j1.getString("end_address"); | |
JSONObject ja = j1.getJSONObject("distance"); | |
// Log.i("jsonOb size", "" + ja.getString("text")); | |
JSONObject ja_line = jsonOb.getJSONObject("overview_polyline"); | |
// Log.i("ja_line size", "" + ja_line.getString("points")); | |
_path = ja_line.getString("points"); | |
} catch (JSONException e) { | |
return false; | |
} | |
return true; | |
} | |
public class MyLocationListener implements LocationListener | |
{ | |
@Override | |
public void onLocationChanged(Location loc) | |
{ | |
GPsLoc=new Double(loc.getLatitude()).toString()+","; | |
GPsLoc+=loc.getLongitude(); | |
String Text = "My current location is: " + "Latitud = " | |
+ loc.getLatitude() + "Longitud = " + loc.getLongitude(); | |
Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT) | |
.show(); | |
} | |
@Override | |
public void onProviderDisabled(String provider) | |
{ | |
Toast.makeText(getApplicationContext(), "Gps Disabled", | |
Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onProviderEnabled(String provider) | |
{ | |
Toast.makeText(getApplicationContext(), "Gps Enabled", | |
Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) | |
{ | |
} | |
}/* End of Class MyLocationListener */ | |
} |