热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

接入百度地图定位加Marker点击事件

*在开始之前需要百度地图的包,定位方面需要这两个包,自己下载以及解压之后整体将libs包方放入即可,不要忘记放进去之后clear才可以哈,多的也不说了,网上很多,先来一个代码,这是xm

*在开始之前需要百度地图的包,定位方面需要这两个包,自己下载以及解压之后整体将libs包方放入即可,不要忘记放进去之后clear才可以

哈,多的也不说了,网上很多,先来一个代码,这是xmlbujuwenjai

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_match_parent"  android:layout_match_parent"  android:orientation="vertical">

    <com.baidu.mapapi.map.MapView  android:id="@+id/map"  android:layout_fill_parent"  android:layout_fill_parent"  android:clickable="true">

    com.baidu.mapapi.map.MapView>
LinearLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub  super.onCreate(savedInstanceState);
    SDKInitializer.initialize(getApplicationContext());//尤为重要,初始化百度地图,推荐在MyApplication里进行  setContentView(R.layout.transit_route_main);
    // 初始化地图  mMapView = (MapView) findViewById(R.id.map);
    markLayout = (LinearLayout) findViewById(R.id.mark_layout);
    mBaidumap = mMapView.getMap();
    UiSettings settings = mBaidumap.getUiSettings();
    settings.setRotateGesturesEnabled(false);//屏蔽旋转  settings.setOverlookingGesturesEnabled(false);//屏蔽双指下拉时变成3D地图   // 开启定位图层  mBaidumap.setMyLocationEnabled(true);

    locationto();
    init();
}
*这是onCreat方法里进行初始化操作

*下边为定位功能的初始化

private void locationto() {
    // TODO 定位功能  // 定位初始化  LocationClient mLocClient = new LocationClient(this);
    mLocClient.registerLocationListener(myListener);
    LocationClientOption option = new LocationClientOption();
    option.setOpenGps(true);// 打开gps  option.setCoorType("bd09ll"); // 设置坐标类型  option.setScanSpan(1000);
    //  option.setIsNeedAddress(true);
    mLocClient.setLocOption(option);
    mLocClient.start();
}
private void init() {
    //描述地图将要发生的变化,使用工厂类MapStatusUpdateFactory创建,设置级别  //为18,进去就是18了,默认是12  MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.zoomTo(18);
    mBaidumap.setMapStatus(mapStatusUpdate);
    //是否显示缩放按钮  mMapView.showZoomControls(false);
}
*这是地图的一些操作。。。关于禁止启动的一些地图初始化操作

*定位必须要有定位的监听函数,下方为定位的监听函数

/**  * 定位SDK监听函数  */ public class MyLocationListenner implements BDLocationListener {

    @Override
    public void onReceiveLocation(BDLocation location) {
        // 取经纬度  myLatitude = location.getLatitude();
        myLongitude = location.getLongitude();
        // map view 销毁后不在处理新接收的位置  if (location == null || mMapView == null)
            return;
        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(0)
                // 此处设置开发者获取到的方向信息,顺时针0-360  .direction(100).latitude(location.getLatitude())
                .longitude(location.getLongitude()).build();
        if (locData != null && mBaidumap != null)
            mBaidumap.setMyLocationData(locData);
        if (isFirstLoc) {
            isFirstLoc = false;
            LatLng ll = new LatLng(location.getLatitude(),
                    location.getLongitude());
            Login();
            MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
            mBaidumap.animateMapStatus(u);
        }

    }

    public void onReceivePoi(BDLocation poiLocation) {
    }
}
*下方3个方法是用来关闭地图的一些操作,切记,要记得关闭

protected void onPause() {
    mMapView.onPause();
    super.onPause();
}

@Override
protected void onResume() {
    mMapView.onResume();
    super.onResume();
}

@Override
protected void onDestroy() {
    mMapView.onDestroy();
    if (mBaidumap != null)
        mBaidumap = null;
    super.onDestroy();
}
*这是定位功能,我们之后还要做覆盖物的添加功能,下面为添加覆盖物

/**  * @author Mikyou  * 添加覆盖物  */ private void addMapMarks() {
    initMarksData();
    mBaidumap.clear();//先清除一下图层  LatLng latLng = null;
    Marker marker = null;
    OverlayOptions options;
    LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
    View view = inflater.inflate(R.layout.item_layout_baidu_map_market, null);//这个是显示的覆盖物,其实是可以显示view的
    ImageView phOne= (ImageView) view.findViewById(R.id.iv_dog_baidu_map_market_phone);
    //遍历MarkInfo的List一个MarkInfo就是一个Mark  for (int i = 0; i <markInfoList.size(); i++) {
       //这是我从网络获取的信息,网络获取就不贴了。。。(不能贴)
 Picasso.with(this).load(markInfoList.get(i).getDog_photo()).into(phone);
        myMarks = BitmapDescriptorFactory.fromView(view);//引入自定义的覆盖物图标,将其转化成一个BitmapDescriptor对象  //经纬度对象  latLng = new LatLng(markInfoList.get(i).getLatitude(), markInfoList.get(i).getLongitude());//需要创建一个经纬对象,通过该对象就可以定位到处于地图上的某个具体点  //图标  optiOns= new MarkerOptions().position(latLng).icon(myMarks).zIndex(9);
        marker = (Marker) mBaidumap.addOverlay(options);//将覆盖物添加到地图上  Bundle bundle = new Bundle();//创建一个Bundle对象将每个mark具体信息传过去,当点击该覆盖物图标的时候就会显示该覆盖物的详细信息  bundle.putSerializable("mark", markInfoList.get(i));
        marker.setExtraInfo(bundle);
    }
    MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);//通过这个经纬度对象,地图就可以定位到该点  mBaidumap.animateMapStatus(msu);
}
*这些信息是我从Info里获取的,就是实体类,我将数据存放到实体类,需要什么直接从实体类里获得

/**  * @author mikyou  * 初始化覆盖物点击事件  */ private void initMarksData() {
    mBaidumap.setOnMarkerClickListener(this);
    mBaidumap.setOnMapClickListener(this);
}
*接下来是覆盖物的点击事件

/**  * @author mikyou  * 覆盖物的点击事件  */ @Override
public boolean onMarkerClick(Marker marker) {
    Bundle bundle = marker.getExtraInfo();
    InfoScanDogBrand.DataBean.InfoBean MyMarker = (InfoScanDogBrand.DataBean.InfoBean) bundle.getSerializable("mark");
    ImageView iv = (ImageView) markLayout.findViewById(R.id.mark_image);
    TextView distanceTv = (TextView) markLayout.findViewById(R.id.distance);
    TextView nameTv = (TextView) markLayout.findViewById(R.id.name);
    TextView zanNumsTv = (TextView) markLayout.findViewById(R.id.zan_nums);

    Picasso.with(this).load(MyMarker.getDog_photo()).into(iv);
    distanceTv.setText(MyMarker.getDistance() + "");
    nameTv.setText(MyMarker.getDog_board_no());
    zanNumsTv.setText(MyMarker.getLongitude() + "");
    //初始化一个InfoWindow  initInfoWindow(MyMarker, marker);
    markLayout.setVisibility(View.VISIBLE);
    return true;
}
*初始化infowindow

/**  * @author mikyou  * 初始化出一个InfoWindow  */ private void initInfoWindow(InfoScanDogBrand.DataBean.InfoBean MyMarker, Marker marker) {
    // TODO Auto-generated method stub  InfoWindow infoWindow;
    LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
    View view = inflater.inflate(R.layout.item_layout_baidu_map, null);
    ImageView phOne= (ImageView) view.findViewById(R.id.iv_dog_photo);
    Picasso.with(this).load(MyMarker.getDog_photo()).into(phone);
    TextView name = (TextView) view.findViewById(R.id.tv_php_name);
    name.setText(MyMarker.getUser_name());
    TextView mobile = (TextView) view.findViewById(R.id.tv_php_mobile);
    mobile.setText(MyMarker.getUser_mobile());
    TextView id = (TextView) view.findViewById(R.id.tv_dog_brand_id);
    id.setText(MyMarker.getDog_board_no());
    final LatLng latLng = marker.getPosition();
    Point p = mBaidumap.getProjection().toScreenLocation(latLng);//将地图上的经纬度转换成屏幕中实际的点  p.y -= 47;//设置屏幕中点的Y轴坐标的偏移量  LatLng ll = mBaidumap.getProjection().fromScreenLocation(p);//把修改后的屏幕的点有转换成地图上的经纬度对象  /**  * @author mikyou  * 实例化一个InfoWindow的对象  * public InfoWindow(View view,LatLng position, int yOffset)通过传入的 view 构造一个 InfoWindow, 此时只是利用该view生成一个Bitmap绘制在地图中,监听事件由开发者实现。  * 参数:  * view - InfoWindow 展示的 view  * position - InfoWindow 显示的地理位置  * yOffset - InfoWindow Y 轴偏移量  * */  infoWindow = new InfoWindow(view, ll, 10);
    mBaidumap.showInfoWindow(infoWindow);//显示InfoWindow }
*整个地图的点击事件,就是用来点击地图让infowindow消失,增强用户体验

/**  * @author zhongqihong  * 给整个地图添加的点击事件  */ @Override
public void onMapClick(LatLng arg0) {//表示点击地图其他的地方使得覆盖物的详情介绍的布局隐藏,但是点击已显示的覆盖物详情布局上,则不会消失,因为在详情布局上添加了Clickable=true  //由于事件的传播机制,因为点击事件首先会在覆盖物布局的父布局(map)中,由于map是可以点击的,map则会把点击事件给消费掉,如果加上Clickable=true表示点击事件由详情布局自己处理,不由map来消费  markLayout.setVisibility(View.GONE);
    mBaidumap.hideInfoWindow();//隐藏InfoWindow }

@Override
public boolean onMapPoiClick(MapPoi mapPoi) {
    return false;
}
*这个类需要实现以下接口


*需要注意的是,infowindow和market都是可以使用view的,这就意味着我们可以用view布局来代替单一的控件,从而让我们实现一些在地图上需要实现的搜索,展示等功能

以上代码很简单,直接刚上去就可以,有些需要你填充东西,这个是我公司的东西,无法展示。


推荐阅读
author-avatar
awetyjdgtjkt_933
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有