package com.algobase.activity; import java.io.File; import java.util.List; import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.content.DialogInterface; import android.graphics.Canvas; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Paint; import android.graphics.Paint.Align; import android.graphics.Path; import android.graphics.Point; import android.graphics.drawable.GradientDrawable; import android.location.Location; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.view.Display; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.ImageButton; import android.widget.Toast; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.GeoPoint; import com.google.android.maps.Overlay; import com.google.android.maps.Projection; import com.algobase.stracks.sTracksRoot; import com.algobase.share.dialog.*; import com.algobase.share.system.*; import com.algobase.share.network.*; import com.algobase.share.geo.*; import com.algobase.share.maps.*; import com.algobase.map.*; import com.algobase.stracks.R; public class WayPointMapActivity extends MapActivity { class OverlayPoint extends Overlay { WayPointMapActivity root; Paint paint; double current_lat = 0; double current_lon = 0; double current_alt = 0; GeoPoint wp_point; Bitmap wpBitmap; GeoPoint home_point = null; Bitmap homeBitmap; float node_width = 12; MapView map_view; MapController map_controller; public double getCurrentLongitude() { return current_lon; } public double getCurrentLatitude() { return current_lat; } public Location getCurrentLocation() { Location loc = new Location("map"); loc.setLatitude(current_lat); loc.setLongitude(current_lon); loc.setAltitude(current_alt); return loc; } public OverlayPoint(WayPointMapActivity r, MapView mv) { root = r; map_view = mv; map_controller = mv.getController(); paint = new Paint(); paint.setDither(true); paint.setAntiAlias(true); paint.setColor(Color.RED); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStrokeCap(Paint.Cap.ROUND); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(1); } void window_coords(double[] lat, double[] lon) { int width = map_view.getWidth(); int height = map_view.getHeight(); Projection proj = map_view.getProjection(); GeoPoint top_left = proj.fromPixels(0,0); GeoPoint bot_right = proj.fromPixels(width-1,height-1); lon[0] = top_left.getLongitudeE6()/1E6; lon[1] = bot_right.getLongitudeE6()/1E6; lat[0] = bot_right.getLatitudeE6()/1E6; lat[1] = top_left.getLatitudeE6()/1E6; } public void set_node_width(float w) { node_width = w; } public void setPoint(Location loc,boolean anim) { current_alt = loc.getAltitude(); setPoint(loc.getLatitude(),loc.getLongitude(),anim); } public void setPoint(double lat, double lon, boolean anim) { current_lat = lat; current_lon = lon; GeoPoint p = new GeoPoint((int)(1000000*lat),(int)(1000000*lon)); if (anim) map_controller.animateTo(p); else map_controller.setCenter(p); } public void setAltitude(double alt) { current_alt = alt; } public void setHomePoint(Location loc) { if (loc != null) { double lat = loc.getLatitude(); double lon = loc.getLongitude(); home_point = new GeoPoint((int)(1000000*lat),(int)(1000000*lon)); } else home_point = null; } public void setWayPoint(Location loc) { if (loc != null) { double lat = loc.getLatitude(); double lon = loc.getLongitude(); current_alt = loc.getAltitude(); wp_point = new GeoPoint((int)(1000000*lat),(int)(1000000*lon)); } else wp_point = null; } public void setHomeBitmap(int res) { homeBitmap = BitmapFactory.decodeResource(getResources(),res); } public void setWpBitmap(int res) { wpBitmap = BitmapFactory.decodeResource(getResources(),res); } void drawTile(Canvas canvas, String tile, boolean highlighted) { if (tile.equals("")) return; int lat = Integer.parseInt(tile.substring(1,3)); if (tile.substring(0,1).equals("S")) lat = -lat; int lon = Integer.parseInt(tile.substring(4,7)); if (tile.substring(3,4).equals("W")) lon = -lon; GeoPoint a = new GeoPoint(1000000*lat,1000000*lon); GeoPoint b = new GeoPoint(1000000*lat,1000000*(lon+1)); GeoPoint c = new GeoPoint(1000000*(lat+1),1000000*(lon+1)); GeoPoint d = new GeoPoint(1000000*(lat+1),1000000*lon); Path path = new Path(); Point p = new Point(); map_view.getProjection().toPixels(a,p); path.moveTo(p.x,p.y); Point q = new Point(); map_view.getProjection().toPixels(b,q); path.lineTo(q.x,q.y); int width = q.x - p.x; map_view.getProjection().toPixels(c,p); path.lineTo(p.x,p.y); map_view.getProjection().toPixels(d,p); path.lineTo(p.x,p.y); path.close(); paint.setStyle(Paint.Style.FILL); paint.setARGB(100,150,150,150); if (highlighted) paint.setARGB(128,255,0,0); canvas.drawPath(path,paint); paint.setStrokeWidth(3); paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.BLUE); canvas.drawPath(path,paint); GeoPoint gp = new GeoPoint((int)(1000000*(lat+0.5)), (int)(1000000*(lon+0.5))); map_view.getProjection().toPixels(gp,p); paint.setStyle(Paint.Style.FILL); paint.setTextAlign(Align.CENTER); paint.setTextSize((int)(width/4.5f)); //paint.setColor(Color.YELLOW); paint.setColor(Color.rgb(255,255,128)); canvas.drawText(tile,p.x,p.y,paint); } public void drawSRTM3(Canvas canvas) { double Lat[] = new double[2]; double Lon[] = new double[2]; window_coords(Lat,Lon); double span = Lon[1] - Lon[0]; if (span > 0.5 && span < 50) { File[] files = root.get_srtm3_folder().listFiles(); for(int i=0; i max_lines) return; if (map_view.isSatellite()) paint.setARGB(255,255,255,200); else paint.setARGB(128,64,64,64); paint.setStrokeWidth((int)(0.5 +pix/2.5)); lon1 = Math.floor(1200*lon1 + 0.5)/1200.0; lat1 = Math.floor(1200*lat1 + 0.5)/1200.0; Projection proj = map_view.getProjection(); int n = (int)(2 + (lat2-lat1)/d) * (int)(2 + (lon2-lon1)/d); float[] coords = new float[8*n]; int i = 0; Point p = new Point(); for(double lat=lat1; lat <= lat2; lat+=d) { for(double lon=lon1; lon <= lon2; lon+=d) { GeoPoint gp = new GeoPoint((int)(1000000*lat),(int)(1000000*lon)); proj.toPixels(gp,p); coords[i++] = p.x-pix; coords[i++] = p.y; coords[i++] = p.x+pix; coords[i++] = p.y; coords[i++] = p.x; coords[i++] = p.y-pix; coords[i++] = p.x; coords[i++] = p.y+pix; } } canvas.drawLines(coords,0,i,paint); } public void drawCurrentPoint(Canvas canvas, double lat, double lon, double alt) { GeoPoint gp = new GeoPoint((int)(1000000*lat),(int)(1000000*lon)); final boolean crosshairs = true; Point p = new Point(); map_view.getProjection().toPixels(gp,p); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(node_width/4); if (map_view.isSatellite()) paint.setColor(Color.YELLOW); else paint.setARGB(128,64,64,64); if (crosshairs) { float w = node_width; int d = 2000; canvas.save(); canvas.translate(p.x,p.y); canvas.drawLine(w,0,d,0,paint); canvas.drawLine(-w,0,-d,0,paint); canvas.drawLine(0,w,0,d,paint); canvas.drawLine(0,-w,0,-d,paint); canvas.restore(); } /* paint.setARGB(128,255,0,0); paint.setStyle(Paint.Style.FILL); canvas.drawCircle(p.x, p.y, node_width, paint); */ if (map_view.isSatellite()) paint.setColor(Color.YELLOW); else paint.setColor(Color.BLACK); canvas.drawCircle(p.x, p.y, node_width, paint); paint.setStyle(Paint.Style.FILL); paint.setTextSize(3.5f*node_width); if (map_view.isSatellite()) paint.setColor(Color.YELLOW); else paint.setColor(Color.BLUE); float nw = node_width; paint.setTextAlign(Align.LEFT); canvas.drawText(String.format("%8.5f",lat),nw,p.y-0.75f*nw,paint); //if (current_alt >= 0) if (current_alt > 0) canvas.drawText(String.format("%.0f m",current_alt),p.x+1.2f*nw,p.y-0.75f*nw,paint); paint.setTextAlign(Align.CENTER); canvas.drawText(String.format("%8.5f",lon),p.x-0.75f*nw,2*p.y-nw,paint); } public void drawBitmap(Canvas canvas, GeoPoint gp, Bitmap bmp, boolean home) { Point p = new Point(); map_view.getProjection().toPixels(gp,p); int w = bmp.getWidth(); int h = bmp.getHeight(); Paint paint = new Paint(); if (home) { // apply filter float[] f; if (map_view.isSatellite()) f = new float[] {2.5f, 0, 0, 0, 0, 0, 2.5f, 0, 0, 0, 0, 0, 1.0f, 0, 0, 0, 0, 0, 1, 0}; else f = new float[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0.75f, 0}; ColorMatrixColorFilter filter = new ColorMatrixColorFilter(new ColorMatrix(f)); paint.setColorFilter(filter); } p.x -= w/2; if (home) p.y -= h/2; else p.y -= h; canvas.drawBitmap(bmp, p.x, p.y, paint); } public void drawPoint(Canvas canvas, GeoPoint gp, int r, int g, int b) { Point p = new Point(); map_view.getProjection().toPixels(gp,p); paint.setARGB(128,r,g,b); paint.setStyle(Paint.Style.FILL); canvas.drawCircle(p.x, p.y, node_width, paint); if (map_view.isSatellite()) paint.setColor(Color.WHITE); else paint.setColor(Color.BLACK); paint.setStrokeWidth(1); paint.setStyle(Paint.Style.STROKE); canvas.drawCircle(p.x, p.y, node_width, paint); } @Override public void draw(Canvas canvas, MapView mapv, boolean shadow) { super.draw(canvas, mapv, shadow); if (shadow) return; //drawSRTM3(canvas); //drawGrid(canvas); double lat[] = new double [2]; double lon[] = new double [2]; window_coords(lat,lon); current_lat = (lat[0] + lat[1])/2; current_lon = (lon[0] + lon[1])/2; //show_toast("home_point: " + home_point); //show_toast("wp_point: " + wp_point); if (home_point != null) drawBitmap(canvas,home_point,homeBitmap,true); if (wp_point != null) drawBitmap(canvas,wp_point,wpBitmap,false); drawCurrentPoint(canvas,current_lat,current_lon,current_alt); } } public final static String EXTRA_LANGUAGE = "waypoint_language"; public final static String EXTRA_SRTM3_URL = "waypoint_srtm3_url"; public final static String EXTRA_NEW = "waypoint_new"; public final static String EXTRA_NAME = "waypoint_name"; public final static String EXTRA_LATITUDE = "waypoint_latitude"; public final static String EXTRA_LONGITUDE = "waypoint_longitude"; public final static String EXTRA_ALTITUDE = "waypoint_altitude"; public final static String EXTRA_HOME_LATITUDE = "waypoint_home_latitude"; public final static String EXTRA_HOME_LONGITUDE = "waypoint_home_longitude"; public final static String EXTRA_HOME_ALTITUDE = "waypoint_home_altitude"; public final static String EXTRA_ZOOM = "waypoint_zoom"; public final static String EXTRA_SELECTED = "waypoint_selected"; public final static String EXTRA_STRACKS_USER = "waypoint_user"; public final static String EXTRA_STRACKS_PASSWORD = "waypoint_password"; WayPointMapActivity activity = this; String language = "English"; String string_cancel = "Cancel"; String string_ok = "OK"; String string_apply = "Apply"; String string_search = "Search"; String string_address = "Address"; String string_city = "City"; String string_street = "Street"; String string_define_home = "Define Home Location"; String string_define_waypoint = "Define Waypoint"; String string_new_waypoint = "New Waypoint"; String string_home_position = "Home Position"; String srtm3_url; String wp_name = ""; boolean wp_new = false; String user = ""; String password = ""; String title; TextView title_view; MapView map_view; MapController map_controller; File srtm3_folder; srtm3Matrix srtm3 = null; Location initial_location = null; Location home_location = null; ImageButton but_zoom_out; ImageButton but_zoom_in; ImageButton but_zoom_fit; //ImageButton but_sat; ImageButton but_search; ImageButton but_lock; ImageButton but_mode; ImageButton but_home; ImageButton but_menu; OverlayPoint map_overlay; Display display; float client_version = 0; Handler handler = new Handler(); public int get_screen_width() { return display.getWidth(); } public int get_screen_height() { return display.getHeight(); } public File get_srtm3_folder() { return srtm3_folder; } private void show_toast(final String txt) { handler.post( new Runnable() { @Override public void run() { Toast.makeText(activity,txt, Toast.LENGTH_SHORT).show(); } }); } @Override public void onCreate(Bundle savedInstanceState) { //show_toast("onCreate"); super.onCreate(savedInstanceState); boolean debug_build = false; try { PackageManager pm = getPackageManager(); PackageInfo pi = pm.getPackageInfo(getPackageName(), 0); debug_build = ((pi.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0); client_version = 0.001f * pi.versionCode; } catch (Exception e) { } setContentView(R.layout.map_waypoint); activity = this; WindowManager mgr = (WindowManager)getSystemService(Context.WINDOW_SERVICE); display = mgr.getDefaultDisplay(); int ds = MyDialog.STYLE_HOLO_LIGHT; MyDialog.setStyle(ds); Intent intent = getIntent(); Bundle bundle = intent.getExtras(); if (intent.hasExtra(EXTRA_SRTM3_URL)) srtm3_url = intent.getStringExtra(EXTRA_SRTM3_URL); language = bundle.getString(EXTRA_LANGUAGE); wp_name = bundle.getString(EXTRA_NAME); wp_new = bundle.getBoolean(EXTRA_NEW); double lat = bundle.getDouble(EXTRA_LATITUDE,0); double lon = bundle.getDouble(EXTRA_LONGITUDE,0); double alt = bundle.getDouble(EXTRA_ALTITUDE,0); double home_lat = bundle.getDouble(EXTRA_HOME_LATITUDE,0); double home_lon = bundle.getDouble(EXTRA_HOME_LONGITUDE,0); double home_alt = bundle.getDouble(EXTRA_HOME_ALTITUDE,0); user = bundle.getString(EXTRA_STRACKS_USER); password = bundle.getString(EXTRA_STRACKS_PASSWORD); int zoom_level = bundle.getInt(EXTRA_ZOOM); home_location = null; if (home_lat != 0 || home_lon != 0) { home_location = new Location("gps"); home_location.setLatitude(home_lat); home_location.setLongitude(home_lon); home_location.setAltitude(home_alt); } if (language.equals("Deutsch")) { string_cancel = "Abbrechen"; string_apply = "Übernehmen"; string_search = "Suchen"; string_address = "Adresse"; string_city = "Stadt"; string_street = "Straße"; string_define_home = "Home Position festlegen"; string_define_waypoint = "Waypoint definieren"; string_new_waypoint = "New Waypoint"; } if (wp_new) title = string_define_waypoint; else if (wp_name.startsWith("Home")) title = string_define_home; else title = "Waypoint: " + wp_name; Button but1 = (Button)findViewById(R.id.button_cancel); but1.setText(string_cancel); but1.setOnClickListener(new OnClickListener() { public void onClick(View v) { finish(); } }); Button but2 = (Button)findViewById(R.id.button_select); but2.setText(string_apply); but2.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent extra = new Intent(); Location loc = map_overlay.getCurrentLocation(); extra.putExtra(EXTRA_LATITUDE,loc.getLatitude()); extra.putExtra(EXTRA_LONGITUDE,loc.getLongitude()); extra.putExtra(EXTRA_ALTITUDE,loc.getAltitude()); extra.putExtra(EXTRA_NAME,wp_name); extra.putExtra(EXTRA_SELECTED,true); setResult(RESULT_OK, extra); finish(); } }); ViewGroup root_view = (ViewGroup)findViewById(R.id.map_layout); File sd_root = Environment.getExternalStorageDirectory(); File program_folder = new File(sd_root,"sTracks"); srtm3_folder = new File(program_folder,"srtm3"); Runtime rt = Runtime.getRuntime(); float max_mb = (float)rt.maxMemory()/(1024*1024); title_view = (TextView)findViewById(R.id.title); int[] clrs_red = new int[] {0xffdd3344, 0xff880a15, 0xffa11d30 }; int[] clrs_green = new int[] {0xff50a870, 0xff005030, 0xff208040}; int[] clrs_blue = new int[] {0xff5070a8, 0xff003050, 0xff204080}; int[] clrs_grey = new int[] {0xff888888, 0xff555555, 0xff999999}; GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,clrs_blue); //gd.setStroke(1,Color.BLACK); title_view.setBackground(gd); title_view.setText(title); String api_release_key = getResources().getString(R.string.api_release_key); String api_debug_key = getResources().getString(R.string.api_debug_key); //map_view = (MapView)findViewById(R.id.mapview); String key = (debug_build) ? api_debug_key : api_release_key; map_view = new MapView(this,key) { @Override public boolean onTouchEvent(MotionEvent event) { double lon = map_overlay.getCurrentLongitude(); double lat = map_overlay.getCurrentLatitude(); double alt = (srtm3 != null) ? srtm3.getAltitude(lat,lon) : 0; map_overlay.setAltitude(alt); /* if (event.getAction() == MotionEvent.ACTION_DOWN) map_overlay.setAltitude(0); else if (event.getAction() == MotionEvent.ACTION_UP) { double lon = map_overlay.getCurrentLongitude(); double lat = map_overlay.getCurrentLatitude(); double alt = (srtm3 != null) ? srtm3.getAltitude(lat,lon) : 0; map_overlay.setAltitude(alt); //title_view.setText(title); } */ return super.onTouchEvent(event); } }; map_view.setClickable(true); map_view.setSatellite(false); root_view.addView(map_view,0); //map_view.setBuiltInZoomControls(true); //map_view.getZoomButtonsController().setAutoDismissed(false); map_controller = map_view.getController(); map_controller.setZoom(16); map_overlay = new OverlayPoint(this,map_view); map_view.getOverlays().add(map_overlay); map_overlay.setHomeBitmap(R.drawable.home36); map_overlay.setWpBitmap(R.drawable.wpoint_red20); if (home_location != null) map_overlay.setHomePoint(home_location); map_overlay.set_node_width(display.getWidth()/60.0f); // initial position if (lat == 0 && lon == 0) { GeoPoint gp = map_view.getMapCenter(); lon = gp.getLongitudeE6()/1000000.0; lat = gp.getLatitudeE6()/1000000.0; alt = 0; } initial_location = new Location("map"); initial_location.setLatitude(lat); initial_location.setLongitude(lon); initial_location.setAltitude(alt); if (!wp_new && !wp_name.startsWith("Home")) map_overlay.setWayPoint(initial_location); // buttons /* but_sat = (ImageButton)findViewById(R.id.button_sat); but_sat.setOnClickListener(new OnClickListener() { public void onClick(View v) { boolean sat = map_view.isSatellite(); map_view.setSatellite(!sat); } }); */ but_lock = (ImageButton)findViewById(R.id.button_lock); but_lock.setVisibility(View.GONE); but_mode = (ImageButton)findViewById(R.id.button_mode); but_mode.setVisibility(View.GONE); but_menu = (ImageButton)findViewById(R.id.button_menu); but_menu.setVisibility(View.GONE); but_search = (ImageButton)findViewById(R.id.button_search); but_search.setOnClickListener(new OnClickListener() { public void onClick(View v) { address_dialog(); } }); but_home = (ImageButton)findViewById(R.id.button_home); but_home.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (home_location != null) map_overlay.setPoint(home_location,true); } }); but_zoom_out = (ImageButton)findViewById(R.id.button_zoom_out); but_zoom_out.setOnClickListener(new OnClickListener() { public void onClick(View v) { map_controller.zoomOut(); } }); but_zoom_in = (ImageButton)findViewById(R.id.button_zoom_in); but_zoom_in.setOnClickListener(new OnClickListener() { public void onClick(View v) { map_controller.zoomIn(); } }); but_zoom_fit = (ImageButton)findViewById(R.id.button_zoom_fit); but_zoom_fit.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (initial_location != null) map_overlay.setPoint(initial_location,true); } }); srtm3 = new srtm3Matrix(srtm3_folder,initial_location,srtm3_url) { @Override protected void show_error(String msg) { activity.show_toast(msg); } }; srtm3.set_auto_download(false); map_controller.setZoom(zoom_level); map_overlay.setPoint(initial_location,false); new MyThread() { public void run() { int count = 10; double alt = sTracksRoot.DISTANCE_UNDEFINED; while (count-- > 0 && alt == sTracksRoot.DISTANCE_UNDEFINED) { sleep(500); alt = srtm3.getAltitude(initial_location); } //show_toast("count = " + count); if (alt != sTracksRoot.DISTANCE_UNDEFINED) { initial_location.setAltitude(alt); map_overlay.setAltitude(alt); map_view.postInvalidate(); } } }.start(); } @Override protected void onResume() { //show_toast("onResume"); super.onResume(); } @Override public void onBackPressed() { super.onBackPressed(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); //show_toast("onConfigurationChanged"); //map_overlay.zoomToPath(); } @Override protected boolean isRouteDisplayed() { return false; } public void address_dialog() { String street = "Kenner Weg 25"; String city = "Trier"; MyDialog dialog = new MyDialog(this,string_address); View view = dialog.addView(R.layout.dialog_address); final TextView txt_street= (TextView)view.findViewById(R.id.text_street); txt_street.setText(string_street); final EditText edt_street= (EditText)view.findViewById(R.id.edit_street); edt_street.setText(street); edt_street.setSelection(street.length()); final TextView txt_city= (TextView)view.findViewById(R.id.text_city); txt_city.setText(string_city); final EditText edt_city= (EditText)view.findViewById(R.id.edit_city); edt_city.setText(city); edt_city.setSelection(city.length()); final Geocoder geo = new Geocoder(this); dialog.setPositiveButton(string_search, new DialogInterface.OnClickListener() { public void onClick(DialogInterface d, int which) { String address = edt_street.getText().toString(); address += ","; address += edt_city.getText().toString(); try { List
addresses = geo.getFromLocationName(address,1); if (addresses.size() > 0) { Address addr = addresses.get(0); double lat = addr.getLatitude(); double lon = addr.getLongitude(); map_overlay.setPoint(lat,lon,true); map_controller.setZoom(17); double alt = 0; if (srtm3 != null) alt = srtm3.getAltitude(lat,lon); map_overlay.setAltitude(alt); map_view.postInvalidate(); title_view.setText(title); } } catch (Exception e) { e.printStackTrace(); } } }); dialog.setNegativeButton(string_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface d, int which) { } }); dialog.show(); } }