2011-01-28 30 views
8

請原諒我的無知,但經過幾個小時的搜索後,我運氣不佳。使用Google位置API

無論如何,我正在嘗試編寫一個小型桌面應用程序,允許用戶輸入一個地址,然後在GPS座標中返回其大致位置。從我可以告訴,谷歌提供了一個地理編碼API [1],允許在下列形式要求:

http://maps.googleapis.com/maps/api/geocode/output?parameters

雖然我熟悉編寫基本的C夏普的應用,我真的不知道在哪裏當涉及到與這個API的接口時開始。任何幫助,你們可以提供將不勝感激。

回答

2

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上,您可以在辦公樓看到指針。

+0

我一定會嘗試了這一點。在此期間,我只想說謝謝,我真的很感激。 – 2011-01-28 18:16:52

+0

如果添加可選的區域參數,它將盡力在該位置返回一個位置。 – 2012-03-15 14:10:01

0

如果你在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。關();

6

下面是一個簡單的代碼來獲得你想要的東西

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}&region=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代碼

13

完全記錄.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); 
1

最近,我必須建立一個商店定位爲一個項目我工作的谷歌地圖的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)

0

通過使用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

相關問題