开发你的LBS应用,使用Google地图引擎,根据地理位置获取纬度、经度。

比较简单,直接上代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
using Tiwer.Config;

namespace Tiwer.Data
{
    public class TGoogleMap
    {
        private string address;
        private string url;

        private string latitude;
        /// <summary>
        /// 纬度
        /// </summary>
        public string Latitude
        {
            get { return latitude; }
        }

        private string longitude;
        /// <summary>
        /// 经度
        /// </summary>
        public string Longitude
        {
            get { return longitude; }
        }

        public DGoogleMap(string location)
        {
            SetAddress(location); 
            GetMapInfo();
        }

        /// <summary>
        /// 获取经度
        /// </summary>
        /// <param name="location">地址</param>
        /// <returns>经度</returns>
        public string GetLongtitude(string location)
        {
            GetMapInfo();
           
            return longitude;
        }

        /// <summary>
        /// 获取纬度
        /// </summary>
        /// <param name="location">地址</param>
        /// <returns>纬度</returns>
        public string GetLatitude( string location)
        {
            SetAddress(location);
            GetMapInfo();

            return latitude;
        }
        
        /// <summary>
        /// 获取地图经纬信息
        /// </summary>
        private void GetMapInfo()
        {
            HttpWebRequest searchRequest = (HttpWebRequest)WebRequest.Create(@url);
            searchRequest.ServicePoint.Expect100Continue = false;

            WebResponse myresponse = searchRequest.GetResponse();
            Stream responseStream = myresponse.GetResponseStream();            

            byte[] buffer = new byte[99];
            responseStream.Read(buffer, 0, (int)99);

            myresponse.Close();

            string results = System.Text.Encoding.ASCII.GetString(buffer);
            string[] arr = results.Split(',');
            
            latitude = arr[2].ToString();
            longitude = arr[3].ToString().Substring(0, 10);
        }

        /// <summary>
        /// 设置地址
        /// </summary>
        /// <param name="location">地址</param>
        private void SetAddress(string location)
        {
            IConfigManager _iconfig = ConfigManagerFactory.CreateConfigManager("general");
            string _key = _iconfig.GetElementValue("GoogleMapKey")["GoogleMapKey"];

            address = HttpUtility.UrlEncode(location);
            url = "http://203.208.39.99/maps/geo?q=" + address + "&output=csv&oe=utf8&sensor=true_or_false&key=" + _key;
        }

        /// <summary>
        /// 获取经度
        /// </summary>
        /// <param name="location">地址</param>
        /// <returns>经度</returns>
        public static string DGetLongtitude(string location)
        {
            DGoogleMap map = new DGoogleMap(location);
            return map.Longitude;
        }
        
        /// <summary>
        /// 获取纬度
        /// </summary>
        /// <param name="location">地址</param>
        /// <returns>纬度</returns>
        public static string DGetLatitude(string location)
        {
            DGoogleMap map = new DGoogleMap(location);
            return map.Latitude;
        }
    }
}

posted @ 2011-03-24 14:16  wgw8299  阅读(1225)  评论(0编辑  收藏  举报