geocoding api_什么是GeoCoding,反向GeoCoding,以及如何创建简单的C#App?

本文介绍了GeoCoding(地址转经纬度)和反向GeoCoding(经纬度转地址)的概念,提供了C#实现示例,包括使用Google Maps API进行地理位置转换的代码,以及如何创建简单的C#应用程序进行地理编码和反向地理编码操作。
摘要由CSDN通过智能技术生成

geocoding api

What is Geocoding and Reverse Geocoding?

什么是地理编码和反向地理编码?

Geocoding: Is the process when you convert an address into geo-coordinates (Latitude, Longitude).

地理编码:将地址转换为地理坐标(纬度,经度)时的过程。

For example: 

例如:

    When you search for “Strada+Tudor+Vladimirescu+34,+Zalău” in GoogleMaps (https://maps.googleapis.com/maps/api/geocode/json?address=Strada+Tudor+Vladimirescu+34,+Zal%C4%83u&key=YOUR_API_KEY&sensor=false), you will get the following coordinates from JSon Geometry/Location:

当您在GoogleMaps中搜索“ Strada + Tudor + Vladimirescu + 34,+Zalău”时( https://maps.googleapis.com/maps/api/geocode/json?address= Strada + Tudor + Vladimirescu + 34,+ Zal% C4%83u &key = YOUR_API_KEY&sensor = false ),您将从JSon Geometry / Location获得以下坐标:

    47.18998999999999, 23.061813 

47.18998999999999,23.061813

Converted to deg/min/sec is:

转换为度/分/秒是:

Latitude: 47 deg 11 min 23.964 sec

纬度:47度11分钟23.964秒

Longitude: 23 deg 3 min 42.527 sec

经度:23度3分钟42.527秒

To test GeoCoding without API KEY, there is an example on Google: https://google-developers.appspot.com/maps/documentation/utils/geocoder/

要在不使用API​​ KEY的情况下测试GeoCoding,请在Google上找到一个示例: https ://google-developers.appspot.com/maps/documentation/utils/geocoder/

For C# Geocoding sample, check the GeoCoding (string address) function.

对于C#地理编码示例,请检查GeoCoding( 字符串 地址)功能。

Reverse GeoCoding: Is the opposite process, when you convert Geo-coordinates(Latitude, Longitude) to a human readable address.

反向地理编码:将地理坐标(纬度,经度)转换为人类可读的地址时,这是相反的过程。

For example: 

例如:

    If you search (https://maps.googleapis.com/maps/api/geocode/json?latlng=47.18998999999999,23.061813&key=YOUR_API_KEY&sensor=false), for a specific coordinate (47.18998999999999, 23.061813) in Google Maps, you will get the following address from JSON formatted_address:

如果你搜索( https://maps.googleapis.com/maps/api/geocode/json?latlng= 47.18998999999999,23.061813&键= YOUR_API_KEY与传感器= FALSE ),特定坐标(47.18998999999999,23.061813)在谷歌地图,你会得到JSON formatted_address中的以下地址:

    Strada Tudor Vladimirescu 34, Zalău, Romania

Strada Tudor Vladimirescu 34,扎鲁,罗马尼亚

    To test Reverse GeoCoding without API KEY, there is an example on Google: https://google-developers.appspot.com/maps/documentation/utils/geocoder/

要在没有API KEY的情况下测试反向GeoCoding,请在Google上找到一个示例: https ://google-developers.appspot.com/maps/documentation/utils/geocoder/

For C# Reverse Geocoding sample, check the ReverseGeoCoding(string latitude, string longitude) function.

对于C#反向地理编码示例,请检查ReverseGeoCoding( 字符串 纬度, 字符串 经度) 函数 。



* More details about how GeoCoding and Reverse GeoCoding works, you can find in the following links:

*有关GeoCoding和反向GeoCoding的工作方式的更多详细信息,可以在以下链接中找到:

  1. https://developers.google.com/maps/documentation/geocoding/start

    https://developers.google.com/maps/documentation/geocoding/start
  2. https://en.wikipedia.org/wiki/Geocoding

    https://zh.wikipedia.org/wiki/地理编码
  3. https://en.wikipedia.org/wiki/Reverse_geocoding

    https://zh.wikipedia.org/wiki/反向地理编码

C# Source Code sample:

C#源代码示例:

The following source code is written in C# Console Application (Framework 4.5). To save space, I created a single project for GeoCoding and Reverse GeoCoding and a class for deserializing the JSON.

以下源代码是用C#控制台应用程序(框架4.5)编写的。 为了节省空间,我为GeoCoding和Reverse GeoCoding创建了一个项目,并为反序列化JSON创建了一个类。

To run it, you will need the following:

要运行它,您将需要以下内容:

-create a new API Key 

-创建一个新的API密钥

-enable Maps Javascript API

-启用Maps Javascript API

-enable GeoLocation API

-启用GeoLocation API

-enable GeoCoding API

启用GeoCoding API

-Visual Studio

-视觉工作室

Tip: don't forget to enable billing information in Google for the API and attach to the project

提示:不要忘记在Google中为API启用结算信息并附加到项目中

This article I will not cover, how to enable API Keys on Google, but you can find more information on their site:

我不会在本文中介绍如何在Google上启用API密钥,但是您可以在其网站上找到更多信息:

https://developers.google.com/maps/documentation/javascript/get-api-key

https://developers.google.com/maps/documentation/javascript/get-api-key

Create a new Console application project and after that, from NuGet, install JSon.Net (Newtonsoft.Json):

创建一个新的Console应用程序项目,然后从NuGet中安装JSon.Net(Newtonsoft.Json):

- Step 1 - File->New-> Project  (I used .Net Framework 4.5)

-步骤1-文件->新建->项目(我使用了.Net Framework 4.5)

- Step 2 - Install from Nuget Json.Net: 

-步骤2-从Nuget Json.Net安装:

- Step 3 - Create a New class in the project (I named it GeoResult.cs), this will be used for deserializing the JSON request

-第3步-在项目中创建一个New类(我将其命名为GeoResult.cs),该类将用于反序列化JSON请求

This is the final view of my project:

这是我的项目的最终视图:

The source code for GeoResult.cs:

GeoResult.cs的源代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GeoCoding_Art
{
    public class GoogleGeoCodeResponse
    {

        public string status { get; set; }
        public results[] results { get; set; }

    }

    public class results
    {
        public string formatted_address { get; set; }
        public geometry geometry { get; set; }
        public string[] types { get; set; }
        public address_component[] address_components { get; set; }
    }

    public class geometry
    {
        public string location_type { get; set; }
        public location location { get; set; }
    }

    public class location
    {
        public string lat { get; set; }
        public string lng { get; set; }
    }

    public class address_component
    {
        public string long_name { get; set; }
        public string short_name { get; set; }
        public string[] types { get; set; }
    }
} 

The source code for Program.cs:

Program.cs的源代码:

using Newtonsoft.Json; //added JSON.NET with Nuget
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace GeoCoding_Art
{
    class Program
    {
        const string apiKey = "YOUR_API_KEY"; //!!!!paste your API KEY HERE!!!!
        static string baseUrlGC = "https://maps.googleapis.com/maps/api/geocode/json?address="; // part1 of the URL for GeoCoding
        static string baseUrlRGC = "https://maps.googleapis.com/maps/api/geocode/json?latlng="; // part1 of the URL for Reverse GeoCoding
        static string plusUrl="&key="+apiKey+"&sensor=false"; // part2 of the URL

        static public int DisplayMenu() // I add a menu for selecting between 1 - GeoCoding / 2 - Reverse Geocoding / 3 - Exit
        {
            Console.WriteLine("GEOCODING TUTORIAL (Select and hit Enter):");
            Console.WriteLine();
            Console.WriteLine("1. GeoCoding"); // 1 for GeoCoding
            Console.WriteLine("2. Reverse GeoCoding"); // 2 for Reverse Geocoding
            Console.WriteLine("3. Exit"); // 3 Exit
            Console.WriteLine();
            var result = Console.ReadLine(); //waiting for an integer input for the menu; value can between 1-3
            return Convert.ToInt32(result); //converting result to an integer for the menu
        }

        static void Main(string[] args)
        {
            int menuInput = 0;
            do // do-while statement for the menu, it loops until the input is 3 (Exit) 
            {
                Console.ForegroundColor = ConsoleColor.Green; // changing color for the console to green 
                menuInput=DisplayMenu(); //getting the result of the input for the menu
                Console.ForegroundColor = ConsoleColor.Gray; // changing to default color

                switch (menuInput.ToString()) //switch statement for checking if input is 1 or 2
                {
                    case "1":    //if the input for menu is 1, then call the GeoCoding function
                        Console.WriteLine("===== GEOCODING =====");
                        Console.WriteLine("Enter an address for GeoCoding Result: ");
                        string inputAddress = Console.ReadLine();
                        Console.WriteLine("-------------------------------------");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("RESULT: " + GeoCoding(inputAddress));
                        Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                    case "2":    //if the input for menu is 2, then call the ReverseGeoCoding function
                        Console.WriteLine("===== REVERSE GEOCODING =====");
                        Console.WriteLine("Enter a Latitude: ");
                        string latitude = Console.ReadLine();
                        Console.WriteLine("Enter a Longitude: ");
                        string longitude = Console.ReadLine();
                        Console.WriteLine("-------------------------------------");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("RESULT: " + ReverseGeoCoding(latitude,longitude));
                        Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                }
            } while (menuInput != 3); 


        }

        static string GeoCoding(string address)
        {
            var json = new WebClient().DownloadString(baseUrlGC + address.Replace(" ", "+") 
                + plusUrl);//concatenate URL with the input address and downloads the requested resource
            GoogleGeoCodeResponse jsonResult = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(json); //deserializing the result to GoogleGeoCodeResponse

            string status = jsonResult.status; // get status 

            string geoLocation = String.Empty;

            if (status == "OK") //check if status is OK
            {
                for (int i = 0; i < jsonResult.results.Length; i++) //loop throught the result for lat/lng
                {
                    geoLocation += "Latitude: " + jsonResult.results[i].geometry.location.lat + 
                    " / Longitude: " + jsonResult.results[i].geometry.location.lng + Environment.NewLine; //append the result addresses to every new line
                }
                return geoLocation; //return result
            }
            else 
            {
                return status; //return status / error if not OK
            }
        }

        static string ReverseGeoCoding(string latitude, string longitude)
        {
            var json = new WebClient().DownloadString(baseUrlRGC + latitude.Replace(" ", "") + "," 
                + longitude.Replace(" ", "") + plusUrl);//concatenate URL with the input lat/lng and downloads the requested resource
            GoogleGeoCodeResponse jsonResult = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(json); //deserializing the result to GoogleGeoCodeResponse

            string status = jsonResult.status; // get status

            string geoLocation = String.Empty;

            if (status == "OK") //check if status is OK
            {
                for (int i = 0; i < jsonResult.results.Length; i++) //loop throught the result for addresses
                {
                    geoLocation += "Address: " + jsonResult.results[i].formatted_address + Environment.NewLine; //append the result addresses to every new line
                }
                return geoLocation; //return result
            }
            else 
            {
                return status; //return status / error if not OK
            }
        }
    }
}


 

Some screenshots of the working app

工作应用程序的一些屏幕截图

The menu: (Waiting for user input 1-3)

菜单:(等待用户输入1-3)

1 - GeoCoding (searched term: Strada Tudor Vladimiresc 34, Zalau, the result is with red):

1-GeoCoding(搜索词:Strada Tudor Vladimiresc 34,Zalau,结果为红色):

2 - Reverse GeoCoding (searched term: Latitude: 47.18998999999999 / Longitude: 23.061813, the result is with red):

2-反向地理编码(搜索词:纬度:47.18998999999999 /经度:23.061813,结果为红色):

3 - for Exit 

3-退出

Source Code

源代码

GeoResult.cs

GeoResult.cs

Program.cs

Program.cs

Entire Project

整个项目

GeoCoding_Art2.zip

GeoCoding_Art2.zip

I wish you happy coding! 

祝您编码愉快!

Molnar Istvan

莫尔纳·伊斯特万(Molnar Istvan)

翻译自: https://www.experts-exchange.com/articles/33239/What-is-GeoCoding-Reverse-GeoCoding-and-how-to-create-a-simple-C-App.html

geocoding api

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值