欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android高德地圖marker自定義彈框窗口

 更新時(shí)間:2021年04月22日 17:33:56   作者:浪克oo  
這篇文章主要為大家詳細(xì)介紹了Android高德地圖marker自定義彈框窗口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android高德地圖marker自定義彈框窗口的具體代碼,供大家參考,具體內(nèi)容如下

最終效果:

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

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

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

public class MainApp extends android.app.Application {
 
    @Override
    public void onCreate() {
        super.onCreate();
        //高德地圖注冊(cè)
        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èn)網(wǎng)絡(luò)權(quán)限 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 用于訪問(wèn)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);// 此方法必須重寫(xiě)
        mMap = mMapView.getMap();
        initPoint(30.665534,104.070929);   //地圖中心點(diǎn)位
        initMarker();//測(cè)試點(diǎn)位
    }
    /**
     * 繪制marker
     */
    private void initMarker() {
        mMarkers = new ArrayList<>();
 
        //繪制marker  實(shí)際使用時(shí)會(huì)循環(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è)置布局最小高度和寬度,不然窗口會(huì)默認(rèn)寬度高度,會(huì)使布局顯示不完整

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; //因?yàn)槭亲远x的布局,返回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)位帶的消息信息  也可通過(guò)這個(gè)傳輸數(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就會(huì)彈出我們自定義的布局了

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);// 此方法必須重寫(xiě)
        mMap = mMapView.getMap();
        mMap.setOnMapClickListener(this);//地圖點(diǎn)擊事件
        initPoint(30.665534,104.070929);   //地圖中心點(diǎn)位
        initMarker();//測(cè)試點(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  實(shí)際使用時(shí)會(huì)循環(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(); //管理地圖的生命周期
    }
 
 
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android應(yīng)用動(dòng)態(tài)修改主題的方法示例

    Android應(yīng)用動(dòng)態(tài)修改主題的方法示例

    今天小編就為大家分享一篇關(guān)于Android應(yīng)用動(dòng)態(tài)修改主題的方法示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-03-03
  • AsyncTask類實(shí)例詳解

    AsyncTask類實(shí)例詳解

    這篇文章主要介紹了AsyncTask類實(shí)例詳解
    2017-10-10
  • Android 中TextView中跑馬燈效果的實(shí)現(xiàn)方法

    Android 中TextView中跑馬燈效果的實(shí)現(xiàn)方法

    這篇文章主要介紹了Android 中TextView中跑馬燈效果的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-02-02
  • 詳解如何使用Android Studio開(kāi)發(fā)Gradle插件

    詳解如何使用Android Studio開(kāi)發(fā)Gradle插件

    這篇文章主要介紹了詳解如何使用Android Studio開(kāi)發(fā)Gradle插件,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • android利用ContentResolver訪問(wèn)者獲取手機(jī)聯(lián)系人信息

    android利用ContentResolver訪問(wèn)者獲取手機(jī)聯(lián)系人信息

    這篇文章主要介紹了android利用ContentResolver訪問(wèn)者獲取手機(jī)聯(lián)系人信息,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2017-02-02
  • Android編程之代碼創(chuàng)建布局實(shí)例分析

    Android編程之代碼創(chuàng)建布局實(shí)例分析

    這篇文章主要介紹了Android編程之代碼創(chuàng)建布局的方法,結(jié)合實(shí)例形式分析了Android通過(guò)代碼創(chuàng)建布局的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • Android內(nèi)存泄漏檢測(cè)工具LeakCanary

    Android內(nèi)存泄漏檢測(cè)工具LeakCanary

    在Android的性能優(yōu)化中,內(nèi)存優(yōu)化是必不可少的點(diǎn),而內(nèi)存優(yōu)化最重要的一點(diǎn)就是解決內(nèi)存泄漏的問(wèn)題,在Android的內(nèi)存泄漏分析工具也不少,比如PC端的有:AndroidStudio自帶的Android?Profiler、MAT等工具;手機(jī)端也有,就是我們今天要介紹的LeakCanary
    2023-04-04
  • Android編程實(shí)現(xiàn)自定義手勢(shì)的方法詳解

    Android編程實(shí)現(xiàn)自定義手勢(shì)的方法詳解

    這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義手勢(shì)的方法,結(jié)合實(shí)例形式分析了Android自定義手勢(shì)的功能、相關(guān)函數(shù)與具體實(shí)現(xiàn)步驟,需要的朋友可以參考下
    2016-10-10
  • Android使用popupWindow仿微信彈出框使用方法

    Android使用popupWindow仿微信彈出框使用方法

    這篇文章主要為大家詳細(xì)介紹了Android使用popupWindow仿微信彈出框使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • 讓Android中RadioGroup不顯示在輸入法上面的辦法

    讓Android中RadioGroup不顯示在輸入法上面的辦法

    在Android開(kāi)發(fā)中,發(fā)現(xiàn)一個(gè)問(wèn)題,打開(kāi)輸入法導(dǎo)致下面的radioGroup的位置發(fā)生了變化,被頂?shù)搅溯斎敕ǖ纳厦妫敲丛撊绾谓鉀Q呢?下面來(lái)看看。
    2016-08-08

最新評(píng)論