日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 百度map 一个layout加载多个mapview,android 百度地图API 使用Marker和InfoWindow

發布時間:2025/3/12 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 百度map 一个layout加载多个mapview,android 百度地图API 使用Marker和InfoWindow 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言:在android開發過程中,百度地圖的使用是比較普遍的,但是如何使用,使用什么版本的百度API還是需要一些講究。

在項目過程中,需要用到百度地圖的marker和InfoWindow的功能。

標注覆蓋物(百度地圖官方圖)

布局文件很簡單,主要就是mapview,如下:

android:layout_width="match_parent" android:layout_height="match_parent"

android:orientation="vertical"

android:background="@color/overall_bg">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

android:id="@+id/gps_button_reset"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/gps_reset_button"

android:layout_marginBottom="8dp"

android:layout_marginLeft="16dp"

android:layout_marginTop="8dp"

android:layout_marginRight="16dp"

/>

android:id="@+id/gps_button_history"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/gps_history_button"

android:layout_marginBottom="8dp"

android:layout_marginLeft="16dp"

android:layout_marginTop="8dp"

android:layout_marginRight="16dp"

/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/gps_text"

android:paddingBottom="8dp"

android:paddingTop="8dp"

android:textSize="15sp"

android:layout_gravity="center"

android:gravity="center"

/>

android:id="@+id/bmapView"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:clickable="true" />

標注覆蓋物實現代碼如下:

/**

* 根據手表的經緯度在地圖上設置位置,更新頂部的位置文字描述

*/

private void updateLocation(ArrayList list) {

/**

* 1. 新用戶默認顯示地址

*/

double lg = 104.06; // 成都市的經緯度

double lt = 30.67;

String location = getResources().getString(R.string.fake_position);

baiduMap.clear();

List potions = new ArrayList<>();

for(int i = list.size() -1; i >=0 ; i--){

// gps 非空,則設置經緯度、位置的文字描述

lg = Double.parseDouble(list.get(i).getLongitude());

lt = Double.parseDouble(list.get(i).getLatitude());

location = list.get(i).getAddress();

//地址太長,處理下

location = location.replace("四川省","").replace("成都市","").replace(".","");

final LatLng ll = new LatLng(lt, lg); // 注意經緯度順序

/**

* 為每個足跡依據先后順序編號

*/

int image_id = 0;

switch (i){

case 9: image_id = R.mipmap.icon_mark1;break;

case 8: image_id = R.mipmap.icon_mark2;break;

case 7: image_id = R.mipmap.icon_mark3;break;

case 6: image_id = R.mipmap.icon_mark4;break;

case 5: image_id = R.mipmap.icon_mark5;break;

case 4: image_id = R.mipmap.icon_mark6;break;

case 3: image_id = R.mipmap.icon_mark7;break;

case 2: image_id = R.mipmap.icon_mark8;break;

case 1: image_id = R.mipmap.icon_mark9;break;

case 0: image_id = R.mipmap.icon_mark10;break;

}

BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(image_id);

OverlayOptions options = new MarkerOptions().position(ll).icon(descriptor).zIndex(i);

Marker marker = (Marker) baiduMap.addOverlay(options);

//為marker添加識別標記信息

Bundle bundle = new Bundle();

bundle.putSerializable("info", list.get(i));

marker.setExtraInfo(bundle);

MapStatusUpdate update = MapStatusUpdateFactory.newLatLngZoom(ll,17);

baiduMap.setMapStatus(update);

}

為標記的marker添加監聽,點擊時實現彈出infowindow。(infowindow每次最多顯示一條信息)

/**

* 為標記添加監聽

* 點擊彈出地理位置信息

*/

baiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {

@Override

public boolean onMarkerClick(Marker marker) {

// 獲得marker中的數據

GPSBean info = (GPSBean) marker.getExtraInfo().get("info");

// 生成一個TextView用戶在地圖中顯示InfoWindow

TextView location = new TextView(getApplicationContext());

location.setBackgroundResource(R.mipmap.gps_button);

location.setPadding(15, 15, 8, 35);

location.setTextColor(Color.DKGRAY);

location.setText("定位時間:" + info.getUploadTime() + "\n" + info.getAddress());

location.setTextSize(12);

// 將marker所在的經緯度的信息轉化成屏幕上的坐標

final LatLng ll = marker.getPosition();

Point p = baiduMap.getProjection().toScreenLocation(ll);

p.y -= 47;

LatLng llInfo = baiduMap.getProjection().fromScreenLocation(p);

// 為彈出的InfoWindow添加點擊事件

mInfoWindow = new InfoWindow(location,llInfo, new InfoWindow.OnInfoWindowClickListener() {

@Override

public void onInfoWindowClick() {

baiduMap.hideInfoWindow();

}

});

// 顯示最后一條的InfoWindow

baiduMap.showInfoWindow(mInfoWindow);

return true;

}

});

最后將地圖顯示到最新的數據,并且顯示Infowindow。

/**

* 顯示最后一個地理位置信息

* 創建InfoWindow展示的view

*/

private void updateBaidumapInfowindown(String location,double lt, double lg){

TextView textView = new TextView(getApplicationContext());

textView.setBackgroundResource(R.mipmap.gps_button);

textView.setPadding(15, 15, 8, 35);

textView.setTextColor(Color.DKGRAY);

textView.setText(location);

textView.setTextSize(12);

final LatLng ll = new LatLng(lt,lg);

mInfoWindow = new InfoWindow(textView, ll, new InfoWindow.OnInfoWindowClickListener() {

@Override

public void onInfoWindowClick() {

baiduMap.hideInfoWindow();

}

});

//顯示InfoWindow

baiduMap.showInfoWindow(mInfoWindow);

/**

* 將地圖視野移動最新的位置

*/

MapStatusUpdate update = MapStatusUpdateFactory.newLatLngZoom(ll,17);

baiduMap.setMapStatus(update);

}

至此可以完成。但是還有最重要的一點,則是百度Api版本的問題,本方法在新版本 3.4.1 似乎就不能用這種方法實現,只能使用3.0.0版本。

總結

以上是生活随笔為你收集整理的android 百度map 一个layout加载多个mapview,android 百度地图API 使用Marker和InfoWindow的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。