android 怎么批量调用百度地图的地理编码功能

如题所述

如何批量调用百度地图的地理编码功能,以下是具体步骤:1.打开百度地图开放平台,注册账号,并创建应用。2.在创建的应用中,开启地理编码API的权限。3.在自己的Android工程中,引入百度地图SDK。4.在代码中进行调用地理编码API的操作,可以使用HttpURLConnection或者HttpClient发送HTTP请求。请求的URL如下所示:http://api.map.baidu.com/geocoder/v2/?address=地址\u0026output=json\u0026ak=你的AK其中,address为要进行地理编码的地址;output为返回格式(json或xml);ak为你创建应用时生成的AK。在这个URL中,address可以是单个地址,也可以是多个地址用分号隔开的组合。例如:http://api.map.baidu.com/geocoder/v2/?address=北京市;上海市\u0026output=json\u0026ak=你的AK这样就可以同时对北京市和上海市进行地理编码了。5.将获取到的结果进行解析,可以使用Gson或者FastJson等第三方JSON库进行解析。通过以上的步骤,就可以批量调用百度地图的地理编码功能了。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-01-02

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

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

<com.baidu.mapapi.map.MapView
    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

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical" >
    <ImageView
        android:id="@+id/iv_title"
        android:layout_width="42dp"
        android:layout_height="42dp"
         />
    <TextView
        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" /></LinearLayout>

本回答被网友采纳
第2个回答  2019-01-02
在当时实习的项目经理(于老师)的指导下我才明白什么意思,代码也都是他写的,特此声明。
界面设计:

使用说明:在上面两个Text文本框分别输入纬度和纬度,第三个Text文本框会输出地址;
在上面两个Text文本框分别输入地址,第三个Text文本框会输出纬度和纬度;
点击"地理编码"后触发事件:

private void button2_Click(object sender, EventArgs e)
{
string url = "http://api.map.baidu.com/geocoder/v2/?ak=Y4NcP7YcxEkiwSuYedq5vW09&output=json&address=" + this.textBox2.Text + "&city=" + this.textBox1.Text;

WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url);
StreamReader sr = new StreamReader(stream);
string strLine = "";
while ((strLine = sr.ReadLine()) != null)
{
JsonObject js = JsonObject.Parse(strLine) as JsonObject;
if (js["status"].ToString()=="0")
{
string result = "经度:";
result += js["result"]["location"]["lat"];
result += ",纬度:" + js["result"]["location"]["lng"];
this.textBox4.Text = result;

}

}
sr.Close();
}
说明:使用Json格式传输数据,调用 Geocoding APIWeb服务API需要百度LBS开发平台申请秘钥,链接如下:http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding
点击"逆地理编码"后触发事件:

private void button1_Click(object sender, EventArgs e)
{
string url = "http://api.map.baidu.com/geocoder/v2/?ak=Y4NcP7YcxEkiwSuYedq5vW09&output=json&location=" + this.textBox1.Text + "," + this.textBox2.Text + "&pois=0";

WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url);
StreamReader sr = new StreamReader(stream);
string strLine = "";
while ((strLine = sr.ReadLine()) != null)
{
JsonObject js = JsonObject.Parse(strLine) as JsonObject;
if (js["status"].ToString() == "0")
{
string result = "地址:" + js["result"]["formatted_address"];
this.textBox4.Text = result;
}
}
sr.Close();
}
}
相似回答