如何调用从C#code谷歌地理编码服务 [英] How to call Google Geocoding service from C# code

查看:100
本文介绍了如何调用从C#code谷歌地理编码服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中一个类库。从那里,我有打电话给谷歌服务与;获得纬度和放大器;经度。

I have one class library in C#. From there I have to call Google service & get latitude & longitude.

我知道如何页面上使用AJAX做到这一点,但我想直接从我的C#类文件中调用谷歌地理编码服务。

I know how to do it using AJAX on page, but I want to call Google Geocoding service directly from my C# class file.

有没有办法做到这一点还是有我可以使用任何其它的服务。

Is there any way to do this or are there any other services which I can use for this.

推荐答案

您可以做这样的事情:

var address = "123 something st, somewhere";
var requestUri = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false", Uri.EscapeDataString(address));

var request = WebRequest.Create(requestUri);
var response = request.GetResponse();
var xdoc = XDocument.Load(response.GetResponseStream());

var result = xdoc.Element("GeocodeResponse").Element("result");
var locationElement = result.Element("geometry").Element("location");
var lat = locationElement.Element("lat");
var lng = locationElement.Element("lng");

您也想验证响应状态和捕捉任何WebExceptions。看看谷歌地理编码API

You will also want to validate the response status and catch any WebExceptions. Have a look at Google Geocoding API.

这篇关于如何调用从C#code谷歌地理编码服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆