百度地图android自定义文字,android开发之百度地图地理编码 自定义marker

一、下载最新百度地图sdk,导入工程中

二、根据官方文档初始化地图,在main.xml中添加对应布局

android:id="@+id/bmapView"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:clickable="true" />

//获取地图控件引用

mapView = (MapView) findViewById(R.id.bmapView);

baiduMap = mapView.getMap(); // 获取地图控制器

三、通过地理编码获取经纬度

p// 第一步,创建地理编码检索实例;

mSearch = GeoCoder.newInstance();

// 第二步,创建地理编码检索监听者;

OnGetGeoCoderResultListener listener = new OnGetGeoCoderResultListener() {

public void onGetGeoCodeResult(GeoCodeResult result) {

if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {

//没有检索到结果

}else {

//获取地理编码结果

float latitude = (float) result.getLocation().latitude;

float longitude = (float) result.getLocation().longitude;

final LatLng point = new LatLng(latitude, longitude);

//加载自定义marker

View popMarker = View.inflate(MainActivity.this, R.layout.pop, null);

Bitmap bitmap1 = getViewBitmap(popMarker);

BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap1);

//构建MarkerOption,用于在地图上添加Marker

OverlayOptions option = new MarkerOptions()

.position(point)

.icon(bitmapDescriptor);

//在地图上添加Marker,并显示

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

}

}

@Override

public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {

if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {

//没有找到检索结果

}

//获取反向地理编码结果

}

};

// 第三步,设置地理编码检索监听者;

mSearch.setOnGetGeoCodeResultListener(listener);

// 第四步,发起地理编码检索;

mSearch.geocode(new GeoCodeOption()

.city("北京")

.address("海淀区上地十街10号"));//百度地图上少一个括号

将View转换成Bitmap的方法

/**

* 将View转换成Bitmap

* @param addViewContent

* @return

*/

private Bitmap getViewBitmap(View addViewContent) {

addViewContent.setDrawingCacheEnabled(true);

addViewContent.measure(

View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),

View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

addViewContent.layout(0, 0,

addViewContent.getMeasuredWidth(),

addViewContent.getMeasuredHeight());

addViewContent.buildDrawingCache();

Bitmap cacheBitmap = addViewContent.getDrawingCache();

Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

return bitmap;

}

Marker的自定义布局pop.xml

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center_vertical" >

android:id="@+id/iv_title"

android:layout_width="42dp"

android:layout_height="42dp"

/>

android:id="@+id/tv_title"

android:layout_width="wrap_content"

android:layout_height="42dp"

android:padding="5dp"

android:gravity="center"

android:text="标题"

android:textSize="16dp" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值