【高德地图API】地理编码与逆地理编码

一、地理编码

该功能实现地理编码服务,即地址匹配,从已知的地址描述到对应的经纬度坐标的转换,即根据地址信息,查询该地址所对应的点坐标等,地址(address) 为必选项,城市(city)为可选项。

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <Grid Opacity="0.8"  Margin="0,0,0,608"  Background="#FF323232"  RenderTransformOrigin="0.497,0.465" Canvas.ZIndex="10" >

                <Grid.RowDefinitions>

                    <RowDefinition Height="Auto"/>

                    <RowDefinition Height="Auto"/>

                </Grid.RowDefinitions>

                <TextBlock Grid.Row="0" Text="地理编码" Margin="3,0,0,0" />

                <StackPanel Grid.Row="1"  Orientation="Vertical">

                    <TextBox x:Name="txtAddress" Grid.Row="0" Text="北京市朝阳区望京阜通东大街方恒国际中心" Margin="12,0,0,0" />

                    <Button   Content="地理编码" Click="Button_Click" Margin="274,0,26,0"  />

                </StackPanel>

            </Grid>

        </Grid>
View Code

 

        AMap amap;

        AMapMarker marker;

        public SearchGeoCode()

        {

            InitializeComponent();

            this.ContentPanel.Children.Add(amap = new AMap());

            amap.MarkerClickListener += amap_MarkerClickListener;

          

        }



        private async Task AddressToGeoCode(string address)

        {

            AMapGeoCodeResult cr = await AMapGeoCodeSearch.AddressToGeoCode(address);

            this.Dispatcher.BeginInvoke(() =>

            {

                if (cr.Erro == null && cr.GeoCodeList != null)

                {

                    if (cr.GeoCodeList.Count==0)

                    {

                        MessageBox.Show("无查询结果");

                        return;

                    }

                    IEnumerable<AMapGeoCode> geocode = cr.GeoCodeList;

                    foreach (AMapGeoCode gcs in geocode)

                    {

                        Debug.WriteLine(gcs.Adcode);

                        Debug.WriteLine(gcs.Building);

                        Debug.WriteLine(gcs.City);

                        Debug.WriteLine(gcs.District);

                        Debug.WriteLine(gcs.FormattedAddress);

                        Debug.WriteLine(gcs.Province);

                        Debug.WriteLine(gcs.Township);

                        Debug.WriteLine(gcs.Location.Lon);

                        Debug.WriteLine(gcs.Location.Lat);

                        Debug.WriteLine(gcs.LevelList[0]);

                        marker= amap.AddMarker(new AMapMarkerOptions()

                        {

                            Position = new LatLng(gcs.Location.Lat, gcs.Location.Lon),

                            Title = gcs.FormattedAddress,

                            Snippet = gcs.District,

                            IconUri = new Uri("Images/AZURE.png", UriKind.Relative),



                        });

                    }

                    //如果返回的geocode数大于1个,调整视图

                    if (geocode.Count()>1)

                    {

                        LatLngBounds.Builder builder = new LatLngBounds.Builder();

                        List<AMapMarker> markers = amap.GetMapMarkers();

                        foreach (AMapMarker marker in markers)

                        {

                            builder.Include(marker.Position);

                        }

                        this.amap.MoveCamera(CameraUpdateFactory.NewLatLngBounds(builder.Build(), markers.Count()));

                    }

                    else

                    {

                        amap.MoveCamera(CameraUpdateFactory.NewLatLngZoom(new LatLng(geocode.FirstOrDefault().Location.Lat, geocode.FirstOrDefault().Location.Lon), 14));

                    }

                }

                else

                {

                    MessageBox.Show(cr.Erro.Message);

                }



            });

        }



        void amap_MarkerClickListener(AMapMarker sender, AMapEventArgs args)

        {

            sender.ShowInfoWindow(new AInfoWindow()

            {

                Title = sender.Title,

                ContentText =sender.Snippet,

            });

        }



        private async void Button_Click(object sender, RoutedEventArgs e)

        {

            amap.Clear();

            if (!string.IsNullOrWhiteSpace(txtAddress.Text))

            {

                await AddressToGeoCode(txtAddress.Text);

            }

            

        }        

二、逆地理编码

该功能实现逆地理编码服务,即地址解析服务,具体是指从已知的经纬度坐标到对应的地址描述(如省市、街区、楼层、房间等)的转换服务,坐标(location) 为必选项,半径(radius)为可选项,详细的参数说明参见参考手册。

 

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <Grid Opacity="0.8" Margin="0,0,0,568"  Background="#FF323232"  RenderTransformOrigin="0.497,0.465" Canvas.ZIndex="10" >

                <Grid.RowDefinitions>

                    <RowDefinition Height="Auto"/>

                    <RowDefinition Height="Auto"/>

                </Grid.RowDefinitions>

                <TextBlock Grid.Row="0" Text="逆地理编码" Margin="3,0,0,0" />

              

                    <Grid  Grid.Row="1">

                        <Grid.RowDefinitions>

                            <RowDefinition Height="Auto"/>

                            <RowDefinition Height="Auto"/>

                        <RowDefinition Height="Auto"/>

                    </Grid.RowDefinitions>

                        <Grid.ColumnDefinitions>

                            <ColumnDefinition Width="Auto"/>

                            <ColumnDefinition Width="Auto"/>

                        </Grid.ColumnDefinitions>

                    <TextBlock Grid.Row="0" Grid.Column="0" Text="经度:" Margin="12,0,0,0" VerticalAlignment="Center" />

                    <TextBlock Grid.Row="1" Grid.Column="0" Text="纬度:" Margin="12,0,0,0" VerticalAlignment="Center" />

                    <TextBox x:Name="txtLon" Grid.Row="0" Grid.Column="1" Text="" Width="180" />

                    <TextBox x:Name="txtLat" Grid.Row="1" Grid.Column="1" Text="" Width="180" />

                    <TextBlock  Grid.Row="2" Text="点击或者输入获得经纬度" Grid.ColumnSpan="2" Margin="3,0,0,0" />

                </Grid>

                    <Button   Content="逆地理编码" Click="Button_Click" Margin="257,69,29,3" Grid.Row="1"  />

             

            </Grid>
View Code

 

        AMap amap;

        AMapMarker marker;

        LatLng latLng;

        public SearchReGeoCode()

        {

            InitializeComponent();

            this.ContentPanel.Children.Add(amap = new AMap());

            amap.Tap += amap_Tap;

            amap.MarkerClickListener += amap_MarkerClickListener;



        }



        void amap_Tap(object sender, System.Windows.Input.GestureEventArgs e)

        {

            latLng = amap.GetProjection().FromScreenLocation(e.GetPosition(amap));

            this.txtLat.Text = latLng.latitude.ToString();

            this.txtLon.Text = latLng.longitude.ToString();

        }



        private async Task GeoCodeToAddress(double lon, double lat)

        {

            AMapReGeoCodeResult rcc = await AMapReGeoCodeSearch.GeoCodeToAddress(lon, lat, 500, "", Extensions.All);



            this.Dispatcher.BeginInvoke(() =>

                {

                    if (rcc.Erro == null && rcc.ReGeoCode != null)

                    {

                        AMapReGeoCode regeocode = rcc.ReGeoCode;

                        Debug.WriteLine(regeocode.Address_component);

                        Debug.WriteLine(regeocode.Formatted_address);

                        Debug.WriteLine(regeocode.Pois);



                        List<AMapPOI> pois = regeocode.Pois.ToList();

                        //POI信息点

                        foreach (AMapPOI poi in pois)

                        {

                            marker = amap.AddMarker(new AMapMarkerOptions()

                            {

                                Position = new LatLng(poi.Location.Lat, poi.Location.Lon),

                                Title = poi.Name,

                                Snippet = poi.Address,

                                IconUri = new Uri("Images/RED.png", UriKind.Relative),



                            });

                        }



                        Debug.WriteLine(regeocode.Roadinters);

                        Debug.WriteLine(regeocode.Roadslist);

                        AMapAddressComponent addressComponent = regeocode.Address_component;

                        Debug.WriteLine(addressComponent.Building);

                        Debug.WriteLine(addressComponent.City);

                        Debug.WriteLine(addressComponent.District);

                        Debug.WriteLine(addressComponent.Neighborhood);

                        Debug.WriteLine(addressComponent.Province);

                        Debug.WriteLine(addressComponent.Stree_number);

                        Debug.WriteLine(addressComponent.Township);

                        AMapStreetNumber streetNumber = addressComponent.Stree_number;

                        Debug.WriteLine(streetNumber.Direction);

                        Debug.WriteLine(streetNumber.Distance);

                        Debug.WriteLine(streetNumber.Location.Lat);

                        Debug.WriteLine(streetNumber.Location.Lon);

                        Debug.WriteLine(streetNumber.Number);

                        Debug.WriteLine(streetNumber.Street);





                        marker = amap.AddMarker(new AMapMarkerOptions()

                          {

                              Position = new LatLng(streetNumber.Location.Lat, streetNumber.Location.Lon),//amap.Center,//

                              Title = addressComponent.Province,

                              Snippet = regeocode.Formatted_address,

                              IconUri = new Uri("Images/AZURE.png", UriKind.Relative),



                          });



                        //显示化弹出信息

                        AInfoWindow info = new AInfoWindow();

                        info.Title = addressComponent.Province;

                        info.ContentText = regeocode.Formatted_address;

                        marker.ShowInfoWindow(info, new Point(0, 0));



                        amap.MoveCamera(CameraUpdateFactory.NewLatLngZoom(new LatLng(Convert.ToDouble(txtLon.Text), Convert.ToDouble(txtLat.Text)), 12));

                    }

                    else

                    {

                        MessageBox.Show(rcc.Erro.Message);

                    }



                });



        }



        void amap_MarkerClickListener(AMapMarker sender, AMapEventArgs args)

        {

            sender.ShowInfoWindow(new AInfoWindow()

            {

                Title = sender.Title,

                ContentText = sender.Snippet,

            });

        }



        private async void Button_Click(object sender, RoutedEventArgs e)

        {

            amap.Clear();

            if (string.IsNullOrWhiteSpace(txtLat.Text) && string.IsNullOrWhiteSpace(txtLon.Text))

            {

                return;

            }

            await GeoCodeToAddress(Convert.ToDouble(txtLon.Text), Convert.ToDouble(txtLat.Text));

        }    

 

三、同时使用地理编码和逆地理编码

已知一个地址或者模糊地址,然后你想知道该地址详细信息或者周边信息(周边POI点)。先通过地址获取经纬度,然后通过逆地理编码获取详细信息。在此不作出示例。

四、批量逆地理编码

目前高德地图WP SDK中并没有提供直接批量转换的接口,而在REST API中已经提供了,你可以在开发者论坛REST API版块中提出接口使用申请,链接:高德地图rest api接口申请方式 

 

 

 

你可能感兴趣的:(api)