如何绘制超过1个地理位置?

时间:2012-11-14 12:00:46

标签: android geolocation

如何绘制超过1个地理位置?我有一个代码,用于绘制1个位置。我修改了这段代码并添加了更多输入。

但是如何修改地理位置代码?实际代码在下面,最后一个代码是我修改过的。我怎么能在这个函数中添加更多参数?

实际代码 -

GeoPoint geoPoint = new GeoPoint((int)(lat1 * 1E6), (int)(lon1 * 1E6));

    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 AndroidGoogleMapsActivity extends MapActivity {
     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Displaying Zooming controls
        MapView mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);

        /**
         * Changing Map Type
         * */
        // mapView.setSatellite(true); // Satellite View
        // mapView.setStreetView(true); // Street View
        // mapView.setTraffic(true); // Traffic view

        /**
         * showing location by Latitude and Longitude
         * */        
        MapController mc = mapView.getController();
        double lat = Double.parseDouble("48.85827758964043");
        double lon = Double.parseDouble("2.294543981552124");
        GeoPoint geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
        mc.animateTo(geoPoint);
        mc.setZoom(15);
        mapView.invalidate(); 


        /**
         * Placing Marker
         * */
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red);
        AddItemizedOverlay itemizedOverlay = 
             new AddItemizedOverlay(drawable, this);


        OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello", "Sample Overlay    
    item");

        itemizedOverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedOverlay);

    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    }




  import java.util.ArrayList;

 import android.content.Context;
 import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

 public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {

   private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

   private Context context;

   public AddItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
   }

   public AddItemizedOverlay(Drawable defaultMarker, Context context) {
        this(defaultMarker);
        this.context = context;
   }

   @Override
   protected OverlayItem createItem(int i) {
      return mapOverlays.get(i);
   }

   @Override
   public int size() {
      return mapOverlays.size();
   }

   @Override
   protected boolean onTap(int index) {
      Log.e("Tap", "Tap Performed");
      return true;
   }

   public void addOverlay(OverlayItem overlay) {
      mapOverlays.add(overlay);
       this.populate();
   }

   /**
    * Getting Latitude and Longitude on Touch event
    * **/
   @Override
   public boolean onTouchEvent(MotionEvent event, MapView mapView) 
   {   

       if (event.getAction() == 1) {                
           GeoPoint geopoint = mapView.getProjection().fromPixels(
               (int) event.getX(),
               (int) event.getY());
           // latitude
           double lat = geopoint.getLatitudeE6() / 1E6;
           // longitude
           double lon = geopoint.getLongitudeE6() / 1E6;
           Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon,    
    Toast.LENGTH_SHORT).show();
       }                            
       return false;
    } 

 }

这是我修改的代码

public class AndroidGoogleMapsActivity extends MapActivity {
private static final int MENU_ITEM_1 = Menu.FIRST + 1;
private static final int MENU_ITEM_2 = Menu.FIRST + 2;
private static final int MENU_ITEM_3 = Menu.FIRST + 3;


EditText Latitude1;
EditText Latitude2;
EditText Latitude3;
EditText Latitude4;



EditText Longitude1;
EditText Longitude2;
EditText Longitude3;
EditText Longitude4;





Toast msg;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Displaying Zooming controls
    MapView mapView = (MapView) findViewById(R.id.mapView);
    mapView.setBuiltInZoomControls(true);


    Latitude1=owncordinates.Latitude1;

    Latitude2=owncordinates.Latitude2;
    Latitude3=owncordinates.Latitude3;
    Latitude4=owncordinates.Latitude4;






    Longitude1=owncordinates.Longitude1;
    Longitude2=owncordinates.Longitude2;
    Longitude3=owncordinates.Longitude3;
    Longitude4=owncordinates.Longitude4;

    /**
     * Changing Map Type
     * */
    // mapView.setSatellite(true); // Satellite View
    // mapView.setStreetView(true); // Street View
    // mapView.setTraffic(true); // Traffic view

    /**
     * showing location by Latitude and Longitude
     * */        
    MapController mc = mapView.getController();



  double lat1 = Double.parseDouble(""+Latitude1);
       double lon1 = Double.parseDouble(""+Longitude1);





    double lat2 = Double.parseDouble(""+Latitude2);
    double lon2 = Double.parseDouble(""+Longitude2);




    double lat3 = Double.parseDouble(""+Latitude3);
    double lon3 = Double.parseDouble(""+Longitude3);


    double lat4 = Double.parseDouble(""+Latitude4);
    double lon4 = Double.parseDouble(""+Longitude4);




    GeoPoint geoPoint = new GeoPoint((int)(lat1 * 1E6), (int)(lon1 * 1E6));
    mc.animateTo(geoPoint);
    mc.setZoom(15);
    mapView.invalidate(); 


    /**
     * Placing Marker
     * */
    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red);
    AddItemizedOverlay itemizedOverlay = 
         new AddItemizedOverlay(drawable, this);


    OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello", "Sample Overlay     
   item");

    itemizedOverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedOverlay);

}

@Override
protected boolean isRouteDisplayed() {
    return false;
}







           import java.util.ArrayList;

     import android.content.Context;
 import android.graphics.drawable.Drawable;
 import android.util.Log;
 import android.view.MotionEvent;
 import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

     public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {

   private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

   private Context context;

   public AddItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
   }

   public AddItemizedOverlay(Drawable defaultMarker, Context context) {
        this(defaultMarker);
        this.context = context;
   }

   @Override
   protected OverlayItem createItem(int i) {
      return mapOverlays.get(i);
   }

   @Override
   public int size() {
      return mapOverlays.size();
   }

   @Override
   protected boolean onTap(int index) {
      Log.e("Tap", "Tap Performed");
      return true;
   }

   public void addOverlay(OverlayItem overlay) {
      mapOverlays.add(overlay);
       this.populate();
   }

   /**
    * Getting Latitude and Longitude on Touch event
    * **/
   @Override
   public boolean onTouchEvent(MotionEvent event, MapView mapView) 
   {   

       if (event.getAction() == 1) {                
           GeoPoint geopoint = mapView.getProjection().fromPixels(
               (int) event.getX(),
               (int) event.getY());
           // latitude
           double lat = geopoint.getLatitudeE6() / 1E6;
           // longitude
           double lon = geopoint.getLongitudeE6() / 1E6;
           Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon,    
       Toast.LENGTH_SHORT).show();
       }                            
       return false;
   } 

}

1 个答案:

答案 0 :(得分:0)

您只需执行以下操作:

    OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello", "Sample Overlay    
item");
    OverlayItem overlayitem2 = new OverlayItem(geoPoint, "Hello", "Sample Overlay    
item");

    itemizedOverlay.addOverlay(overlayitem);
    itemizedOverlay.addOverlay(overlayitem2);

现在您有2个相同类型的叠加项目。