在尝试在地图上显示图钉时,在draw()上获得无限循环

时间:2011-07-15 12:54:29

标签: android

我正在尝试在地图上显示图钉并尝试使用以下代码 而且我没有在地图上获得引脚,而是绘制函数的无限循环。 任何人都可以帮助我。

  public class MyMapActivity extends MapActivity 
 {
private MapView mapview; 
MapController mc;
GeoPoint p;
public String strCountry,strAdressLine,strSubAdminArea,strCity,strFeature,strZipcode;
public Vector<UserLocation> userSavedLoaction;

public void garLoacationAddress( double LATITUDE, double LONGITUDE)
{
       String lati=String.valueOf(LATITUDE);
       String longti=String.valueOf(LONGITUDE);
       Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);

       try {
              List<Address> addresses = geocoder.getFromLocation(LATITUDE,LONGITUDE, 1);
              userSavedLoaction=new Vector<UserLocation>();
              UserLocation objUserLocation=new UserLocation();
              if (addresses.size()> 0){
                objUserLocation.Country = addresses.get(0).getCountryName();
                objUserLocation.Address=addresses.get(0).getAddressLine(0);
                objUserLocation.City=addresses.get(0).getLocality();
                objUserLocation.Zip= addresses.get(0).getPostalCode();
                objUserLocation.State=addresses.get(0).getAdminArea();
                objUserLocation.GeoCode=""+LATITUDE+","+LONGITUDE;
                objUserLocation.Is_Primary=false;
                userSavedLoaction.add(objUserLocation);
                Log.i("Location",userSavedLoaction.get(0).Country+userSavedLoaction.get(0).City+userSavedLoaction.get(0).Address+userSavedLoaction.get(0).GeoCode+userSavedLoaction.get(0).State+userSavedLoaction.get(0).Zip);
              }

              else{
                  Log.i("","no adress returned");
                  }
        } 
       catch (IOException e) 
       {
  // TODO Auto-generated catch block
                 e.printStackTrace();

          }             
}

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);
    mapview=(MapView)findViewById(R.id.mapView);
    mapview.setSatellite(true);


    mapview.setBuiltInZoomControls(true);
    mc= mapview.getController();

    mc.setZoom(10);

       final MapOverlay mapOverlay = new MapOverlay();           
       List<Overlay> listOfOverlays = mapview.getOverlays();
       listOfOverlays.clear();
       listOfOverlays.add(mapOverlay);        
       mapview.invalidate();


       TabsActivity.btnLogout.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {


                   Log.i("long"+mapOverlay.LONGITUDE, "lati"+mapOverlay.LATITUDE);
                   garLoacationAddress(mapOverlay.LATITUDE, mapOverlay.LONGITUDE);
                   Ga2ooParsers objGa2ooParsers=new Ga2ooParsers();
                   objGa2ooParsers.addUserSavedLocation(userSavedLoaction);

            }
        });
}
       @Override
       protected boolean isRouteDisplayed()
       {
           return false;
       }
       @Override
       public void onResume()
       {
        super.onResume();
        Log.e("MyMapActivity","onResume");

           TabsActivity.btnBack.setVisibility(View.VISIBLE);
                           TabsActivity.rlCommonTitleBar.setBackgroundResource(R.drawable.top_bar_empty);
           TabsActivity.tvTitle.setText("Map");
           TabsActivity.tvTitle.setVisibility(View.VISIBLE);
           TabsActivity.btnLogout.setBackgroundResource(R.drawable.save);
           TabsActivity.rlCommonTitleBar.invalidate();

           TabsActivity.btnBack.setOnClickListener(new OnClickListener()
           {
            @Override
            public void onClick(View v) 
            {
                MyMapActivity.this.finishFromChild(MyMapActivity.this);
            }
        });


       }

       class MapOverlay extends Overlay
       {

        double LATITUDE;
        double LONGITUDE;
        Geocoder geoCoder = null;



         @Override
            public boolean onTouchEvent(MotionEvent event, MapView mapView) 
            {   
                //---when user lifts his finger---


                if (event.getAction() == 1) {                
                p = mapView.getProjection().fromPixels(
                        (int) event.getX(),
                        (int) event.getY());

                   LATITUDE =   p.getLatitudeE6()/ 1E6;
                   LONGITUDE=   p.getLongitudeE6()/ 1E6;                   
                   Log.i("long"+LONGITUDE, "lati"+LATITUDE);  


        } 
                return false;
            }

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

             super.draw(canvas, mapView, shadow);                   
             Geocoder geoCoder = new Geocoder(getApplicationContext(), Locale.getDefault());    
              try {
                  List<Address> addresses = geoCoder.getFromLocationName(
                      "India", 5);

                  if (addresses.size() > 0) {
                      p = new GeoPoint(
                              (int) (addresses.get(0).getLatitude() * 1E6), 
                              (int) (addresses.get(0).getLongitude() * 1E6));
                      Log.i("this is p inside the draw", ""+p);
                  }


                  Point screenPts = new Point();
                  mapView.getProjection().toPixels(p, screenPts);

                  //---add the marker---
                  Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.red);            
                  canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);  
                  mc.animateTo(p); 
              }
              catch (IOException e) {
                     e.printStackTrace();
                 }
                 return true;

         }
       }

}

1 个答案:

答案 0 :(得分:0)

这是一篇关于Implementing the onTouchEvent for the MapActivity的好文章,看起来可能会有所帮助。

相关问题