使用Google api进行地点搜索 [英] Near by place search using Google api
问题描述
我使用google api在附近搜索
https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants + in + Sydney& key = YOUR_API_KEY
我只想要json格式的结果附近信息。
请告知我如何过滤结果并仅显示json格式的附近(地址)信息。
String sURL =https://maps.googleapis.com/maps/api/place/nearbysearch/json?location= ;
sURL + = objNearByServicesRequest.Latitude +,+ objNearByServicesRequest.Longitude;
sURL + =& radius =+ WebConfigurationManager.AppSettings [RadiusNearBySearch] +& types =+ objNearByServicesRequest.Types +& keyword =+ objNearByServicesRequest.Keyword;
sURL + =& key =+ WebConfigurationManager.AppSettings [GoogleAPIKeyNearBySearch];
String strreturn = String.Empty;
使用(var client = new WebClient())
using (var stream = client.OpenRead(sURL))
using(var reader = new StreamReader(stream))
{
var jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadToEnd());
Console.WriteLine((string)jObject [results] [0] [nearby]);
}
我想在json中展示出来2字段的格式如下所示。
如何使用C#编写以下格式显示的代码。
[
{
id:1,
附近:Opp。火车站,Gabba Estate,Gohar Baug,孟买,
},
{
id :2
附近:SH 15,Navjivan Colony,Mumbai,
}
]
我尝试过:
> https://maps.googleapis.com/maps/api/place/textsearch/xml?query = restaurants + in + Sydney& key = YOUR_API_KEY
我只希望下面的附近信息以json格式生成。
请告知我如何过滤结果和仅显示json格式的邻近(地址)信息。
I search near by palce using google api
https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+in+Sydney&key=YOUR_API_KEY
I want only vicinity information from result in json format.
Please advise how can I filter result and display only vicinity(address) information in json format.
String sURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=";
sURL += objNearByServicesRequest.Latitude + "," + objNearByServicesRequest.Longitude;
sURL += "&radius=" + WebConfigurationManager.AppSettings["RadiusNearBySearch"] + "&types=" + objNearByServicesRequest.Types + "&keyword=" + objNearByServicesRequest.Keyword;
sURL += "&key=" + WebConfigurationManager.AppSettings["GoogleAPIKeyNearBySearch"];
String strreturn = String.Empty;
using (var client = new WebClient())
using (var stream = client.OpenRead(sURL))
using (var reader = new StreamReader(stream))
{
var jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadToEnd());
Console.WriteLine((string)jObject["results"][0]["vicinity"]);
}
I want to display out out in json format for 2 field like below.
how can I write code for display out put in below format using C#.
[
{
"id": 1,
"vicinity": "Opp. Railway Station, Gabba Estate,, Gohar Baug, Mumbai",
},
{
"id": 2
"vicinity": "SH 15, Navjivan Colony, Mumbai",
}
]
What I have tried:
>https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+in+Sydney&key=YOUR_API_KEY
I want only vicinity information from below result in json format.
Please advise how can I filter result and display only vicinity(address) information in json format.
推荐答案
最可能的问题是响应是多行的,但你只是阅读第一行。
使用ReadToEnd
而不是<$更改代码以读取整个响应c $ c> ReadLine :
The most likely issue is that the response is on multiple lines, but you're only reading the first line.
Either change your code to read the entire response, usingReadToEnd
instead ofReadLine
:
using (var client = new WebClient())
using (var stream = client.OpenRead(sURL))
using (var reader = new StreamReader(stream))
{
var jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadToEnd());
Console.WriteLine((string)jObject["results"][0]["vicinity"]);
}
或使用 JsonReader
加载数据:
Or use a JsonReader
to load the data:
using (var client = new WebClient())
using (var stream = client.OpenRead(sURL))
using (var reader = new StreamReader(stream))
using (var json = new JsonTextReader(reader))
{
var jObject = Newtonsoft.Json.Linq.JObject.Load(json);
Console.WriteLine((string)jObject["results"][0]["vicinity"]);
}
这篇关于使用Google api进行地点搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!