Using ItemizedOverlay to add marker on MapView

Thảo luận trong 'Điện thoại, viễn thông' bắt đầu bởi bka15, 30/9/12.

  1. bka15 New Member

    bka15

    Tham gia ngày:
    18/7/12
    Bài viết:
    128
    Đã được thích:
    105
    Điểm thành tích:
    0
    Reference last exercise MapView and MapActivity, we are going to add overlay of ItemizedOverlay, to add marker on our MapView.

    [​IMG]

    The main.xml and AndroidManifest.xml are kept as in last post MapView and MapActivity.

    Add a new class MyItemizedOverlay.java extends ItemizedOverlay.


    PHP:
    package com.AndroidMapView;
     
    import java.util.ArrayList;
     
    import android.graphics.Canvas;
    import android.graphics.drawable.Drawable;
     
    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 
    MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{
     
    private 
    ArrayList<OverlayItemoverlayItemList = new ArrayList<OverlayItem>();
     
    public 
    MyItemizedOverlay(Drawable marker) {
    super(boundCenterBottom(marker));
    // TODO Auto-generated constructor stub
     
    populate();
    }
     
    public 
    void addItem(GeoPoint pString titleString snippet){
    OverlayItem newItem = new OverlayItem(ptitlesnippet);
    overlayItemList.add(newItem);
       
    populate();
    }
     
    @
    Override
    protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return overlayItemList.get(i);
    }
     
    @
    Override
    public int size() {
    // TODO Auto-generated method stub
    return overlayItemList.size();
    }
     
    @
    Override
    public void draw(Canvas canvasMapView mapViewboolean shadow) {
    // TODO Auto-generated method stub
    super.draw(canvasmapViewshadow);
    //boundCenterBottom(marker);
    }
     
    }
    Modify our main class AndroidMapViewActivity.java to add ItemizedOverlay on ur MapView.


    PHP:
    package com.AndroidMapView;
     
    import com.google.android.maps.GeoPoint;
    import com.google.android.maps.MapActivity;
    import com.google.android.maps.MapView;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
     
    public class 
    AndroidMapViewActivity extends MapActivity {
     
    /** Called when the activity is first created. */
     
    @Override
     
    public void onCreate(Bundle savedInstanceState) {
         
    super.onCreate(savedInstanceState);
         
    setContentView(R.layout.main);
         
    MapView mapView = (MapViewfindViewById(R.id.mapview);
         
    mapView.setBuiltInZoomControls(true);
       
         
    Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
         
    int markerWidth marker.getIntrinsicWidth();
         
    int markerHeight marker.getIntrinsicHeight();
         
    marker.setBounds(0markerHeightmarkerWidth0);
     
       
         
    MyItemizedOverlay myItemizedOverlay = new MyItemizedOverlay(marker);
         
    mapView.getOverlays().add(myItemizedOverlay);
       
         
    GeoPoint myPoint1 = new GeoPoint(0*10000000*1000000);
         
    myItemizedOverlay.addItem(myPoint1"myPoint1""myPoint1");
         
    GeoPoint myPoint2 = new GeoPoint(50*100000050*1000000);
         
    myItemizedOverlay.addItem(myPoint2"myPoint2""myPoint2");
     
     }
     
    @
    Override
    protected boolean isLocationDisplayed() {
    // TODO Auto-generated method stub
    return false;
    }
     
    @
    Override
    protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
    }
     
    }
    Related articles:
    - Easy drawing current location and compass on the MapView, using MyLocationOverlay.
    - Detect touch on marker in MapView

    Un-reelated article:
    - OpenStreetMap version using osmdroid
     
    2 people like this.
    Đang tải...
  2. nguyenson.glhd New Member

    nguyenson.glhd

    Tham gia ngày:
    8/4/13
    Bài viết:
    31
    Đã được thích:
    0
    Điểm thành tích:
    0
    Giới tính:
    Nam
    Bài viết hay.
     

Chia sẻ trang này