• 【Android】3.11 地理编码功能


    分类:C#、Android、VS2015、百度地图应用; 创建日期:2016-02-04

    一、简介

    地理编码指的是将地址信息建立空间坐标关系的过程,提供了地理坐标和地址之间相互转换的能力。

    地理编码分为正向地图编码和反向地图编码。

    l 正向地理编码:将中文地址或地名描述转换为地球表面上相应位置;

    l 反向地理编码:将地球表面的地址坐标转换为标准地址的过程。

    1、正向地理编码

    正向地理编码指的是由地址信息转换为坐标点的过程。

    2、反向地理编码

    反向地理编码服务实现了将地球表面的地址坐标转换为标准地址的过程。

    反向地理编码提供了坐标定位引擎,帮助用户通过地面某个地物的坐标值来反向查询得到该地物所在的行政区划、所处街道、以及最匹配的标准地址信息。通过丰富的标准地址库中的数据,可帮助用户在进行移动端查询、商业分析、规划分析等领域创造无限价值。

    反向地理编码的实现形式与正向地理编码的方式相同,此处不再赘述。(更多详细信息请参考Demo中的代码)

    二、运行截图

    简介:介绍地址信息与坐标之间的相互转换

    详述:

    (1)正向地理编码:将地址信息转换为经纬度坐标;

    (2)反向地理编码:将经纬度坐标转换为地址信息;

    本示例运行截图如下:

    image

    三、设计步骤

    1、添加demo11_geocoder.xml文件

    在layout文件夹下添加该文件,然后将代码改为下面的内容:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <EditText
                android:id="@+id/city"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="北京" />
    
            <EditText
                android:id="@+id/geocodekey"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="海淀区上地十街10号" />
    
            <Button
                android:id="@+id/geocode"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/button_style"
                android:text="Geo" />
        </LinearLayout>
    
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <EditText
                android:id="@+id/lat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="39.904965" />
    
            <EditText
                android:id="@+id/lon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="116.327764" />
    
            <Button
                android:id="@+id/reversegeocode"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/button_style"
                android:text="ReverseGeo" />
        </LinearLayout>
    
        <com.baidu.mapapi.map.TextureMapView
            android:id="@+id/bmapView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true" />
    
    </LinearLayout>

    2、添加Demo11GeoCoder.cs文件

    在SrcSdkDemos文件夹下添加该文件,然后将代码改为下面的内容:

    using Android.App;
    using Android.Content.PM;
    using Android.OS;
    using Android.Widget;
    using Com.Baidu.Mapapi.Map;
    using Com.Baidu.Mapapi.Model;
    using Com.Baidu.Mapapi.Search.Core;
    using Com.Baidu.Mapapi.Search.Geocode;
    
    namespace BdMapV371Demos.SrcSdkDemos
    {
        /// <summary>
        /// 此demo用来展示如何进行地理编码搜索(用地址检索坐标)、反地理编码搜索(用坐标检索地址)
        /// </summary>
        [Activity(Label = "@string/demo_name_geocode",
            ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden,
            ScreenOrientation = ScreenOrientation.Sensor)]
        public class Demo11GeoCoder : Activity
        {
            GeoCoder mSearch = null; // 搜索模块,也可去掉地图模块独立使用
            BaiduMap mBaiduMap = null;
            TextureMapView mMapView = null;
    
            protected override void OnCreate(Bundle savedInstanceState)
            {
    
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.demo11_geocoder);
    
                // 地图初始化
                mMapView = FindViewById<TextureMapView>(Resource.Id.bmapView);
                mBaiduMap = mMapView.Map;
    
                var btnGeocode = FindViewById<Button>(Resource.Id.geocode);
                btnGeocode.Click += (s, e) =>
                {
                    EditText editCity = FindViewById<EditText>(Resource.Id.city);
                    EditText editGeoCodeKey = FindViewById<EditText>(Resource.Id.geocodekey);
                    // Geo搜索
                    mSearch.Geocode(new GeoCodeOption()
                        .City(editCity.Text)
                        .Address(editGeoCodeKey.Text));
                };
    
                var btnReverseGeocode = FindViewById<Button>(Resource.Id.reversegeocode);
                btnReverseGeocode.Click += (s, e) =>
                {
                    EditText lat = FindViewById<EditText>(Resource.Id.lat);
                    EditText lon = FindViewById<EditText>(Resource.Id.lon);
                    LatLng ptCenter = new LatLng(float.Parse(lat.Text), float.Parse(lon.Text));
                    // 反Geo搜索
                    mSearch.ReverseGeoCode(new ReverseGeoCodeOption()
                            .Location(ptCenter));
                };
    
                // 初始化搜索模块,注册搜索结果事件
                mSearch = GeoCoder.NewInstance();
                mSearch.GetGeoCodeResult += (s, e) =>
                {
                    var result = e.P0;
                    if (result == null || result.Error != SearchResult.ERRORNO.NoError)
                    {
                        Toast.MakeText(this, "抱歉,未能找到结果", ToastLength.Long).Show();
                    }
                    mBaiduMap.Clear();
                    mBaiduMap.AddOverlay(new MarkerOptions()
                        .InvokePosition(result.Location)
                        .InvokeIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.icon_marka)));
                    mBaiduMap.SetMapStatus(MapStatusUpdateFactory.NewLatLng(result.Location));
                    string info = string.Format("纬度:{0:f6},经度:{1:f6}",
                            result.Location.Latitude, result.Location.Longitude);
                    Toast.MakeText(this, info, ToastLength.Long).Show();
                };
                mSearch.GetReverseGeoCodeResult += (s, e) =>
                {
                    var result = e.P0;
                    if (result == null || result.Error != SearchResult.ERRORNO.NoError)
                    {
                        Toast.MakeText(this, "抱歉,未能找到结果", ToastLength.Long).Show();
                    }
                    mBaiduMap.Clear();
                    mBaiduMap.AddOverlay(new MarkerOptions()
                        .InvokePosition(result.Location)
                        .InvokeIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.icon_markb)));
                    mBaiduMap.SetMapStatus(MapStatusUpdateFactory.NewLatLng(result.Location));
                    Toast.MakeText(this, result.Address, ToastLength.Long).Show();
                };
            }
    
            protected override void OnPause()
            {
                mMapView.OnPause();
                base.OnPause();
            }
    
            protected override void OnResume()
            {
                mMapView.OnResume();
                base.OnResume();
            }
    
            protected override void OnDestroy()
            {
                mMapView.OnDestroy();
                mSearch.Destroy();
                base.OnDestroy();
            }
        }
    }

    3、修改MainActivity.cs

    在MainActivity.cs文件的demos字段定义中,去掉【示例11】下面的注释。

    运行观察结果。

  • 相关阅读:
    导出excel
    JS一些记录
    Concat
    (二)《SQL进阶教程》学习记录--GROUP BY、PARTITION BY
    PostgreSQL 时间转换
    vlc+flv.js 摄像头 H5 直播
    echarts label formatter params backgroundColor rich 标签设置背景图并传参
    异步、多线程、Await/Async、Task
    “2+3”等于我的自白
    SignalR:React + ASP.NET Core Api
  • 原文地址:https://www.cnblogs.com/rainmj/p/5181486.html
Copyright © 2020-2023  润新知