code_hacker

Untitled

Nov 3rd, 2011
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.25 KB | None | 0 0
  1. package com.example.gui;
  2.  
  3.  
  4. import java.io.IOException;
  5. import java.net.HttpURLConnection;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import javax.xml.parsers.DocumentBuilder;
  12. import javax.xml.parsers.DocumentBuilderFactory;
  13. import javax.xml.parsers.ParserConfigurationException;
  14.  
  15. import org.w3c.dom.Document;
  16. import org.xml.sax.SAXException;
  17.  
  18. import android.app.Activity;
  19. import android.content.Context;
  20. import android.content.Intent;
  21. import android.graphics.Bitmap;
  22. import android.graphics.BitmapFactory;
  23. import android.graphics.Canvas;
  24. import android.graphics.Color;
  25. import android.graphics.Paint;
  26. import android.graphics.Path;
  27. import android.graphics.Point;
  28. import android.graphics.RectF;
  29. import android.graphics.drawable.Drawable;
  30. import android.location.Address;
  31. import android.location.Geocoder;
  32. import android.location.Location;
  33. import android.location.LocationListener;
  34. import android.location.LocationManager;
  35. import android.net.Proxy;
  36. import android.os.Bundle;
  37. import android.util.Log;
  38. import android.view.View;
  39. import android.widget.Button;
  40. import android.widget.TextView;
  41. import android.widget.Toast;
  42. import android.content.SharedPreferences;
  43.  
  44. import com.example.gui.LocationActivity.MapOverlay;
  45. import com.google.android.maps.GeoPoint;
  46. import com.google.android.maps.ItemizedOverlay;
  47. import com.google.android.maps.MapActivity;
  48. import com.google.android.maps.MapController;
  49. import com.google.android.maps.MapView;
  50. import com.google.android.maps.Overlay;
  51. import com.google.android.maps.OverlayItem;
  52. import com.google.android.maps.Projection;
  53.  
  54.  
  55. public class ShowMapActivity extends MapActivity implements LocationListener {
  56.     /** Called when the activity is first created. */
  57.    
  58.     LocationManager locationManager;
  59.       Geocoder geocoder;
  60.       TextView locationText;
  61.       MapView map;  
  62.       MapController mapController;
  63.       GeoPoint point1,point2;
  64.       String autoReplyToken = "Request_Accepted";
  65.       private List<Overlay> mapOverlays;
  66.       public static final String PREFS_NAME = "Frndloc_file";
  67.       private static final String TAG="checking";
  68.      
  69.      
  70.      
  71.       int latitude1,latitude2,longitude1,longitude2;
  72.      
  73.     @Override
  74.     public void onCreate(Bundle savedInstanceState) {
  75.         super.onCreate(savedInstanceState);
  76.         setContentView(R.layout.main8);
  77.    
  78.         map = (MapView)this.findViewById(R.id.mapview);
  79.         map.setBuiltInZoomControls(true);
  80.        
  81.         mapController = map.getController();
  82.         mapController.setZoom(8);
  83.        
  84.         SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  85.         String defValue="def_value";
  86.        
  87.         String latlon = settings.getString("friendlocation", defValue);
  88.        
  89.         boolean check=latlon.startsWith(defValue);
  90.            
  91.  
  92.         locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
  93.        
  94.         geocoder = new Geocoder(this);
  95.  
  96.         Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  97.  
  98.         latitude1 = (int)(location.getLatitude() * 1000000);
  99.         longitude1 = (int)(location.getLongitude() * 1000000);
  100.  
  101.         point1 = new GeoPoint(latitude1,longitude1);
  102.        
  103.    
  104.         friendlocation(latlon);
  105.  
  106.         mapOverlays = map.getOverlays();
  107.         Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
  108.         Markers itemizedoverlay = new Markers(drawable,this);
  109.    
  110.         OverlayItem overlayitem = new OverlayItem(point1, "Hi!", "Second!");
  111.  
  112.    
  113.         OverlayItem overlayitem2 = new OverlayItem(point2, "Hello!", " fisrt one!");
  114.  
  115.         itemizedoverlay.addOverlay(overlayitem);
  116.         itemizedoverlay.addOverlay(overlayitem2);
  117.  
  118.         mapOverlays.add(itemizedoverlay);
  119.         /*}
  120.         else{
  121.            
  122.             Toast.makeText(getBaseContext(),
  123.                     "You don't have friend location",
  124.                     Toast.LENGTH_SHORT).show();
  125.            
  126.         }*/
  127.        
  128.         DrawPath(point1, point2, Color.GREEN, map);
  129.        
  130.     }
  131.  
  132.  
  133.       public class Markers extends ItemizedOverlay {
  134.  
  135.           private Context ctx;
  136.  
  137.           private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
  138.  
  139.           public Markers(Drawable defaultMarker, Context cont) {
  140.  
  141.                super(boundCenterBottom(defaultMarker));
  142.                this.ctx = cont;
  143.                // TODO Auto-generated constructor stub
  144.           }
  145.  
  146.           @Override
  147.           protected OverlayItem createItem(int i) {
  148.                // TODO Auto-generated method stub
  149.                return mOverlays.get(i);
  150.           }
  151.  
  152.           @Override
  153.           public boolean onTap(GeoPoint p, MapView mapView) {
  154.                // TODO Auto-generated method stub
  155.                return super.onTap(p, mapView);
  156.           }
  157.  
  158.  
  159.           @Override
  160.           protected boolean onTap(int index) {
  161.                // TODO Auto-generated method stub
  162.                Toast.makeText(this.ctx, mOverlays.get(index).getTitle().toString()+", Latitude: "+mOverlays.get(index).getPoint().getLatitudeE6(), Toast.LENGTH_SHORT).show();
  163.                return super.onTap(index);        
  164.           }
  165.  
  166.           @Override
  167.           public int size() {
  168.                // TODO Auto-generated method stub
  169.                return mOverlays.size();
  170.           }
  171.  
  172.           public void addOverlay(OverlayItem item) {
  173.                mOverlays.add(item);
  174.                setLastFocusedIndex(-1);
  175.                populate();
  176.  
  177.           }
  178.  
  179.           public void clear() {
  180.                mOverlays.clear();
  181.                setLastFocusedIndex(-1);
  182.                populate();
  183.           }
  184.          }
  185.      
  186.     public void friendlocation(String latlon) { //<9>
  187.  
  188.         boolean check=latlon.startsWith(autoReplyToken);
  189.         int idx1,idx2;
  190.         if(!check){
  191.             idx1=0;
  192.             idx2=1;
  193.         }
  194.         else{
  195.             idx1=1;
  196.             idx2=2;
  197.         }
  198.        
  199.         String [] location = latlon.split("\\s+");
  200.  
  201.         double lat= Double.valueOf(location[idx1].trim()).doubleValue();
  202.         double lon=Double.valueOf(location[idx2].trim()).doubleValue();
  203.        
  204.        
  205.         latitude2 = (int)(lat * 1000000);
  206.         longitude2 = (int)(lon * 1000000);
  207.  
  208.         point2 = new GeoPoint(latitude2,longitude2);
  209.                
  210.     }
  211.    
  212.     @Override
  213.     protected boolean isRouteDisplayed() {
  214.         // TODO Auto-generated method stub
  215.         return false;
  216.     }
  217.  
  218.     @Override
  219.     public void onProviderDisabled(String arg0) {
  220.         // TODO Auto-generated method stub
  221.        
  222.     }
  223.  
  224.     @Override
  225.     public void onProviderEnabled(String provider) {
  226.         // TODO Auto-generated method stub
  227.        
  228.     }
  229.  
  230.     @Override
  231.     public void onStatusChanged(String provider, int status, Bundle extras) {
  232.         // TODO Auto-generated method stub
  233.        
  234.     }
  235.    
  236.        @Override
  237.         protected void onResume() {
  238.           super.onResume();
  239.           locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7>
  240.         }
  241.  
  242.         @Override
  243.         protected void onPause() {
  244.           super.onPause();
  245.           locationManager.removeUpdates(this); //<8>
  246.         }
  247.  
  248.         @Override
  249.         public void onLocationChanged(Location arg0) {
  250.             // TODO Auto-generated method stub
  251.            
  252.         }
  253.        
  254.         private void DrawPath(GeoPoint src,GeoPoint dest, int color, MapView mMapView01)
  255.         {
  256.         // connect to map web service
  257.         StringBuilder urlString = new StringBuilder();
  258.         urlString.append("http://maps.google.com/maps?f=d&hl=en");
  259.         urlString.append("&saddr=");//from
  260.         urlString.append( Double.toString((double)src.getLatitudeE6()/1.0E6 ));
  261.         urlString.append(",");
  262.         urlString.append( Double.toString((double)src.getLongitudeE6()/1.0E6 ));
  263.         urlString.append("&daddr=");//to
  264.         urlString.append( Double.toString((double)dest.getLatitudeE6()/1.0E6 ));
  265.         urlString.append(",");
  266.         urlString.append( Double.toString((double)dest.getLongitudeE6()/1.0E6 ));
  267.         urlString.append("&ie=UTF8&0&om=0&output=kml");
  268.         Log.d("xxx","URL="+urlString.toString());
  269.         // get the kml (XML) doc. And parse it to get the coordinates(direction route).
  270.         Document doc = null;
  271.         HttpURLConnection urlConnection= null;
  272.         URL url = null;
  273.         try
  274.         {
  275.         url = new URL(urlString.toString());
  276.         System.setProperty("http.proxyHost", "my_proxy");
  277.         System.setProperty("http.proxyPort", "my_port");
  278.         System.setProperty("http.proxyUser", "my_username");
  279.         System.setProperty("http.proxyPassword", "my_password");
  280.  
  281.         urlConnection=(HttpURLConnection)url.openConnection();
  282.         urlConnection.setRequestMethod("GET");
  283.         urlConnection.setDoOutput(true);
  284.         urlConnection.setDoInput(true);
  285.         urlConnection.connect();
  286.  
  287.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  288.         DocumentBuilder db = dbf.newDocumentBuilder();
  289.         doc = db.parse(urlConnection.getInputStream());
  290.  
  291.         if(doc.getElementsByTagName("GeometryCollection").getLength()>0)
  292.         {
  293.         //String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName();
  294.         String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue() ;
  295.         Log.d("xxx","mukul");
  296.         Log.d("xxx","path="+ path);
  297.         String [] pairs = path.split(" ");
  298.         String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude lngLat[1]=latitude lngLat[2]=height
  299.         // src
  300.         GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
  301.         mMapView01.getOverlays().add(new MyOverLay(startGP,startGP,1));
  302.         GeoPoint gp1;
  303.         GeoPoint gp2 = startGP;
  304.         for(int i=1;i<pairs.length;i++) // the last one would be crash
  305.         {
  306.         lngLat = pairs[i].split(",");
  307.         gp1 = gp2;
  308.         // watch out! For GeoPoint, first:latitude, second:longitude
  309.         gp2 = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
  310.         mMapView01.getOverlays().add(new MyOverLay(gp1,gp2,2,color));
  311.         Log.d("xxx","pair:" + pairs[i]);
  312.         }
  313.         mMapView01.getOverlays().add(new MyOverLay(dest,dest, 3)); // use the default color
  314.         }
  315.         }
  316.         catch (MalformedURLException e)
  317.         {
  318.         e.printStackTrace();
  319.         }
  320.         catch (IOException e)
  321.         {
  322.         e.printStackTrace();
  323.         }
  324.         catch (ParserConfigurationException e)
  325.         {
  326.         e.printStackTrace();
  327.         }
  328.         catch (SAXException e)
  329.         {
  330.         e.printStackTrace();
  331.         }
  332.         }
  333.  
  334.    
  335.         public class MyOverLay extends Overlay
  336.         {
  337.         private GeoPoint gp1;
  338.         private GeoPoint gp2;
  339.         private int mRadius=6;
  340.         private int mode=0;
  341.         private int defaultColor;
  342.         private String text="";
  343.         private Bitmap img = null;
  344.  
  345.         public MyOverLay(GeoPoint gp1,GeoPoint gp2,int mode) // GeoPoint is a int. (6E)
  346.         {
  347.         this.gp1 = gp1;
  348.         this.gp2 = gp2;
  349.         this.mode = mode;
  350.         defaultColor = 999; // no defaultColor
  351.  
  352.         }
  353.  
  354.         public MyOverLay(GeoPoint gp1,GeoPoint gp2,int mode, int defaultColor)
  355.         {
  356.         this.gp1 = gp1;
  357.         this.gp2 = gp2;
  358.         this.mode = mode;
  359.         this.defaultColor = defaultColor;
  360.         }
  361.         public void setText(String t)
  362.         {
  363.         this.text = t;
  364.         }
  365.         public void setBitmap(Bitmap bitmap)
  366.         {
  367.         this.img = bitmap;
  368.         }
  369.         public int getMode()
  370.         {
  371.         return mode;
  372.         }
  373.  
  374.         @Override
  375.         public boolean draw
  376.         (Canvas canvas, MapView mapView, boolean shadow, long when)
  377.         {
  378.         Projection projection = mapView.getProjection();
  379.         if (shadow == false)
  380.         {
  381.         Paint paint = new Paint();
  382.         paint.setAntiAlias(true);
  383.         Point point = new Point();
  384.         projection.toPixels(gp1, point);
  385.         // mode=1&#65306;start
  386.         if(mode==1)
  387.         {
  388.         if(defaultColor==999)
  389.         paint.setColor(Color.BLUE);
  390.         else
  391.         paint.setColor(defaultColor);
  392.         RectF oval=new RectF(point.x - mRadius, point.y - mRadius,
  393.         point.x + mRadius, point.y + mRadius);
  394.         // start point
  395.         canvas.drawOval(oval, paint);
  396.         }
  397.         // mode=2&#65306;path
  398.         else if(mode==2)
  399.         {
  400.         if(defaultColor==999)
  401.         paint.setColor(Color.RED);
  402.         else
  403.         paint.setColor(defaultColor);
  404.         Point point2 = new Point();
  405.         projection.toPixels(gp2, point2);
  406.         paint.setStrokeWidth(5);
  407.         paint.setAlpha(120);
  408.         canvas.drawLine(point.x, point.y, point2.x,point2.y, paint);
  409.         }
  410.         /* mode=3&#65306;end */
  411.         else if(mode==3)
  412.         {
  413.         /* the last path */
  414.  
  415.         if(defaultColor==999)
  416.         paint.setColor(Color.GREEN);
  417.         else
  418.         paint.setColor(defaultColor);
  419.         Point point2 = new Point();
  420.         projection.toPixels(gp2, point2);
  421.         paint.setStrokeWidth(5);
  422.         paint.setAlpha(120);
  423.         canvas.drawLine(point.x, point.y, point2.x,point2.y, paint);
  424.         RectF oval=new RectF(point2.x - mRadius,point2.y - mRadius,
  425.         point2.x + mRadius,point2.y + mRadius);
  426.         /* end point */
  427.         paint.setAlpha(255);
  428.         canvas.drawOval(oval, paint);
  429.         }
  430.         }
  431.         return super.draw(canvas, mapView, shadow, when);
  432.         }
  433.  
  434.         }
  435.  
  436.    
  437. }
  438.  
  439.  
Advertisement
Add Comment
Please, Sign In to add comment