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;
}
}
}
}
}
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;
}
}
}
}
}
<sys>
<key1>7bf97874e7486e04b8556eab41117713</key1>
<key2>A8a4c484282b5c902f4800a3b732c3bd</key2>
<key3>D18b1faa880ba25c96044f2bbacbfc52</key3>
</sys>
没用过百度地图api,完整的调用参数都是什么,给出可以测试的值int lastdisindex = str.LastIndexOf("distance");
包含 distance的这段返回的完整json是什么样的
{
"status":2,
"message":"Request Parameter Error:ak"
}
而且状态为0时返回的json里根本没有“distance”,看下面返回的status==0时的数据,哪来的distance{
"status":0,
"message":"ok",
"type":1,
"info":{
"copyright":{
"text":"@2013 Baidu - Data",
"imageUrl":"http://api.map.baidu.com/images/copyright_logo.png"
}
},
"result":{
"originInfo":{
"area_id":131,
"wd":"",
"uid":""
},
"destinationInfo":{
"area_id":131,
"wd":"",
"uid":""
},
"origin":{
"cityName":"\u5317\u4eac\u5e02",
"listType":0
},
"destination":{
"cityName":"\u5317\u4eac\u5e02",
"listType":0
}
}
}
整个项目源码
你项目源码也只是这一个类带2个静态函数而已,需要知道返回的带distance的json数据是什么格式,才好进行分析处理然后用delphi写,问题是根本就没有这个distance,猜测你是想从两个地点来获取它们之间的距离吧?由于你的代码不是按json格式来解析,是简单的分析字符串而已,没法直接转,得明白逻辑然后用delphi改写。
我用delphi写个测试
TBaiduApi.GetDistance('海淀', '北京', '丰台', '北京');
调用得到这大段json,里面也没有distance数据,所以没法继续写下去了,{"message":"ok","status":0,"result":{"originInfo":{"uid":"","area_id":131,"wd":"海淀"},"destinationInfo":{"uid":"","area_id":131,"wd":"丰台"},"origin":{"cityName":"北京市","content":[{"uid":"47d8390458ee9582601c6785","location":{"lng":116.31186978466,"lat":39.992279996524},"telephone":null,"address":"北京市海淀区","name":"海淀路"},{"uid":"5e53af1a5e5f305f4051c0ac","location":{"lng":116.31928529665,"lat":39.991695292986},"telephone":null,"address":"北大南门正对面北四环边上","name":"海淀路社区"},{"uid":"113f3fdab4becfaea3a93428","location":{"lng":116.3090627596,"lat":39.990688253346},"telephone":null,"address":"北京市海淀区","name":"海淀大街"},{"uid":"e2c734e41875264415a1907f","location":{"lng":116.31318759885,"lat":39.990734825977},"telephone":"(010)62565648","address":"海淀区海淀大街25号海淀图书城昊海楼西门对面","name":"海淀图书城(海淀大街)"},{"uid":"eed7447c0d63c8fc28d7ab98","location":{"lng":116.31505472685,"lat":39.981740255584},"telephone":null,"address":"304路;304路;307路;307路;386路;386路;611路;611路;630路;630路;671路;671路;944路;944路;运通109线;运通109线","name":"海淀南路"},{"uid":"75715cc431518b8cc27da28e","location":{"lng":116.31764094841,"lat":39.98339633962},"telephone":null,"address":"681路;681路","name":"海淀中街"},{"uid":"0edf38a9ad2e8a57f3193728","location":{"lng":116.29916460068,"lat":39.996530657533},"telephone":null,"address":"北京市海淀区","name":"海淀公园路"},{"uid":"fa01e9371a040053774ff1ca","location":{"lng":116.32201794201,"lat":39.982083099537},"telephone":"01062583042","address":"北京市海淀区中关村大街29号(海淀黄庄路口)","name":"海淀医院"},{"uid":"5211d2192055af597dc8765b","location":{"lng":116.31458428425,"lat":39.988523627161},"telephone":null,"address":"北京市海淀区","name":"海淀大街\/四环(路口)"},{"uid":"f3911cf818136bb349fd3f98","location":{"lng":116.30855485767,"lat":39.981005011516},"telephone":null,"address":"北京市海淀区","name":"万泉河路\/海淀南路(路口)"},{"uid":"5e58b27a0a9c82456c476003","location":{"lng":116.31596983067,"lat":39.988489076607},"telephone":null,"address":"北京市海淀区","name":"海淀大街\/海淀西街(路口)"},{"uid":"5ffb18160d7e5c5e6f476055","location":{"lng":116.31944806961,"lat":39.981896508606},"telephone":null,"address":"北京市海淀区","name":"海淀东三街\/海淀南路(路口)"},{"uid":"3263e8836ba7e7e2df1e526d","location":{"lng":116.31781198578,"lat":39.969027406134},"telephone":"(010)88414810,13910859304","address":"海淀区苏州桥东南角","name":"海淀驾校"},{"uid":"c8e8f63304358475ab0dd859","location":{"lng":116.32519803334,"lat":39.982559940693},"telephone":"(010)82533588","address":"中关村大街28号","name":"海淀剧院"},{"uid":"35456bf5907a1133c42ba1a5","location":{"lng":116.31186295754,"lat":39.993648177557},"telephone":null,"address":"302路;332路;332路;333内;333外;394路;394路;608路;608路;718路;718路;运通106线;运通106线;运通114线;运通114线;运通118线;运通118线;运通124线;运通124线","name":"海淀桥北"},{"uid":"1277334c9a68fcc71ce9998a","location":{"lng":116.30524172729,"lat":39.996939271861},"telephone":null,"address":"384路;384路;384路区间;384路区间;482路;482路;534路;534路;563路;563路;671路;671路;968路;968路;特10路;特10路;特5路;特5路;运通108线;运通108线","name":"海淀公园"},{"uid":"e90784153553d15f43e192d7","location":{"lng":116.32042955821,"lat":39.992399949261},"telephone":null,"address":"209路夜班车;302路;332路","name":"海淀路东口"},{"uid":"d1b77be6fbe5fe527fc87608","location":{"lng":116.31463683512,"lat":39.992272534019},"telephone":null,"address":"209路夜班车;209路夜班车;302路;332路","name":"海淀路西口"},{"uid":"34059f97e1f7935470455bb2","location":{"lng":116.30488096779,"lat":39.965522882638},"telephone":"(010)62523089","address":"长春桥路17号","name":"海淀区政协"},{"uid":"6835e3a7f7b29057e311031b","location":{"lng":116.30950544456,"lat":39.993249976918},"telephone":"(010)62642626","address":"颐和园路12号海淀体育中心院内","name":"海淀游泳馆"},{"uid":"c1653405cea17479aa175aae","location":很长还有,超出帖子允许长度了
http://www.cnblogs.com/del/archive/2009/10/22/1588384.html
你给个北京站和北京西站这样就可以了