Would be a possibility.
thx for that

) if you do that. 
package com.bandpsoftware.mapper;
/**
* Google Maps Tutorial
* Txt2Java pCode Generated File
* Version 1.0.1.4
* B&P Software 2012
*
* This requires the Google API (Android 2.1)
* The mapping requires the correct API key in main.xml (B&P's won't work for other compilers)
* Uses the direction service to draw a blue line between the endpoints of the direction steps
* Stores the written directions also
*
*/
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
public class Mapper extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
ArrayList<Direction> directions = new ArrayList<Direction>();
Direction dir = null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
getDirections("http://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&sensor=false");
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true); //newer one
// This sets up where to show a push pin
String coordinates[] = {"42.35849","-71.06010"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
// This gets everything going
goToPoint();
showPosition();
}//oncreate
@Override
protected boolean isRouteDisplayed() {
return true;
}//isroutedisplayed
// This moves the map center to point p
public void goToPoint() {
int cx = (int) (((directions.get(0).lat+directions.get(directions.size()-1).lat)/2d)*1E6);
int cy = (int) (((directions.get(0).lng+directions.get(directions.size()-1).lng)/2d)*1E6);
GeoPoint gp = new GeoPoint(cx,cy);
int latSpan = (int) (Math.abs(directions.get(0).lat-directions.get(directions.size()-1).lat)*1E6);
int lngSpan = (int) (Math.abs(directions.get(0).lng-directions.get(directions.size()-1).lng)*1E6);
mc = mapView.getController();
mc.zoomToSpan(latSpan, lngSpan);
mc.animateTo(gp);
}//goto
// This shows the pushpin at point p
public void showPosition() {
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}//showpos
class MapOverlay extends Overlay {
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(5);
Point screenPts = new Point();
GeoPoint gp;
int i = 0;
float lx = 0;
float ly = 0;
while (i<directions.size()) {
dir = directions.get(i);
gp = new GeoPoint((int) (dir.lat * 1E6), (int) (dir.lng * 1E6));
mapView.getProjection().toPixels(gp, screenPts);
if (i > 0) canvas.drawLine(lx, ly, screenPts.x, screenPts.y, paint);
lx = screenPts.x;
ly = screenPts.y;
i++;
}//drawing route
mapView.getProjection().toPixels(p, screenPts);
// This puts the marker on the map
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pushpin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-46, null);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
// Touch event
// When the map is pressed and released the pushpin goes
// to that point
if (event.getAction() == 1) {
p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
goToPoint();
showPosition();
}//if
return false;
}//ontouch
}//mapoverlay class
//http://maps.googleapis.com/maps/api/directions/output?parameters
//http://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&sensor=false
public void getDirections(String loginUrl) {
JSONObject jArray = null;
String result = "";
String s;
String s1;
String s2;
directions.clear();
try{
HttpGet request = new HttpGet(loginUrl);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity entityResponse = response.getEntity();
result = EntityUtils.toString(entityResponse);
} catch(Exception e) {
Log.e("log_tag", "Error converting result "+e.toString());
}
try {
jArray = new JSONObject(result);
} catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
try {
JSONArray array = jArray.getJSONArray("routes");
JSONObject routes = array.getJSONObject(0);
JSONArray legs = routes.getJSONArray("legs");
JSONObject steps = legs.getJSONObject(0);
JSONArray legs1 = steps.getJSONArray("steps");
for(int i = 0; i < legs1.length(); i++){
JSONObject steps1 = legs1.getJSONObject(i);
if (i == 0) s = steps1.getString("start_location");
else s = steps1.getString("end_location");
s1 = s.substring(s.indexOf(":")+1); //cut off beginning
s = s1.substring(s1.indexOf(":")+1, s1.indexOf("}")); //isolate longitude
s1 = s1.substring(0, s1.indexOf(",")); //isolate latitude
s.replaceAll(" ", ""); //remove spaces
s1.replaceAll(" ", "");
s2 = steps1.getString("html_instructions");
dir = new Direction(Double.parseDouble(s),Double.parseDouble(s1),s2);
directions.add(dir);
}//for
} catch(Exception e) {
Log.e("log_tag", "Error converting result "+e.toString());
}
}//getdirections
public class Direction {
public double lng = 0;
public double lat = 0;
public String txt = "";
public Direction(double latitude, double longitude, String written) {
lng = longitude;
lat = latitude;
txt = written;
}//constructor
}//direction class
//TODO: Fill In Methods Etc.
}//class

.



Return to Other Coding-Problems
Users browsing this forum: No registered users and 7 guests