上一篇博文我们探讨了百度地图的SDK的环境集成与搭建,这篇文章将介绍如何在百度地图上进行标注。
1、在activity_main.xml中新建bmapView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 添加百度地图控件 -->
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
</LinearLayout>
2、在MainActivity.java中获取mapView并进行标注。
package com.example.baidu_demo_2;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView = (MapView)this.findViewById(R.id.bmapView);
// 设置地图类型
BaiduMap mBaiduMap = mapView.getMap();
// 普通地图
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
// 卫星地图
// mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
//实时交通地图
// mBaiduMap.setTrafficEnabled(true);
// 地图标注
LatLng point = new LatLng(39.963175, 116.400244);
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.icon_marka);
OverlayOptions overlayOptions = new MarkerOptions().position(point).icon(bitmapDescriptor);
mBaiduMap.addOverlay(overlayOptions);
}
}
时间: 2024-10-28 14:43:33