2013-01-09 61 views
3

在我的應用程序中,我使用的是Google地圖,首先我通過標記顯示用戶當前位置,並通過透明圓圈顯示其準確性,現在當我想將另一個標記添加到同一點時這代表我以前的位置相同的代碼不適合我,有人請幫我弄清楚代碼有什麼問題,我只想在單個地圖視圖中添加多個標記無法在Google地圖上添加多個標記

這裏是我的代碼標記: -

LocationResult locationResult = new LocationResult(){ 
     @Override 
     public void gotLocation(Location location){ 
      //Got the location! 
      if (location != null) { 
       GeoPoint point = new GeoPoint(
         (int) (location.getLatitude() * 1E6), 
         (int) (location.getLongitude() * 1E6)); 

       accuracy = location.getAccuracy(); 
       cur_lati = location.getLatitude(); 
       cur_longi = location.getLongitude(); 

       String address = ""; 
       Geocoder geoCoder = new Geocoder(
         getBaseContext(), Locale.getDefault()); 
       try { 
        List<Address> addresses = geoCoder.getFromLocation(
          point.getLatitudeE6()/1E6, 
          point.getLongitudeE6()/1E6, 1); 

        if (addresses.size() > 0) { 
         for (int index = 0; 
           index < addresses.get(0).getMaxAddressLineIndex(); index++) 
          address += addresses.get(0).getAddressLine(index) + " "; 
        } 
       }catch (IOException e) {   
        e.printStackTrace(); 
       } 


       Input.setPinPoints(cur_lati,cur_longi,address);     // sending lat,lon to input class 

       Toast.makeText(getBaseContext(),"Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude() + "accuracy" + location.getAccuracy(),Toast.LENGTH_SHORT).show(); 
       mc = mapView.getController(); 
       mc.animateTo(point); 
       mc.setZoom(19); 


       MapOverlay mapOverlay = new MapOverlay(); 
       mapOverlay.setPointToDraw(point); 
       List<Overlay> listOfOverlays = mapView.getOverlays(); 
       listOfOverlays.clear(); 
       listOfOverlays.add(mapOverlay); 

       Bundle bundle=getIntent().getExtras(); 

       if(bundle!=null){ 

        int value = bundle.getInt("tab_value"); 
        lati = bundle.getDouble("lati"); 
        longi = bundle.getDouble("longi"); 



        //GeoPoint point1 = new GeoPoint((int) lati, (int) longi); 

        if(value == 1){ 

         MapOverlay1 mapOverlay1 = new MapOverlay1(); 
         //  mapOverlay1.setPointToDraw(point1); 
         List<Overlay> listOfOverlays1 = mapView.getOverlays(); 
         listOfOverlays1.add(mapOverlay1); 


        } 

       } 

      } 
     } 
    }; 
    MyLocation myLocation = new MyLocation(); 
    myLocation.getLocation(this, locationResult); 


    btnimg.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Intent intent=new Intent(getApplicationContext(),Input.class); 
      startActivity(intent); 

     } 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_safe_city, menu); 
    return true; 
} 

@Override 
protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
} 

@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    super.onBackPressed(); 

    finish(); 
} 

@Override 
public void finish() { 
    // TODO Auto-generated method stub 
    super.finish(); 

} 


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(); 
     Paint mPaintBorder,mPaintFill ; 
     mapView.getProjection().toPixels(pointToDraw, screenPts); 

     // add marker 
     Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pinimage); 
     canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); 

     mPaintBorder = new Paint(); 
     mPaintBorder.setStyle(Paint.Style.STROKE); 
     mPaintBorder.setAntiAlias(true); 
     mPaintBorder.setColor(0xee4D2EFF); 
     mPaintFill = new Paint(); 
     mPaintFill.setStyle(Paint.Style.FILL); 
     mPaintFill.setColor(0x154D2EFF); 

     int radius = (int) mapView.getProjection().metersToEquatorPixels(accuracy); 

     canvas.drawCircle(screenPts.x, screenPts.y, radius, mPaintBorder); 

     canvas.drawCircle(screenPts.x, screenPts.y, radius, mPaintFill); 

     return true; 
    } 
} 

class MapOverlay1 extends com.google.android.maps.Overlay 
{ 

    private GeoPoint p1 = new GeoPoint((int)(lati),(int)(longi)); 

    Projection projection; 



    @Override 
    public boolean draw(Canvas canvas, MapView mapView, 
      boolean shadow, long when) 
    { 

     super.draw(canvas, mapView, shadow); 


     Point screenPts1 = new Point(); 
     mapView.getProjection().toPixels(p1, screenPts1); 
     //---add the marker--- 
     Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.car); 
     canvas.drawBitmap(bmp1, screenPts1.x, screenPts1.y-50, null); 

     return true; 

    } 


} 

通過使用MapOverlay類,我可以在地圖上放下一個別針,顯示用戶的當前位置,但是當我使用MapOverlay1以顯示其不工作的第二個標誌....

任何幫助將很可觀 在此先感謝......

回答

2

切換到新谷歌地圖的API。使用起來會更好。 如果您必須使用舊API,請使用MyLocationOverlay作爲當前位置。 另外,如果我記得正確的話,有一個set/getMarker方法夫婦可以節省您自己在畫布上繪製標記的麻煩。

相關問題