使用Google位置API
请原谅我的无知,但经过几个小时的搜索后,我运气不佳。使用Google位置API
无论如何,我正在尝试编写一个小型桌面应用程序,允许用户输入一个地址,然后在GPS坐标中返回其大致位置。从我可以告诉,谷歌提供了一个地理编码API [1],允许在下列形式要求:
http://maps.googleapis.com/maps/api/geocode/output?parameters
虽然我熟悉编写基本的C夏普的应用,我真的不知道在哪里当涉及到与这个API的接口时开始。任何帮助,你们可以提供将不胜感激。
Geocode API非常简单,只需要3个参数即输出,传感器和地址就可以获得纬度/经度。
输出所需的输出格式,JSON或XML(这个)
传感器应该是一个布尔值,指示天气或不值来自一个传感器,诸如GPS芯片。
地址应该是您希望进行地理编码的地址(不要忘记对其进行网址编码)。
这是一个例子,在那里我地理编码我的办公地址,并响应得到JSON: http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=1+Maritime+Plaza+San+Francisco+CA
如果您导航到你应该看到类似:
{
"status": "OK",
"results": [ {
"types": [ "street_address" ],
"formatted_address": "1 Maritime Plaza, San Francisco, CA 94111, USA",
"address_components": [ {
"long_name": "1",
"short_name": "1",
"types": [ "street_number" ]
}, {
"long_name": "Maritime Plaza",
"short_name": "Maritime Plaza",
"types": [ "route" ]
}, {
"long_name": "San Francisco",
"short_name": "San Francisco",
"types": [ "locality", "political" ]
}, {
"long_name": "San Francisco",
"short_name": "San Francisco",
"types": [ "administrative_area_level_3", "political" ]
}, {
"long_name": "San Francisco",
"short_name": "San Francisco",
"types": [ "administrative_area_level_2", "political" ]
}, {
"long_name": "California",
"short_name": "CA",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "United States",
"short_name": "US",
"types": [ "country", "political" ]
}, {
"long_name": "94111",
"short_name": "94111",
"types": [ "postal_code" ]
} ],
"geometry": {
"location": {
"lat": 37.7953907,
"lng": -122.3991803
},
"location_type": "ROOFTOP",
"viewport": {
"southwest": {
"lat": 37.7922431,
"lng": -122.4023279
},
"northeast": {
"lat": 37.7985383,
"lng": -122.3960327
}
}
}
} ]
}
如果拿提供纬度/经度,并将其放置在map上,您可以在办公楼看到指针。
如果你在C#中这样做,你会希望使用HttpWebRequest和HttpWebResponse类。您可以将参数化的URL(Google API调用)作为参数传递给Create()方法。我建议请求将数据作为XML数据返回。关闭连接(httpWResp.Close())后,您可以使用流读取器进行读取。上看到的GetResponseStream()方法文档:http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx
HttpWebRequest的HttpWReq = (HttpWebRequest的)WebRequest.Create(“http://maps.googleapis.com/maps/api/geocode/xml?sensor=false &地址= 1 + Maritime + Plaza + San + Francisco + CA “);
HttpWebResponse HttpWResp =(HttpWebResponse)HttpWReq.GetResponse(); //插入使用响应对象的代码。 HttpWResp。关();
下面是一个简单的代码来获得你想要的东西
using System;
using System.Web;
using System.Net;
using System.Runtime.Serialization.Json;
namespace GoogleMapsSample
{
public class GoogleMaps
{
/// <summary>
///
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public static GeoResponse GetGeoCodedResults(string address)
{
string url = string.Format(
"http://maps.google.com/maps/api/geocode/json?address={0}®ion=dk&sensor=false",
HttpUtility.UrlEncode(address)
);
var request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GeoResponse));
var res = (GeoResponse)serializer.ReadObject(request.GetResponse().GetResponseStream());
return res;
}
}
和您的辅助类是
namespace GoogleMapsSample
{
[DataContract]
public class GeoResponse
{
[DataMember(Name = "status")]
public string Status { get; set; }
[DataMember(Name = "results")]
public CResult[] Results { get; set; }
[DataContract]
public class CResult
{
[DataMember(Name = "geometry")]
public CGeometry Geometry { get; set; }
[DataContract]
public class CGeometry
{
[DataMember(Name = "location")]
public CLocation Location { get; set; }
[DataContract]
public class CLocation
{
[DataMember(Name = "lat")]
public double Lat { get; set; }
[DataMember(Name = "lng")]
public double Lng { get; set; }
}
}
}
public GeoResponse()
{ }
}
}
然后你就可以在你的控制使用此代码/像这样
的WinForms GeoResponse response = new GeoResponse();
response = GoogleMaps.GetGeoCodedResults(TextBoxPostcode.Text);
并在代码背后使用StringBuilder或其他任何构建JS代码
完全记录.NET库 -
为.NET https://github.com/maximn/google-maps/
//Directions
DirectionsRequest directionsRequest = new DirectionsRequest()
{
Origin = "NYC, 5th and 39",
Destination = "Philladephia, Chesnut and Wallnut",
};
DirectionsResponse directions = MapsAPI.GetDirections(directionsRequest);
//Geocode
GeocodingRequest geocodeRequest = new GeocodingRequest()
{
Address = "new york city",
};
GeocodingResponse geocode = MapsAPI.GetGeocode(geocodeRequest);
Console.WriteLine(geocode);
//Elevation
ElevationRequest elevationRequest = new ElevationRequest()
{
Locations = new Location[] { new Location(54, 78) },
};
ElevationResponse elevation = MapsAPI.GetElevation(elevationRequest);
Console.WriteLine(elevation);
最近,我必须建立一个商店定位为一个项目我工作的谷歌地图的Web服务API的包装。我以前从未使用任何Google或Bing API。使用本教程,我能够很好地掌握Location API。我会建议阅读本教程,并在三篇教程结束时,您应该有一个很好的理解。
Building a Store Locator ASP.NET Application Using Google Maps API (Part 1)
Building a Store Locator ASP.NET Application Using Google Maps API (Part 2)
Building a Store Locator ASP.NET Application Using Google Maps API (Part 3)
通过使用GuigleAPI nuget,你可以简单地做:
GoogleGeocodingAPI.GoogleAPIKey = "YOUR API KEY";
var result = await GoogleGeocodingAPI.GetCoordinatesFromAddressAsync("100 Market St, Southbank");
var latitude = result.Item1;
var longitude = result.Item2;
只需用的NuGet命令安装,包装Easyforce.GuigleAPI并安装它你很好 走。
检查这个答案详细信息: https://stackoverflow.com/a/44484010/1550202
我一定会尝试了这一点。在此期间,我只想说谢谢,我真的很感激。 – 2011-01-28 18:16:52
如果添加可选的区域参数,它将尽力在该位置返回一个位置。 – 2012-03-15 14:10:01