GMap.NET地名搜索

 

GMap中地址搜索主要用到GeocodingProvider。GeocodingProvider的方法如下:

      GeoCoderStatusCode GetPoints(string keywords, out List<PointLatLng> pointList); //根据关键字得到一组坐标

      PointLatLng? GetPoint(string keywords, out GeoCoderStatusCode status);//根据关键字得到一个坐标

      // ...

      GeoCoderStatusCode GetPlacemarks(PointLatLng location, out List<Placemark> placemarkList);//根据坐标得到地址

      Placemark GetPlacemark(PointLatLng location, out GeoCoderStatusCode status);//根据坐标得到一组地址

对google api比较熟悉的同学对PlaceMark很熟悉吧!!

 

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GMap.NET.MapProviders;
using GMap.NET;
using GMap.NET.WindowsForms.Markers;
using GMap.NET.WindowsForms;

namespace Demo.WindowsForms.YJQDemos
{
    public partial class SearchAddress : Form
    {
        private GMapOverlay searchResultLayer = new GMapOverlay("SearchResultLayer");
        private List<PointLatLng> searchResult = null;

        public SearchAddress()
        {
            InitializeComponent();
        }

        private void BTNSearchAddress_Click(object sender, EventArgs e)
        {
            String searcheKey = this.TBSAdress.Text.Trim();
            GeocodingProvider gp = this.MapControl.MapProvider as GeocodingProvider;
            if (gp == null)
            {
                gp = GMapProviders.OpenStreetMap as GeocodingProvider;
            }
            
            searchResult = new List<PointLatLng>();
            gp.GetPoints(searcheKey, out searchResult);
            this.LBXSearchResult.Items.Clear();
            this.searchResultLayer.Clear();
            this.LBXSearchResult.Visible = true;
            foreach (PointLatLng pt in searchResult)
            {
               // this.LBXSearchResult.Items.Add(String.Format("{0},{1}", pt.Lng, pt.Lat));
                GMarkerGoogle marker = new GMarkerGoogle(pt, GMarkerGoogleType.arrow);
                searchResultLayer.Markers.Add(marker);
                GeoCoderStatusCode placeMarkResult = new GeoCoderStatusCode();
                Placemark place = gp.GetPlacemark(pt, out placeMarkResult);
                this.LBXSearchResult.Items.Add(place.Address);
            }

            this.MapControl.ZoomAndCenterMarkers(searchResultLayer.Id);
        }

        private void MapControl_Load(object sender, EventArgs e)
        {

        }

        private void SearchAddress_Load(object sender, EventArgs e)
        {
            this.MapControl.MapProvider = GMapProviders.GoogleChinaMap;
            this.MapControl.MapProvider.MinZoom = 1;
            this.MapControl.MapProvider.MaxZoom = 17;
            this.MapControl.DragButton = System.Windows.Forms.MouseButtons.Left;
            this.MapControl.Overlays.Add(searchResultLayer);
            GMapProvider.Language = LanguageType.ChineseSimplified;
        }

        private void LBXSearchResult_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.LBXSearchResult.SelectedIndex < 0)
            {
                return;
            }
            searchResultLayer.Clear();
            GMarkerGoogle marker = new GMarkerGoogle(this.searchResult[this.LBXSearchResult.SelectedIndex], GMarkerGoogleType.arrow);
            searchResultLayer.Markers.Add(marker);
            this.MapControl.Position = this.searchResult[this.LBXSearchResult.SelectedIndex];
        }

       
    }
}

  

效果:

 

 

posted @ 2012-12-10 00:18  如坐夕阳  Views(972)  Comments(1Edit  收藏  举报