其他分享
首页 > 其他分享> > Android 百度地图的使用

Android 百度地图的使用

作者:互联网

1.初始化地图

//初始化地图
    private void initMapView() {
        //1.获取地图控件引用
        mMapView = findViewById(R.id.bmapView);
        mBaidumap = mMapView.getMap();
        //2.设置初始定位点在上海
        GEO_SHANGHAI = new LatLng(31.227, 121.481);
        GEO_BEIJING = new LatLng(39.963175, 116.400244);
        mBaidumap.setMapStatus(MapStatusUpdateFactory.newLatLng(GEO_SHANGHAI));
        //3.设置地图比例尺,14级。比例尺为1000m(百度地图暂时只能放大到100m)
        mBaidumap.setMapStatus(MapStatusUpdateFactory.zoomTo(13));

    }

2.用户输入地址获取经纬度

package com.example.lucky.zjzyhq3;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.table.MapTableData;

public class ShowDataActivity extends AppCompatActivity {
    SmartTable table;
    Button btSD_exit;
    Button btSD_query;
    GeoCoder mSearch = null; // 搜索模块,也可去掉地图模块独立使用
    BaiduMap mBaidumap;
    MapView mMapView = null;
    LatLng GEO_SHANGHAI;
    LatLng GEO_BEIJING;
    EditText etSD_queryaddress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_data);
        //1.获取从MyFragment_basicMessage中传过来的值
        Intent intent=getIntent();
        String queryResultStr=intent.getStringExtra("queryresult");

        table =findViewById(R.id.table);
        btSD_exit=findViewById(R.id.btSD_exit);
        btSD_query=findViewById(R.id.btSD_query);
        etSD_queryaddress=findViewById(R.id.etSD_queryaddress);
        if(queryResultStr.equals("[]")){
            Toast.makeText(ShowDataActivity.this,"无匹配查询结果",Toast.LENGTH_LONG).show();
        }else {
            MapTableData tableData = MapTableData.create("表格名",JsonHelper.jsonToMapList(queryResultStr));
            table.setTableData(tableData);
        }

        //2.初始化地图
        initMapView();
        //3.初始化搜索模块,注册事件监听
        mSearch = GeoCoder.newInstance();
        mSearch.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
            @Override
            public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
                if (geoCodeResult == null || geoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
                    Toast.makeText(ShowDataActivity.this, "抱歉,未能找到结果", Toast.LENGTH_LONG)
                            .show();
                    return;
                }
                mBaidumap.clear();
                mBaidumap.addOverlay(new MarkerOptions().position(geoCodeResult.getLocation())
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.mipmap.icon_gcoding)));
                //加上覆盖物
                mBaidumap.setMapStatus(MapStatusUpdateFactory.newLatLng(geoCodeResult
                        .getLocation()));
                //定位
                String strInfo = String.format("纬度:%f 经度:%f",
                        geoCodeResult.getLocation().latitude, geoCodeResult.getLocation().longitude);
                Toast.makeText(ShowDataActivity.this, strInfo, Toast.LENGTH_LONG).show();
            }


            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
                if (reverseGeoCodeResult == null || reverseGeoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
                    Toast.makeText(ShowDataActivity.this, "抱歉,未能找到结果", Toast.LENGTH_LONG)
                            .show();
                    return;
                }
                mBaidumap.clear();
                mBaidumap.addOverlay(new MarkerOptions().position(reverseGeoCodeResult.getLocation())
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.mipmap.icon_gcoding)));
                //加上覆盖物
                mBaidumap.setMapStatus(MapStatusUpdateFactory.newLatLng(reverseGeoCodeResult
                        .getLocation()));
                //定位
                Toast.makeText(ShowDataActivity.this, reverseGeoCodeResult.getAddress(),
                        Toast.LENGTH_LONG).show();
                //result保存翻地理编码的结果 坐标-->城市
            }
        });

        btSD_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1=new Intent(ShowDataActivity.this,MainScreenActivity.class);
                startActivity(intent1);
            }
        });

        btSD_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取用户输入的地址
                String[] queryAddress=etSD_queryaddress.getText().toString().split("_");
                if(!queryAddress[0].equals("")){
                    mSearch.geocode(new GeoCodeOption()
                            .city(queryAddress[0])
                            .address(queryAddress[1]));
                }

            }
        });
        System.out.println(queryResultStr);
    }

    //初始化地图
    private void initMapView() {
        //1.获取地图控件引用
        mMapView = findViewById(R.id.bmapView);
        mBaidumap = mMapView.getMap();
        //2.设置初始定位点在上海
        GEO_SHANGHAI = new LatLng(31.227, 121.481);
        GEO_BEIJING = new LatLng(39.963175, 116.400244);
        mBaidumap.setMapStatus(MapStatusUpdateFactory.newLatLng(GEO_SHANGHAI));
        //3.设置地图比例尺,14级。比例尺为1000m(百度地图暂时只能放大到100m)
        mBaidumap.setMapStatus(MapStatusUpdateFactory.zoomTo(13));

    }
}

3.批量放置标记点

(1)标记点介绍

点标记用来在地图上标记任何位置,例如用户位置、车辆位置、店铺位置等一切带有位置属性的事物。

(2)java代码

//定义Maker坐标点

LatLng point = new LatLng(39.963175, 116.400244);
LatLng point1 = new LatLng(39.967175, 116.402244);
LatLng point2 = new LatLng(39.965175, 116.404244);
LatLng point3 = new LatLng(39.969175, 116.408244);
LatLng point4 = new LatLng(39.960175, 116.406244);

ArrayList<LatLng> pointList=new ArrayList<>();
pointList.add(point);
pointList.add(point1);
pointList.add(point2);
pointList.add(point3);
pointList.add(point4);

for (int i = 0; i <pointList.size() ; i++) {
     //构建Marker图标
    BitmapDescriptor bitmap = BitmapDescriptorFactory
           .fromResource(R.mipmap.icon_gcoding);
     //构建MarkerOption,用于在地图上添加Marker
    OverlayOptions option = new MarkerOptions()
            .position(pointList.get(i))
            .icon(bitmap);
      //在地图上添加Marker,并显示
     mBaidumap.addOverlay(option);
}

(3)效果图

参考文献:

https://blog.csdn.net/u013815546/article/details/45193133(android 百度地图api 地理编码&反地理编码)

https://blog.csdn.net/xiaobijia/article/details/50349865(android 百度地图定位总结)

标签:baidu,mBaidumap,LatLng,地图,new,import,Android,com,百度
来源: https://www.cnblogs.com/luckyplj/p/10732602.html