Working with the Google Location API

前端 未结 6 2095
别跟我提以往
别跟我提以往 2021-02-06 05:50

Forgive me for my ignorance, but after several hours of searching, I\'m having little luck.

Anyway, I am attempting to write a small desktop application that will allow

相关标签:
6条回答
  • 2021-02-06 06:10

    The Geocode API is rather simple, to get lat/lon from the api you only need to 3 params: output, sensor and address.

    output the output format you want, json or xml (IIRC)

    sensor should be a boolean indicating weather or not the value comes from a sensor such as a GPS chip.

    address should be the address (don't forget to url encode it) you wish to geocode.

    This is an example, where I geocode my office address, and get JSON in response: http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=1+Maritime+Plaza+San+Francisco+CA

    If you navigate to that you should see something like:

    {
      "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
            }
          }
        }
      } ]
    }
    

    If you take the lat/lon provided and place it on a map you see a pointer on my office building.

    0 讨论(0)
  • 2021-02-06 06:21

    Fully documented .NET library -

    Google Maps Web Services API wrapper for .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);
    
    0 讨论(0)
  • 2021-02-06 06:25

    Here is a sample code to get what you want

    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;
            }
    
    
        }
    

    and you helper class would be

     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()
            { }
        }
    
    
    }
    

    Then you can use this code in your control/winforms like this

                GeoResponse response = new GeoResponse();
                response = GoogleMaps.GetGeoCodedResults(TextBoxPostcode.Text);
    

    and build the JS on the code behind using StringBuilder or anyting else

    0 讨论(0)
  • 2021-02-06 06:26

    By using GuigleAPI nuget you can simply do:

    GoogleGeocodingAPI.GoogleAPIKey = "YOUR API KEY";
    var result = await GoogleGeocodingAPI.GetCoordinatesFromAddressAsync("100 Market St, Southbank");
    var latitude = result.Item1;
    var longitude = result.Item2;
    

    Just install it with the nuget command Install-Package Easyforce.GuigleAPI and you're good to go.

    Check this answer for more details: https://stackoverflow.com/a/44484010/1550202

    0 讨论(0)
  • 2021-02-06 06:31

    If your doing this in C#, you will want to use the HttpWebRequest and HttpWebResponse classes. You can pass the parameterized URL (google API call)as an argument to the Create() method. I would suggest requesting the data be returned as XML data. After you close the connection (httpWResp.Close()), you can read using a stream reader. See documentation on the GetResponseStream() method: 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&address=1+Maritime+Plaza+San+Francisco+CA ");

    HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); // Insert code that uses the response object. HttpWResp.Close();

    0 讨论(0)
  • 2021-02-06 06:37

    I recently had to build a Store Locator for a project I am working on. I have never before this used any of the Google or Bing API's. Using this tutorial I was able to get a pretty good grasp of the Location API. I would suggest going through this tutorial and by the end of the three tutorials you should have a good understanding.

    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 讨论(0)
提交回复
热议问题