|
Partner
|
|
|
Geografische Koordinaten mit der Google Maps API abfragen
Autor:
Jan Welker
|
Sprache:
C#
|
Bewertung:
8.36 (6 votes)
|
Anzahl der Aufrufe:
23202
|
Beschreibung:
Mit der Google Maps API kann sehr einfach der Längen- und Breitengrad zu einer belieben Position abgefragt werden. Dabei ist es egal, ob man nach „Deutschland“, „München“, „Berlin Alexanderplatz 1“ oder nach einer Postleitzahl sucht.
Für den Aufruf der Methode wird ein Google Maps API Key benötigt, dieser kann hier kostenlos beantragt werden: http://code.google.com/apis/maps/signup.html
Sollen mehrere Koordinaten abgefragt werden, ist darauf zu achten, dass eine Pause von ca. 100ms zwischen den Abfragen eingehalten wird, sonst erkennt Google einen DOS Angriff und sperrt die IP für einige Minuten.
Folgende Usings werden benötigt:
using System.Net; using System.IO; using System.Text;
Abgelegt unter: Google, API, Geodata, Geografische Koordinaten, Longitude, Latitude.
|
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
/// <summary>
/// Gets the geo data.
/// </summary>
/// <param name="location">The location.</param>
/// <param name="googleKey">The google key.</param>
/// <returns></returns>
public static LatLong GetGeoData(string location, string googleKey)
{
LatLong myLatLong = new LatLong();
if (!string.IsNullOrEmpty(googleKey))
{
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://maps.google.com/maps/geo?q=" + location.Trim() + "&output=csv&key=" + googleKey);
HttpWebResponse webResponse;
try
{
webResponse = myReq.GetResponse() as HttpWebResponse;
}
catch
{
return myLatLong;
}
if (webResponse != null)
if (webResponse.StatusCode == HttpStatusCode.OK)
{
StreamReader streamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.ASCII);
string responseString = streamReader.ReadToEnd();
if (!string.IsNullOrEmpty(responseString))
{
string[] arrayResponse = responseString.Split(',');
if (arrayResponse.Length == 4)
{
myLatLong.Latitude = arrayResponse[2];
myLatLong.Longitude = arrayResponse[3];
}
}
}
}
return myLatLong;
}
public class LatLong
{
private string _latitude;
private string _longitude;
public string Latitude
{
get { return _latitude; }
set { _latitude = value; }
}
public string Longitude
{
get { return _longitude; }
set { _longitude = value; }
}
}
|
|
Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)
|
Timo Boehme schrieb am:
25.11.2008 11:28:28
|
SoftIl schrieb am:
12.12.2008 18:17:58
|
Jan Welker schrieb am:
13.12.2008 14:58:25
|
andyhirt schrieb am:
31.03.2009 23:19:15
|
|
Diese Snippets könnten für Sie interessant sein:
|
|
|
|
|
|
|
|
|