[英]Retrievin GPS/DMS Coordinates from Google
I have some code in an ASP .Net program which makes a request to Google map API and retrieves the co-ordinates as LAT/LNG我在 ASP .Net 程序中有一些代码,它向 Google 地图 API 发出请求并将坐标检索为 LAT/LNG
string url = https://maps.google.com/maps/api/geocode/xml?address=ABC DEF,UK&sensor=false&key=AI123ZYX
WebRequest request = WebRequest.Create(url);
using (WebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
// removed for brevity
// Returns LAT/LNG values
}
}
This works as expected but i cant workout how to convert or retrieve the DMS (Degree, Minutes, Second eg 1° 23' 45" W 67° 89' 10" N) value.这按预期工作,但我无法练习如何转换或检索 DMS(度、分钟、秒,例如 1° 23' 45" W 67° 89' 10" N)值。
I've checked Googles documentation but everything points at a phone or web which you enter values or the address and it would display it.我已经检查了 Google 的文档,但所有内容都指向您输入值或地址的电话或网络,它会显示它。
Im trying to do the same but programatically so i could store the LAT/LNG along with the DMS (or maybe known as GPS) co-ordinates.我试图以编程方式做同样的事情,这样我就可以将 LAT/LNG 与 DMS(或可能称为 GPS)坐标一起存储。 How do i retrieve these co-ordinates?我如何检索这些坐标?
So it looks like a query to the Google Maps API will return you a JSON object (SRC: https://stackoverflow.com/a/34597393/8502032 ).所以看起来对 Google Maps API 的查询会返回一个 JSON 对象(SRC: https : //stackoverflow.com/a/34597393/8502032 )。 C# doesn't handle JSON natively, so you can use Newtonsoft's Json.NET library to help you parse the object into something more workable. C# 本身不处理 JSON,因此您可以使用 Newtonsoft 的 Json.NET 库来帮助您将对象解析为更可行的内容。
You can access the information from there in a few ways.您可以通过几种方式从那里访问信息。 One is using Json.net's deserialize function.一种是使用 Json.net 的反序列化功能。 This will take the JSON string and interpolate it into a useable object.这将获取 JSON 字符串并将其插入到一个可用的对象中。 You can make a custom object that will grab the sections of JSON that you care about, and then drop the rest.您可以创建一个自定义对象,该对象将抓取您关心的 JSON 部分,然后删除其余部分。
This comes from the answer I linked, and we'll use that as our dataset that we're trying to access.这来自我链接的答案,我们将使用它作为我们尝试访问的数据集。
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 34.1330949,
"lng" : -117.9143879
},
"southwest" : {
"lat" : 33.8068768,
"lng" : -118.3527671
}
},
"copyrights" : "Map data ©2016 Google",
"legs" : [
{
"distance" : {
"text" : "35.9 mi",
"value" : 57824
},
"duration" : {
"text" : "51 mins",
"value" : 3062
},
"end_address" : "Universal Studios Blvd, Los Angeles, CA 90068, USA",
"end_location" : {
"lat" : 34.1330949,
"lng" : -118.3524442
},
"start_address" : "Disneyland (Harbor Blvd.), S Harbor Blvd, Anaheim, CA 92802, USA",
"start_location" : {
"lat" : 33.8098177,
"lng" : -117.9154353
},
... Additional results truncated in this example[] ...
So in order to access what we want we'd make a series of classes that match the information we want.因此,为了访问我们想要的内容,我们将创建一系列与我们想要的信息相匹配的类。
class Routes
{
Legs legs;
}
class Legs
{
EndLocation endLocation;
}
class EndLocation
{
string lat;
string lon;
}
This is rough code, and I haven't been able to test it but it should hopefully give you a good starting direction.这是一个粗略的代码,我还没有能够测试它,但它应该会给你一个好的开始方向。
using Newtonsoft.Json;
void ReadJson()
{
// Let's say for now that our results are held in a file, for simplicity's sake
string jsonString = File.ReadAllText(location.txt);
// Now we'll call the deserializer, since we have the JSON string.
// It'll turn it into our object automatically with the template.
Routes locationInfo = JsonConvert.DeserializeObject<Routes>(jsonString);
serializer.Deserialize(jsonString, locationInfo);
}
After that you should be able to access the locationInfo
object, just as you would any regular C# object.之后,您应该能够访问locationInfo
对象,就像访问任何常规 C# 对象一样。 This should work, but at the very least I'm hoping gives you a good direction on how to solve your problem.这应该有效,但至少我希望为您提供如何解决问题的好方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.