溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中利用marker自定義一個彈框窗口

發(fā)布時間:2021-04-30 15:33:41 來源:億速云 閱讀:218 作者:Leah 欄目:開發(fā)技術(shù)

怎么在Android中利用marker自定義一個彈框窗口?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

1.gradle里添加高德地圖依賴

implementation 'com.amap.api:map2d:latest.integration'//2d地圖功能 
implementation 'com.amap.api:location:latest.integration'//定位功能

2.如果要用到定位的話,就首先到高德控制臺里面加入本應(yīng)用的信息獲取到key,再在Application里設(shè)置key,并在AndroidManifest.xml中應(yīng)用MainApp

public class MainApp extends android.app.Application {
 
    @Override
    public void onCreate() {
        super.onCreate();
        //高德地圖注冊
        AMapLocationClient.setApiKey("0f1d26a891783cc4d632965a7cc08443");
    }
 
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hk.testapplication">
    <uses-permission android:name="android.permission.INTERNET" /> <!-- 訪問網(wǎng)絡(luò)權(quán)限 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 用于訪問GPS定位 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <application
        android:name=".MainApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.TestApplication">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

3. 創(chuàng)建activity_main.xml地圖布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <com.amap.api.maps2d.MapView
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.amap.api.maps2d.MapView>
 
</androidx.constraintlayout.widget.ConstraintLayout>

 4. MainActivity里加載地圖,添加marker

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMapView = findViewById(R.id.mapview);
        mMapView.onCreate(savedInstanceState);// 此方法必須重寫
        mMap = mMapView.getMap();
        initPoint(30.665534,104.070929);   //地圖中心點(diǎn)位
        initMarker();//測試點(diǎn)位
    }
    /**
     * 繪制marker
     */
    private void initMarker() {
        mMarkers = new ArrayList<>();
 
        //繪制marker  實際使用時會循環(huán)創(chuàng)建marker并填入數(shù)據(jù)
        Marker marker = mMap.addMarker(new MarkerOptions()
                .anchor(0.5f, 0.5f)
                .position(new LatLng(30.665534,104.070929))
                .title("標(biāo)題數(shù)據(jù)")
                .snippet("消息數(shù)據(jù)")
                .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                        .decodeResource(getResources(), R.mipmap.ic_launcher_round))));//點(diǎn)位圖標(biāo)
        mMarkers.add(marker);
    }
    /**
     * 加載地圖中心點(diǎn)
     */
    private void initPoint(double latitude, double Longitude) {
        LatLng marker1 = new LatLng(latitude, Longitude);
        mMap.moveCamera(CameraUpdateFactory.changeLatLng(marker1));
        mMap.moveCamera(CameraUpdateFactory.zoomTo(12));
    }
   
   
    @Override
    public void onResume() {
        super.onResume();
        if (mMapView != null)
            mMapView.onResume(); //管理地圖的生命周期
    }
 
    @Override
    public void onPause() {
        super.onPause();
        if (mMapView != null)
            mMapView.onPause(); //管理地圖的生命周期
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mMapView != null)
            mMapView.onDestroy(); //管理地圖的生命周期
    }
 
 
}

5.添加彈框自定義布局view_map_infowindow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:minHeight="50dp"
    android:minWidth="100dp"
    android:background="#ffff"
    android:gravity="center"
    >
<ImageView
    android:id="@+id/iv_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"/>
    <TextView
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:id="@+id/tv_msg"
        android:text="自定義布局"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/iv_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>

記得設(shè)置布局最小高度和寬度,不然窗口會默認(rèn)寬度高度,會使布局顯示不完整

6.添加自定義彈框窗口adapter

/**
 *自定義地圖彈框adapter
 * @author hk
 */
public class MapInfoWinAdapter implements AMap.InfoWindowAdapter, View.OnClickListener {
    private Context mContext;
    private LatLng latLng;
    private TextView mTvMsg;
    private ImageView mIvLeft,mIvRight;
    private String mSnippet,mTitle;
    @Override
    public View getInfoWindow(Marker marker) {
        initData(marker);
        View view = initView();
        return view;
    }
    @Override
    public View getInfoContents(Marker marker) {
        return null; //因為是自定義的布局,返回null
    }
    public MapInfoWinAdapter(Context context) {
        mContext = context;
    }
    private void initData(Marker marker) {
        //當(dāng)前點(diǎn)位經(jīng)緯度
        latLng = marker.getPosition();
        //當(dāng)前點(diǎn)位帶的消息信息  也可通過這個傳輸數(shù)據(jù)把數(shù)據(jù)轉(zhuǎn)成json
        mSnippet = marker.getSnippet();
        //當(dāng)前點(diǎn)位帶的標(biāo)題信息
        mTitle = marker.getTitle();
    }
    @NonNull
    private View initView() {
        //獲取自定義的布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_map_infowindow, null);
        mTvMsg = (TextView) view.findViewById(R.id.tv_msg);
        mIvLeft= (ImageView) view.findViewById(R.id.iv_left);
        mIvRight= (ImageView) view.findViewById(R.id.iv_right);
        mTvMsg.setText("我是自定義布局彈框");
        mIvLeft.setOnClickListener(this);
        mIvRight.setOnClickListener(this);
        return view;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_left:
                Toast.makeText(mContext,"我是左邊按鈕點(diǎn)擊事件",Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_right:
                Toast.makeText(mContext,"我是右邊按鈕點(diǎn)擊事件",Toast.LENGTH_SHORT).show();
                break;
        }
 
    }
}

7.地圖綁定adapter

//重要 創(chuàng)建自定義適配器
MapInfoWinAdapter adapter = new MapInfoWinAdapter(this);
mMap.setInfoWindowAdapter(adapter);//設(shè)置自定義窗口adapter

現(xiàn)在點(diǎn)擊marker就會彈出我們自定義的布局了

8.點(diǎn)擊地圖或彈框關(guān)閉彈框窗口

mMap.setOnInfoWindowClickListener(this);//彈框窗口點(diǎn)擊事件
   mMap.setOnMapClickListener(this);//地圖點(diǎn)擊事件
 
    @Override
    public void onMapClick(LatLng latLng) {
        //點(diǎn)擊地圖區(qū)域關(guān)閉所有窗口
        for (Marker marker : mMarkers) {
            marker.hideInfoWindow();
        }
    }
    @Override
    public void onInfoWindowClick(Marker marker) {
        if (marker.isInfoWindowShown()) {
            marker.hideInfoWindow();//再次點(diǎn)擊窗口就隱藏窗口
        }
    }

到此自定義彈框窗口就完成了,以下為完整MainActivity代碼

public class MainActivity extends AppCompatActivity implements AMap.OnInfoWindowClickListener, AMap.OnMapClickListener {
    private AMap mMap;
    private List<Marker> mMarkers;
    private MapView mMapView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMapView = findViewById(R.id.mapview);
        mMapView.onCreate(savedInstanceState);// 此方法必須重寫
        mMap = mMapView.getMap();
        mMap.setOnMapClickListener(this);//地圖點(diǎn)擊事件
        initPoint(30.665534,104.070929);   //地圖中心點(diǎn)位
        initMarker();//測試點(diǎn)位
    }
    /**
     * 繪制marker
     */
    private void initMarker() {
        mMarkers = new ArrayList<>();
        //重要 創(chuàng)建自定義適配器
        MapInfoWinAdapter adapter = new MapInfoWinAdapter(this);
        mMap.setInfoWindowAdapter(adapter);//設(shè)置自定義窗口adapter
        mMap.setOnInfoWindowClickListener(this);
 
        //繪制marker  實際使用時會循環(huán)創(chuàng)建marker并填入數(shù)據(jù)
        Marker marker = mMap.addMarker(new MarkerOptions()
                .anchor(0.5f, 0.5f)
                .position(new LatLng(30.665534,104.070929))
                .title("標(biāo)題數(shù)據(jù)")
                .snippet("消息數(shù)據(jù)")
                .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                        .decodeResource(getResources(), R.mipmap.ic_launcher_round))));//點(diǎn)位圖標(biāo)
        mMarkers.add(marker);
    }
    /**
     * 加載地圖中心點(diǎn)
     */
    private void initPoint(double latitude, double Longitude) {
        LatLng marker1 = new LatLng(latitude, Longitude);
        mMap.moveCamera(CameraUpdateFactory.changeLatLng(marker1));
        mMap.moveCamera(CameraUpdateFactory.zoomTo(12));
    }
    @Override
    public void onMapClick(LatLng latLng) {
        //點(diǎn)擊地圖區(qū)域關(guān)閉所有窗口
        for (Marker marker : mMarkers) {
            marker.hideInfoWindow();
        }
    }
    @Override
    public void onInfoWindowClick(Marker marker) {
        if (marker.isInfoWindowShown()) {
            marker.hideInfoWindow();//再次點(diǎn)擊窗口就隱藏窗口
        }
    }
 
    @Override
    public void onResume() {
        super.onResume();
        if (mMapView != null)
            mMapView.onResume(); //管理地圖的生命周期
    }
 
    @Override
    public void onPause() {
        super.onPause();
        if (mMapView != null)
            mMapView.onPause(); //管理地圖的生命周期
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mMapView != null)
            mMapView.onDestroy(); //管理地圖的生命周期
    }
 
 
}

看完上述內(nèi)容,你們掌握怎么在Android中利用marker自定義一個彈框窗口的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI