1. 程式人生 > >高德地圖---自定義飄窗

高德地圖---自定義飄窗

第一步繼承類MainActivity extends Activity implements OnInfoWindowClickListener,InfoWindowAdapter

//給定位的mark新增infoWindow
aMap.setOnInfoWindowClickListener(this);// 設定點選infoWindow事件監聽器
aMap.setInfoWindowAdapter(this);// 設定自定義InfoWindow樣式

第二步 重寫繼承類中的方法

//infoWindow回撥的方法
    @Override
    public View getInfoContents(Marker arg0) {
        Log.e
("infoWindow", "--------------getInfoContents---------------------"); View infoContent = getLayoutInflater().inflate( R.layout.posi_layout, null); render(arg0, infoContent); return infoContent; } //當點選mark的時候會呼叫getInfoWindow 和 render方法 可以來顯示或隱藏飄窗 @Override public View getInfoWindow(Marker arg0) { Log.e
("infoWindow", "--------------getInfoWindow---------------------"); View infoContent = getLayoutInflater().inflate( R.layout.posi_layout, null); render(arg0, infoContent); return infoContent; } private void render(Marker arg0, View infoContent) { Log.e
("infowindow", "----------------render--------------------------------"); ImageView image_navi=(ImageView) infoContent.findViewById(R.id.image_posi_navi); //自定義的飄窗佈局 新增點選方法 image_navi.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Log.e("render", "-----image_navi.setOnClickListener----------"); if(mark_posiAPP.getPosition().latitude==0 |mark_posiAPP.getPosition().longitude==0|Double.parseDouble(weidu)==0|Double.parseDouble(jingdu)==0){ Toast.makeText(getApplicationContext(), "定位未成功不能進行路徑規劃", 0).show(); }else{ Intent intent=new Intent(MainActivity.this,Activity_NaviChoose.class); intent.putExtra("la_start", mark_posiAPP.getPosition().latitude); intent.putExtra("ln_start", mark_posiAPP.getPosition().longitude); intent.putExtra("la_end", Double.parseDouble(weidu)); intent.putExtra("ln_end", Double.parseDouble(jingdu)); startActivity(intent); } } }); }

第三步 通過點選mark顯示或隱藏飄窗

//mark點選時呼叫的方法
    @Override
    public boolean onMarkerClick(Marker arg0) {
        Log.e("mark","----------onMarkerClick--------------------------" );
        if(flag_isShowInfoWindow){
            Log.e("mark","----------onMarkerClick--------------------------" +flag_isShowInfoWindow);
            aMap.clear();
            MarkerOptions mark=new MarkerOptions();
            mark.position(new LatLng(mark_posiAPP.getPosition().latitude, mark_posiAPP.getPosition().longitude));
            BitmapDescriptor fromResource01 = new BitmapDescriptorFactory().fromResource(R.drawable.ic_point_view_num);
            mark.icon(fromResource01);
            aMap.addMarker(mark);
            flag_isShowInfoWindow=false;

        }else{  //顯示
            Log.e("mark","----------onMarkerClick--------------------------" +flag_isShowInfoWindow);
            aMap.clear();
            MarkerOptions mark=new MarkerOptions();
            mark.position(new LatLng(mark_posiAPP.getPosition().latitude, mark_posiAPP.getPosition().longitude));
            BitmapDescriptor fromResource01 = new BitmapDescriptorFactory().fromResource(R.drawable.ic_point_view_num);
            mark.icon(fromResource01);
            mark.title("我是titile"); //不寫這個我這就顯示不了 也不知道為什麼
            mark.snippet("我是sippet");
            Marker addMarker2 = aMap.addMarker(mark);

            addMarker2.showInfoWindow();
            aMap.invalidate();// 重新整理地圖

            flag_isShowInfoWindow=true;
        }

        return false;
    }