用baidu的api勉强写个查询两个地点之间的驾车距离。但现在需要改成dephi的。以前从来没写个dephi。现在项目有很着急,求大虾们帮帮忙

解决方案 »

  1.   

    项目地址
    http://download.csdn.net/download/cmy_ymc/5990727
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.IO;
    using System.Xml;
    using System.Xml.Linq;
    using System.Windows.Forms;namespace WindowsFormsApplication11
    {
        class BaiduApi
        {
            public static string GetDistance(string rregion, string rcity, string dregion, string dcity)
            {
                string str = "";
                try
                {
                    XDocument docbaidudis = XDocument.Load(Application.StartupPath + "\\baidudis.XML");
                    int count = int.Parse(creatconxml());
                    WebRequest request = WebRequest.Create("http://api.map.baidu.com/direction/v1?mode=driving&origin=" + rregion + "&destination=" + dregion + "&origin_region=" + rcity + "&destination_region=" + dcity + "&output=json&ak=" + docbaidudis.Element("sys").Element("key" + count.ToString()).Value);
                    WebResponse response = request.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
                    str = reader.ReadToEnd();
                    reader.Close();
                    reader.Dispose();
                    response.Close();
                    int statusindex = str.IndexOf("status");
                    string status = str.Substring(statusindex + 8, 1);
                    if (status == "0")
                    {
                        int lastdisindex = str.LastIndexOf("distance");
                        string distance = str.Substring(lastdisindex + 10, str.IndexOf(",", lastdisindex) - lastdisindex);
                        return distance.Remove(distance.IndexOf(','));
                    }
                    else if (status == "2")
                    {
                        return status;
                    }
                    else//(status == "5")
                    {
                        XDocument docbaiducount = XDocument.Load(Application.StartupPath + "\\baiducount.XML");
                        docbaiducount.Element("sys").Element("key").Value = (int.Parse(docbaiducount.Element("sys").Element("key").Value) + 1).ToString();
                        docbaiducount.Save("baiducount.xml");
                        return GetDistance(rregion, rcity, dregion, dcity);
                    }
                }
                catch (Exception tt)
                {
                    return "0";
                }
            }
            private static string creatconxml()
            {
                if (!File.Exists("baiducount.xml"))
                {
                    XmlWriter writer = null;
                    try
                    {
                        writer = XmlWriter.Create("baiducount.xml");
                        writer.WriteStartElement("sys");
                        writer.WriteElementString("date", System.DateTime.Today.ToShortDateString());
                        writer.WriteElementString("key", "1");
                        writer.WriteEndDocument();
                        writer.Flush();
                    }
                    finally
                    {
                        if (writer != null)
                            writer.Close();
                    }
                    return "1";
                }
                else
                {
                    XDocument doc = XDocument.Load(Application.StartupPath + "\\baiducount.XML");
                    string datetime = doc.Element("sys").Element("date").Value;
                    if (Convert.ToDateTime(datetime).Date != System.DateTime.Today)
                    {
                        doc.Element("sys").Element("date").Value = System.DateTime.Today.ToShortDateString();
                        doc.Element("sys").Element("key").Value = "1";
                        doc.Save("baiducount.XML");
                        return "1";
                    }
                    else
                    {
                        return doc.Element("sys").Element("key").Value;
                    }
                }
            }
        }
    }
      

  3.   

    <?xml version="1.0" encoding="utf-8"?>
    <sys>
      <date>2013-09-03</date>
      <key>1</key>
    </sys>
    <?xml version="1.0" encoding="utf-8"?>
    <sys>
      <key1>7bf97874e7486e04b8556eab41117713</key1>
      <key2>A8a4c484282b5c902f4800a3b732c3bd</key2>
      <key3>D18b1faa880ba25c96044f2bbacbfc52</key3>
    </sys>因为百度的key是有使用限制的,一个key一天只能用1000次
    两个xml文件是用来控制key的使用的。当一个key使用超过1000,就会转到下个key