2011-05-13 55 views
0

這是我的代碼!我想在我目前的 位置添加覆蓋項目!但似乎是一個問題,我不能任何人檢查我有什麼 添加到我的代碼,只是把覆蓋項目出現在我的當前位置請檢查它。如何在此代碼中添加疊加項目在我當前的位置

package tryanabtry.opa; 
import java.util.List; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.graphics.drawable.Drawable; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.Handler; 
import android.util.Log; 
import android.widget.TextView; 
import android.widget.Toast; 
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; 
import com.google.android.maps.OverlayItem; 
public class tryanabtry extends MapActivity 
{ 
    private MapView mapView; 
    private MapController mc; 
    int error=100; 
    GeoPoint p, p2, p3, p4,p5,p6,p7,p8,p9,p10,p11,p12,p13; 
    List<Overlay> mapOverlays; 
    Drawable drawable, drawable2 , drawable3, 
    drawable4,drawable6,drawable7,drawable8,drawable9,drawable10,drawable11,drawable12,drawable13,drawable5; 
    HelloItemizedOverlay itemizedOverlay, itemizedOverlay2 , 
     itemizedOverlay3, 
     itemizedOverlay4,itemizedOverlay5,itemizedOverlay6,itemizedOverlay7,itemizedOverlay8,itemizedOverlay9,itemizedOverlay10,itemizedOverlay11,itemizedOver lay12,itemizedOverlay13; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     try{ 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      /* Use the LocationManager class to obtain GPS locations */ 
      LocationManager mlocManager = 
       (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
      LocationListener mlocListener = new MyLocationListener(); 
      mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
       0, mlocListener); 
      mapView = (MapView) findViewById(R.id.mapView); 

      mapView.setStreetView(true); 
      mapView.setSatellite(true); 
      mapView.setBuiltInZoomControls(true); 
      mc = mapView.getController(); 

      mc.setZoom(12); 
      addOverLays(); 
     } 
     catch(Exception e){ 
      Log.d("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",e.getMessage()); 
     } 
    } 

    public void addOverLays(){ 
     String [] coordinates = 
      {"31.216487288475037","29.932637214660645" ,"30.084123015403748", 
      "51.5002" , "-0.1262","31.337149143218994"}; 
     double lat = 29.98739718380868,lat2 
      =29.98763859272003,lat3=29.987574219703674,lat4=29.98718498160553; 
     double log = 31.442527770886084, log2 = 
      31.44235074520111,log3=31.44225418567575,log4=31.442527770996094; 
     p = new GeoPoint((int) (lat * 1E6), (int) (log * 1E6)); 
     p2 = new GeoPoint((int) (lat2 * 1e6), (int) (log2 * 1e6)); 
     p3=new GeoPoint((int) (lat3 * 1e6), (int) (log3 * 1e6)); 
     p4=new GeoPoint((int) (lat4 * 1e6), (int) (log4 * 1e6)); 

     mapOverlays = mapView.getOverlays(); 
     drawable = this.getResources().getDrawable(R.drawable.vitblue); 
     drawable2 = this.getResources().getDrawable(R.drawable.ballon); 
     drawable3 = this.getResources().getDrawable(R.drawable.blackaaaaa); 
     drawable4 = this.getResources().getDrawable(R.drawable.vitblue); 
     itemizedOverlay = new HelloItemizedOverlay(drawable,this); 
     itemizedOverlay2 = new HelloItemizedOverlay(drawable2,this); 
     itemizedOverlay3 = new HelloItemizedOverlay(drawable3,this); 
     itemizedOverlay4 = new HelloItemizedOverlay(drawable4,this); 
     OverlayItem overlayitem = new OverlayItem(p, "Cairo", " over1"); 
     OverlayItem over2 = new OverlayItem(p2, "ulm", "over2"); 
     OverlayItem over3 = new OverlayItem(p3, "offff", "over3"); 
     OverlayItem over4 = new OverlayItem(p4, "offff", "over4"); 
     itemizedOverlay.addOverlay(overlayitem); 
     mapOverlays.add(itemizedOverlay); 
     itemizedOverlay2.addOverlay(over2); 
     mapOverlays.add(itemizedOverlay2); 
     itemizedOverlay3.addOverlay(over3); 
     mapOverlays.add(itemizedOverlay3); 
     itemizedOverlay4.addOverlay(over4); 
     mapOverlays.add(itemizedOverlay4); 
     mc.setZoom(17); 
    } 

    public class MyLocationListener implements LocationListener 
    { 
     @Override 
     public void onLocationChanged(Location loc) 
     { 
      GeoPoint point = new GeoPoint( (int) (loc.getLatitude() * 
       1E6), 
       (int) (loc.getLongitude() * 1E6)); 
///////////////////////////////////MY NEW EDIT \\\\\\\\\\\\\ 
      mapOverlays = mapView.getOverlays(); 
      drawable11 = this.getResources().getDrawable(R.drawable.vitblue); 
      itemizedOverlay11= new HelloItemizedOverlay(drawable11,this); 
      OverlayItem over11 = new OverlayItem(point, "offff", "over5"); 
      itemizedOverlay11.addOverlay(over11); 
      mapOverlays.add(itemizedOverlay11); 
/////////////////////////////////////////////////////////////////////// /////////The constructor HelloItemizedOverlay(Drawable, tryanabtry.MyLocationListener) is undefined ////// error 
      String Text = "My current location is: " + 
       "Latitud ="+ loc.getLatitude() + 
       "Longitud =" + loc.getLongitude(); 
      Toast.makeText(getApplicationContext(), 
      Text, 
       Toast.LENGTH_SHORT).show(); 
      mc.animateTo(point); 
     } 

     private Resources getResources() { 
      // TODO Auto-generated method stub 
      return null; 
     } 

     private void DoubletoString(double latitude) { 
      // TODO Auto-generated method stub 
     } 

     public void onProviderDisabled(String provider) 
     { 
      Toast.makeText(getApplicationContext(), 
       "Gps Disabled", 
      Toast.LENGTH_SHORT).show(); 
     } 

     public void onProviderEnabled(String provider) 
     { 
      Toast.makeText(getApplicationContext(), 
       "Gps Enabled", 
      Toast.LENGTH_SHORT).show(); 
     } 

     public void onStatusChanged(String provider, int status, Bundle 
       extras) 
     { 
     } 

     protected boolean isRouteDisplayed() { 
      return false; 
     } 
    }/* End of Class MyLocationListener */ 

    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    } 
} 
/* End of UseGps Activity*/ 

我HelloOverlay班組長

package tryanabtry.opa; 
import java.util.ArrayList; 
import tryanabtry.opa.tryanabtry.MyLocationListener; 
import android.app.AlertDialog; 
import android.app.AlertDialog.Builder; 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.graphics.Point; 
import android.graphics.drawable.Drawable; 
import android.util.Log; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.ItemizedOverlay; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.OverlayItem; 
import com.google.android.maps.Projection; 
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> 
{ 
    private ArrayList<OverlayItem> mOverlays = new 
    ArrayList<OverlayItem>(); 
    tryanabtry m = new tryanabtry(); 
    public HelloItemizedOverlay(Drawable defaultMarker, tryanabtry n) { 
     super(boundCenterBottom(defaultMarker)); 
     m = n; 
    } 
    @Override 
    protected OverlayItem createItem(int i) { 
     return mOverlays.get(i); 
    } 
    @Override 
    public int size() { 
     return mOverlays.size(); 
    } 
    public void addOverlay(OverlayItem overlay) { 
     mOverlays.add(overlay); 
     populate(); 
    } 
    // ////////// 
    public void draw(android.graphics.Canvas canvas, MapView mapView, 
      boolean shadow) { 
     class MapOverlay extends Overlay { 
      private GeoPoint pointToDraw; 
      public void setPointToDraw(GeoPoint point) { 
       pointToDraw = point; 
      } 
      public GeoPoint getPointToDraw() { 
       return pointToDraw; 
      } 
      @Override 
      public boolean draw(Canvas canvas, MapView mapView, boolean shadow, 
        long when) { 
       super.draw(canvas, mapView, shadow); 
       // convert point to pixels 
       Point screenPts = new Point(); 
       mapView.getProjection().toPixels(pointToDraw, screenPts); 
       // add marker 
       Bitmap bmp = BitmapFactory.decodeResource(getResources(), 
        R.drawable.ballon); 
       canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24,  null) 
       return true; 
      } 
      private Resources getResources() { 
       // TODO Auto-generated method stub 
       return null; 
      } 
     } 
     GeoPoint p = new GeoPoint((int) (29.987574219703674 * 1E6), 
      (int) (31.44225418567575 * 1E6)); 
     GeoPoint p2 = new GeoPoint((int) (29.98763859272003 * 1E6), 
      (int) (31.44235074520111 * 1E6)); 
     GeoPoint p3 = new GeoPoint((int) (29.98718498160553* 1E6), 
      (int) (31.442527770996094 * 1E6)); 
     GeoPoint p4= new GeoPoint((int) (29.98739718380868 * 1E6), 
      (int) (31.442527770886084 * 1E6)); 
     // Let's assume you've assigned values to these two GeoPoints now. 
     Projection projection = mapView.getProjection(); 
     Point startingPoint = projection.toPixels(p, null); 
     Point startingPoint1 = projection.toPixels(p, null); 
     Point startingPoint2= projection.toPixels(p2, null); 
     Point endingPoint = projection.toPixels(p2, null); 
     Point endingPoint1 = projection.toPixels(p3, null); 
     Point endingPoint2= projection.toPixels(p4, null); 
     // Create the path containing the line between the two points. 
     Path path = new Path(); 
     path.moveTo(startingPoint.x, startingPoint.y); 
     path.lineTo(endingPoint.x, endingPoint.y); 
     Path path1 = new Path(); 
     path1.moveTo(startingPoint1.x, startingPoint1.y); 
     path1.lineTo(endingPoint1.x, endingPoint1.y); 
     Path path2 = new Path(); 
     path2.moveTo(startingPoint2.x, startingPoint2.y); 
     path2.lineTo(endingPoint2.x, endingPoint2.y); 

     Paint paint = new Paint(); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setColor(Color.RED); 
     Paint paint1 = new Paint(); 
     paint1.setStyle(Paint.Style.STROKE); 
     paint1.setColor(Color.GREEN); 
     Paint paint2 = new Paint(); 
     paint2.setStyle(Paint.Style.STROKE); 
     paint2.setColor(Color.YELLOW); 
     // Draw the path! 
     super.draw(canvas, mapView, shadow); 
     // el super de 3shnnn to addd koloooo overlay w path 
     canvas.drawPath(path, paint); 
     canvas.drawPath(path1, paint1); 
     canvas.drawPath(path2, paint2); 
    } 
    @Override 
    public boolean onTap(int i) { 
     try { 
      String s = mOverlays.get(0).getSnippet(); 
      String[] infos = s.split(","); 
      Builder builder = new AlertDialog.Builder(m); 
      builder.setTitle("Information about: " + infos[0]); 
      builder.setIcon(m.getResources().getDrawable( 
       m.getResources().getIdentifier(mOverlays.get(0).getTitle(), 
       "drawable", "net.learn2develop.GoogleMaps"))); 
      builder.setMessage("age: " + infos[1] + "\njob: " + infos[2] 
       + "\ninstitute: " + infos[3]); 
      builder.setPositiveButton("ok", null); 
      builder.show(); 
     } 
     catch (Exception e) { 
      Log.d("3'lt", "lihh ya wdi3", e); 
     } 
     return true; 
    } 
+0

任何幫助,請 – wowo 2011-05-13 11:12:31

回答

0

就在添加這段代碼你onLocationChanged

@Override 
public void onLocationChanged(Location l) { 
    // TODO Auto-generated method stub 
    overlayList = mapView.getOverlays(); 
    MyLocationOverlay object = new MyLocationOverlay(yourclass.this, mapView); 
    overlayList.add(myLocation); 
    myLocation.enableMyLocation(); 
} 
0

,我遲到了,但原因你所得到的錯誤:

The constructor HelloItemizedOverlay(Drawable, tryanabtry.MyLocationListener) is undefined

是因爲你的使用this當調用構造函數HelloItemizedOverlay時。在這種情況下,this是指代碼在內的MyLocationListener的實例,而不是類tryanabtry的實例。

替換此行:

itemizedOverlay11= new HelloItemizedOverlay(drawable11,this); 

與此:

itemizedOverlay11= new HelloItemizedOverlay(drawable11,tryanabtry.this); 

也請注意,按照慣例Java類應該以大寫字母開頭,所以Tryanabtry而不是tryanabtry

相關問題