百度地图自定义Marker(android)

上传者: showraining | 上传时间: 2025-01-12 18:45:20 | 文件大小: 8.82MB | 文件类型: RAR
在Android开发中,百度地图API提供了丰富的功能,其中包括自定义Marker。Marker是地图上的一个标记,通常用于表示特定的位置或信息。本项目聚焦于在Android平台上如何利用百度地图SDK实现自定义Marker,以满足个性化需求。 我们需要理解百度地图SDK的基本用法。在Android项目中,需要引入百度地图的SDK库,这可以通过在`build.gradle`文件中添加依赖来完成。例如: ```gradle dependencies { implementation 'com.baidu.mapapi:baidumapapi:5.2.0' } ``` 然后,确保在`AndroidManifest.xml`文件中添加了必要的权限,如互联网访问权限和定位权限: ```xml ``` 接下来,我们创建一个地图Activity,初始化百度地图控件,并设置地图中心点。在布局文件中添加MapView组件,代码如下: ```xml ``` 在Activity的Java代码中初始化并设置地图: ```java MapView mapView = findViewById(R.id.bmapView); mapView.onCreate(savedInstanceState); BaiduMap baiduMap = mapView.getMap(); baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); LatLng center = new LatLng(39.916527, 116.407395); // 北京市的经纬度 MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(center); baiduMap.setMapStatus(update); ``` 自定义Marker的关键在于创建BitmapDescriptor,它代表地图上的图标。可以使用`BitmapDescriptorFactory`来从资源文件加载图片,或者创建自定义的Bitmap。例如,我们可以从`res/drawable`目录下的图片资源创建BitmapDescriptor: ```java BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_web); ``` 接下来,创建一个MarkerOption对象,设置其位置、图标和点击事件: ```java LatLng markerPosition = new LatLng(39.916527, 116.407395); MarkerOptions markerOption = new MarkerOptions() .position(markerPosition) .icon(bitmapDescriptor) .draggable(true); ``` 将MarkerOption添加到地图上: ```java Marker marker = baiduMap.addOverlay(markerOption); ``` 如果需要为Marker添加自定义信息,可以考虑使用InfoWindow,它会在用户点击Marker时弹出。创建一个包含信息的布局文件,然后设置InfoWindowAdapter: ```java LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View infoWindow = inflater.inflate(R.layout.info_window, null); baiduMap.setInfoWindowAdapter(new BaiduMap.InfoWindowAdapter() { @Override public View getInfoWindow(Marker marker) { return infoWindow; } @Override public View getInfoContents(Marker marker) { return null; } }); ``` 当用户点击Marker时,会自动显示InfoWindow。可以通过监听Marker的点击事件来实现: ```java baiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { // 显示InfoWindow baiduMap.showInfoWindow(marker); return true; // 返回true表示已处理点击事件 } }); ``` 在实际应用中,可能需要动态添加多个Marker,或者根据数据动态更新Marker的位置和信息。这就需要在适当的地方调用`addOverlay`方法,并处理对应的Marker列表。 Android版百度地图自定义Marker涉及的关键知识点包括:引入百度地图SDK、设置地图权限、初始化地图控件、设置地图中心点、创建BitmapDescriptor、创建MarkerOption、添加Marker、实现InfoWindow和监听Marker点击事件。通过这些步骤,开发者可以根据具体需求打造个性化的地图应用。

文件下载

资源详情

[{"title":"( 84 个子文件 8.82MB ) 百度地图自定义Marker(android)","children":[{"title":".project <span style='color:#111;'> 848B </span>","children":null,"spread":false},{"title":"project.properties <span style='color:#111;'> 634B </span>","children":null,"spread":false},{"title":"src","children":[{"title":"com","children":[{"title":"example","children":[{"title":"baidumapdemo","children":[{"title":"MainActivity.java <span style='color:#111;'> 7.75KB </span>","children":null,"spread":false}],"spread":true},{"title":"bean","children":[{"title":"Hotel.java <span style='color:#111;'> 2.64KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true},{"title":"AndroidManifest.xml <span style='color:#111;'> 2.39KB </span>","children":null,"spread":false},{"title":"res","children":[{"title":"drawable-ldpi","children":null,"spread":false},{"title":"drawable-hdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 7.48KB </span>","children":null,"spread":false},{"title":"custom_marker_triangle.png <span style='color:#111;'> 2.82KB </span>","children":null,"spread":false},{"title":"hotel_pic.png <span style='color:#111;'> 10.88KB </span>","children":null,"spread":false}],"spread":true},{"title":"values-v11","children":[{"title":"styles.xml <span style='color:#111;'> 325B </span>","children":null,"spread":false}],"spread":true},{"title":"menu","children":[{"title":"main.xml <span style='color:#111;'> 423B </span>","children":null,"spread":false}],"spread":true},{"title":"drawable-mdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 3.69KB </span>","children":null,"spread":false}],"spread":true},{"title":"layout","children":[{"title":"custom_marker.xml <span style='color:#111;'> 1.41KB </span>","children":null,"spread":false},{"title":"activity_main.xml <span style='color:#111;'> 415B </span>","children":null,"spread":false}],"spread":true},{"title":"drawable-xxhdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 24.20KB </span>","children":null,"spread":false}],"spread":true},{"title":"drawable-xhdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 12.22KB </span>","children":null,"spread":false}],"spread":true},{"title":"values-w820dp","children":[{"title":"dimens.xml <span style='color:#111;'> 377B </span>","children":null,"spread":false}],"spread":true},{"title":"values","children":[{"title":"strings.xml <span style='color:#111;'> 225B </span>","children":null,"spread":false},{"title":"styles.xml <span style='color:#111;'> 689B </span>","children":null,"spread":false},{"title":"dimens.xml <span style='color:#111;'> 216B </span>","children":null,"spread":false}],"spread":false},{"title":"values-v14","children":[{"title":"styles.xml <span style='color:#111;'> 382B </span>","children":null,"spread":false}],"spread":false}],"spread":false},{"title":".settings","children":[{"title":"org.eclipse.jdt.core.prefs <span style='color:#111;'> 177B </span>","children":null,"spread":false}],"spread":true},{"title":"assets","children":[{"title":"data.json <span style='color:#111;'> 6.25KB </span>","children":null,"spread":false}],"spread":true},{"title":"gen","children":[{"title":"com","children":[{"title":"example","children":[{"title":"baidumapdemo","children":[{"title":"BuildConfig.java <span style='color:#111;'> 166B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 408.66KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true},{"title":"android","children":[{"title":"support","children":[{"title":"v7","children":[{"title":"appcompat","children":[{"title":"R.java <span style='color:#111;'> 79.16KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true}],"spread":true},{"title":"libs","children":[{"title":"armeabi-v7a","children":[{"title":"libBaiduMapSDK_v3_3_0_15.so <span style='color:#111;'> 1.10MB </span>","children":null,"spread":false},{"title":"liblocSDK5.so <span style='color:#111;'> 33.84KB </span>","children":null,"spread":false}],"spread":true},{"title":"android-support-v4.jar <span style='color:#111;'> 1.13MB </span>","children":null,"spread":false},{"title":"fastjson-1.2.6.jar <span style='color:#111;'> 410.92KB </span>","children":null,"spread":false},{"title":"armeabi","children":[{"title":"libBaiduMapSDK_v3_3_0_15.so <span style='color:#111;'> 1.10MB </span>","children":null,"spread":false},{"title":"liblocSDK5.so <span style='color:#111;'> 33.84KB </span>","children":null,"spread":false}],"spread":true},{"title":"BaiduLBS_Android.jar <span style='color:#111;'> 1020.76KB </span>","children":null,"spread":false}],"spread":true},{"title":".classpath <span style='color:#111;'> 475B </span>","children":null,"spread":false},{"title":"proguard-project.txt <span style='color:#111;'> 781B </span>","children":null,"spread":false},{"title":"ic_launcher-web.png <span style='color:#111;'> 50.19KB </span>","children":null,"spread":false},{"title":"bin","children":[{"title":"resources.ap_ <span style='color:#111;'> 411.21KB </span>","children":null,"spread":false},{"title":"R.txt <span style='color:#111;'> 59.62KB </span>","children":null,"spread":false},{"title":"classes","children":[{"title":"com","children":[{"title":"example","children":[{"title":"baidumapdemo","children":[{"title":"R$layout.class <span style='color:#111;'> 2.32KB </span>","children":null,"spread":false},{"title":"R$style.class <span style='color:#111;'> 18.14KB </span>","children":null,"spread":false},{"title":"R$dimen.class <span style='color:#111;'> 4.27KB </span>","children":null,"spread":false},{"title":"MainActivity$MyLocationListenner.class <span style='color:#111;'> 2.20KB </span>","children":null,"spread":false},{"title":"MainActivity$SDKReceiver.class <span style='color:#111;'> 1.42KB </span>","children":null,"spread":false},{"title":"BuildConfig.class <span style='color:#111;'> 357B </span>","children":null,"spread":false},{"title":"R$drawable.class <span style='color:#111;'> 3.67KB </span>","children":null,"spread":false},{"title":"R$attr.class <span style='color:#111;'> 8.52KB </span>","children":null,"spread":false},{"title":"R$bool.class <span style='color:#111;'> 808B </span>","children":null,"spread":false},{"title":"R$integer.class <span style='color:#111;'> 645B </span>","children":null,"spread":false},{"title":"R$id.class <span style='color:#111;'> 3.71KB </span>","children":null,"spread":false},{"title":"R$anim.class <span style='color:#111;'> 801B </span>","children":null,"spread":false},{"title":"R.class <span style='color:#111;'> 1011B </span>","children":null,"spread":false},{"title":"R$string.class <span style='color:#111;'> 1.49KB </span>","children":null,"spread":false},{"title":"MainActivity$1.class <span style='color:#111;'> 867B </span>","children":null,"spread":false},{"title":"R$menu.class <span style='color:#111;'> 406B </span>","children":null,"spread":false},{"title":"MainActivity.class <span style='color:#111;'> 8.04KB </span>","children":null,"spread":false},{"title":"R$color.class <span style='color:#111;'> 3.67KB </span>","children":null,"spread":false},{"title":"R$styleable.class <span style='color:#111;'> 18.31KB </span>","children":null,"spread":false}],"spread":false},{"title":"bean","children":[{"title":"Hotel.class <span style='color:#111;'> 2.73KB </span>","children":null,"spread":false}],"spread":false}],"spread":false}],"spread":false},{"title":"android","children":[{"title":"support","children":[{"title":"v7","children":[{"title":"appcompat","children":[{"title":"R$layout.class <span style='color:#111;'> 2.26KB </span>","children":null,"spread":false},{"title":"R$style.class <span style='color:#111;'> 18.09KB </span>","children":null,"spread":false},{"title":"R$dimen.class <span style='color:#111;'> 4.18KB </span>","children":null,"spread":false},{"title":"R$drawable.class <span style='color:#111;'> 3.57KB </span>","children":null,"spread":false},{"title":"R$attr.class <span style='color:#111;'> 8.53KB </span>","children":null,"spread":false},{"title":"R$bool.class <span style='color:#111;'> 820B </span>","children":null,"spread":false},{"title":"R$integer.class <span style='color:#111;'> 657B </span>","children":null,"spread":false},{"title":"R$id.class <span style='color:#111;'> 3.58KB </span>","children":null,"spread":false},{"title":"R$anim.class <span style='color:#111;'> 813B </span>","children":null,"spread":false},{"title":"R.class <span style='color:#111;'> 1015B </span>","children":null,"spread":false},{"title":"R$string.class <span style='color:#111;'> 1.40KB </span>","children":null,"spread":false},{"title":"R$color.class <span style='color:#111;'> 3.68KB </span>","children":null,"spread":false},{"title":"R$styleable.class <span style='color:#111;'> 17.91KB </span>","children":null,"spread":false}],"spread":false}],"spread":false}],"spread":false}],"spread":false}],"spread":false},{"title":"AndroidManifest.xml <span style='color:#111;'> 2.39KB </span>","children":null,"spread":false},{"title":"dexedLibs","children":[{"title":"appcompat_v7-6ccf2c767201a021c6e595fa68d72190.jar <span style='color:#111;'> 166B </span>","children":null,"spread":false},{"title":"BaiduLBS_Android-cee1712b25e0772942eea977f67c10a2.jar <span style='color:#111;'> 736.17KB </span>","children":null,"spread":false},{"title":"android-support-v7-appcompat-aadc57fef13df64cdd46a6b775f4f29b.jar <span style='color:#111;'> 228.43KB </span>","children":null,"spread":false},{"title":"fastjson-1.2.6-eff84e2e1c0c30db1ad37f46a744b878.jar <span style='color:#111;'> 180.84KB </span>","children":null,"spread":false},{"title":"android-support-v4-4696e9ee16c3c78a42e9239db05fdb35.jar <span style='color:#111;'> 395.10KB </span>","children":null,"spread":false}],"spread":false},{"title":"jarlist.cache <span style='color:#111;'> 364B </span>","children":null,"spread":false},{"title":"res","children":[{"title":"crunch","children":[{"title":"drawable-hdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 5.82KB </span>","children":null,"spread":false},{"title":"custom_marker_triangle.png <span style='color:#111;'> 213B </span>","children":null,"spread":false}],"spread":false},{"title":"drawable-mdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 3.04KB </span>","children":null,"spread":false}],"spread":false},{"title":"drawable-xxhdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 17.47KB </span>","children":null,"spread":false}],"spread":false},{"title":"drawable-xhdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 9.14KB </span>","children":null,"spread":false}],"spread":false}],"spread":false}],"spread":false},{"title":"classes.dex <span style='color:#111;'> 2.31MB </span>","children":null,"spread":false},{"title":"BaiduMapDemo.apk <span style='color:#111;'> 3.43MB </span>","children":null,"spread":false}],"spread":true}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明