百度地圖SDK for Android[Demo自訂圖層]

來源:互聯網
上載者:User

   在此基礎之上,我們對工程檔案做一定的修改。        第一步,修改布局檔案,添加button控制項,用於控制添加自訂覆蓋物。代碼如下:[html]  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      tools:context=".MainActivity" >        <!-- 放入百度地圖的mapview -->      <com.baidu.mapapi.map.MapView android:id="@+id/bmapsView"          android:layout_width="fill_parent"          android:layout_height="fill_parent"          android:clickable="true"/>        <!-- 添加按鈕 -->      <Button          android:id="@+id/button1"          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:layout_alignParentLeft="true"          android:layout_alignParentRight="true"          android:layout_alignParentTop="true"          android:text="《添加自訂覆蓋物》" />    </RelativeLayout>          第二步,定義繼承自ItemizedOverlay<OverlayItem>的自訂圖層。代碼如下:[java] public class MyOverLay extends ItemizedOverlay<OverlayItem> {        private List<OverlayItem> GeoList = new ArrayList<OverlayItem>();      private Context mContext;        private double mLat1 = 39.90923;//39.9022; // point1緯度      private double mLon1 = 116.397428;//116.3822; // point1經度        private double mLat2 = 39.9022;      private double mLon2 = 116.3922;        private double mLat3 = 39.917723;      private double mLon3 = 116.3722;        public MyOverLay(Drawable marker, Context context){          super(marker);          this.mContext= context;          // 用給定的經緯度構造GeoPoint,單位是微度 (度 * 1E6)          GeoPoint p1 = new GeoPoint((int)(mLat1 * 1E6), (int)(mLon1 * 1E6));          GeoPoint p2 = new GeoPoint((int)(mLat2 * 1E6), (int)(mLon2 * 1E6));          GeoPoint p3 = new GeoPoint((int)(mLat3 * 1E6), (int)(mLon3 * 1E6));            GeoList.add(new OverlayItem(p1, "P1", "point1"));          GeoList.add(new OverlayItem(p2, "P2", "point2"));          GeoList.add(new OverlayItem(p3, "P3", "point3"));                 populate();//createItem(int)方法構造item。一旦有了資料,在調用其它方法前,首先調用這個方法      }        @Override      protected OverlayItem createItem(int i){          return GeoList.get(i);      }        @Override      public int size(){          return GeoList.size();      }        @Override      // 處理當點擊事件      protected boolean onTap(int i){          Toast.makeText(this.mContext, GeoList.get(i).getSnippet(), Toast.LENGTH_SHORT).show();          return true;      }    }          第三步,在主類中定義並初始化button控制項,設定相應的點擊事件。代碼如下:[java]  // 初始化button並設定點擊操作事件  button = (Button) findViewById(R.id.button1);  button.setOnClickListener(new OnClickListener() {            @Override      public void onClick(View v) {          // TODO Auto-generated method stub          Drawable marker =getResources().getDrawable(R.drawable.ic_launcher);          mapView.getOverlays().add(new MyOverLay(marker, MainActivity.this));          mapView.refresh();       }  });          第四步,運行,顯示結果如下:        

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.