輸入地址後呼叫 google 查經度和緯度。
我們只要輸入 http://maps.googleapis.com/maps/api/geocode/xml?address=地址&sensor=false
google 會返回一個包含地理資訊的 XML 檔。
分析這個檔案,我們可以獲得一些有用的資訊。
01 | using System; |
02 | using System.Collections.Generic; |
03 | using System.ComponentModel; |
04 | using System.Data; |
05 | using System.Drawing; |
06 | using System.Linq; |
07 | using System.Text; |
08 | using System.Windows.Forms; |
09 | using System.Xml; |
10 | using System.Net; |
11 | using System.IO; |
12 | namespace WindowsFormsApplication1 |
13 | { |
14 | public partial class Form1 : Form |
15 | { |
16 | public Form1() |
17 | { |
18 | InitializeComponent(); |
19 | } |
20 |
21 | private void button1_Click( object sender, EventArgs e) |
22 | { |
23 | show_map( "台中市政府" ); |
24 | //show_map("暴風城"); |
25 | } |
26 |
27 | private void show_map( string strx) |
28 | { |
29 | if (strx == "" ) return ; |
30 | string h = "http://maps.google.com/maps/api/geocode/xml?address=" + strx + "&sensor=false" ; |
31 | |
32 | try |
33 | { |
34 | WebRequest snw = WebRequest.Create(h); |
35 | HttpWebResponse snr = (HttpWebResponse)(snw.GetResponse()); |
36 | StreamReader sr = new StreamReader(snr.GetResponseStream(),Encoding.UTF8); |
37 | XmlTextReader xtr = new XmlTextReader(sr); |
38 | XmlDocument xd = new XmlDocument(); |
39 | xd.Load(xtr); |
40 | // 請參閱底下附的 Xml 檔。 |
41 | |
42 | string ok = xd.SelectSingleNode( "/GeocodeResponse/status" ).InnerText; |
43 | if (ok.ToUpper()!= "OK" ) |
44 | { |
45 | MessageBox.Show( "查無此址!" ); |
46 | return ; |
47 | } |
48 | // |
49 | string lat = xd.SelectSingleNode( "/GeocodeResponse/result/geometry/location/lat" ).InnerText; |
50 | string lng = xd.SelectSingleNode( "/GeocodeResponse/result/geometry/location/lng" ).InnerText; |
51 | MessageBox.Show( "緯度:" + lat + "\n\n" + "經度:" + lng); |
52 | |
53 | } |
54 | catch (Exception ex) |
55 | { |
56 | MessageBox.Show(ex.Message); |
57 | } |
58 | } |
59 | } |
60 |
61 | } |
以下是返回的 XML 檔:
001 | <? xml version = "1.0" encoding = "UTF-8" ?> |
002 | < GeocodeResponse > |
003 | < status >OK</ status > |
004 | < result > |
005 | < type >establishment</ type > |
006 | < formatted_address >403台灣台中市西區民權路99號台中市政府</ formatted_address > |
007 | < address_component > |
008 | < long_name >台中市政府環境保護局</ long_name > |
009 |
010 | < short_name >台中市政府環境保護局</ short_name > |
011 | < type >establishment</ type > |
012 | </ address_component > |
013 | < address_component > |
014 | < long_name >99號</ long_name > |
015 | < short_name >99號</ short_name > |
016 | < type >street_number</ type > |
017 |
018 | </ address_component > |
019 | < address_component > |
020 | < long_name >民權路</ long_name > |
021 | < short_name >民權路</ short_name > |
022 | < type >route</ type > |
023 | </ address_component > |
024 | < address_component > |
025 |
026 | < long_name >利民里</ long_name > |
027 | < short_name >利民里</ short_name > |
028 | < type >sublocality</ type > |
029 | < type >political</ type > |
030 | </ address_component > |
031 | < address_component > |
032 | < long_name >西區</ long_name > |
033 |
034 | < short_name >西區</ short_name > |
035 | < type >locality</ type > |
036 | < type >political</ type > |
037 | </ address_component > |
038 | < address_component > |
039 | < long_name >台中市</ long_name > |
040 | < short_name >台中市</ short_name > |
041 |
042 | < type >administrative_area_level_2</ type > |
043 | < type >political</ type > |
044 | </ address_component > |
045 | < address_component > |
046 | < long_name >台灣</ long_name > |
047 | < short_name >TW</ short_name > |
048 | < type >country</ type > |
049 |
050 | < type >political</ type > |
051 | </ address_component > |
052 | < address_component > |
053 | < long_name >403</ long_name > |
054 | < short_name >403</ short_name > |
055 | < type >postal_code</ type > |
056 | </ address_component > |
057 |
058 | < geometry > |
059 | < location > |
060 | < lat >24.1389239</ lat > |
061 | < lng >120.6785550</ lng > |
062 | </ location > |
063 | < location_type >APPROXIMATE</ location_type > |
064 | < viewport > |
065 |
066 | < southwest > |
067 | < lat >24.1287412</ lat > |
068 | < lng >120.6625476</ lng > |
069 | </ southwest > |
070 | < northeast > |
071 | < lat >24.1491058</ lat > |
072 | < lng >120.6945624</ lng > |
073 |
074 | </ northeast > |
075 | </ viewport > |
076 | < bounds > |
077 | < southwest > |
078 | < lat >24.1377610</ lat > |
079 | < lng >120.6772470</ lng > |
080 | </ southwest > |
081 | < northeast > |
082 |
083 | < lat >24.1395080</ lat > |
084 | < lng >120.6789820</ lng > |
085 | </ northeast > |
086 | </ bounds > |
087 | </ geometry > |
088 | </ result > |
089 | < result > |
090 | < type >establishment</ type > |
091 |
092 | < formatted_address >420台灣台中市豐原區陽明街36號台中市政府</ formatted_address > |
093 | < address_component > |
094 | < long_name >台中市政府</ long_name > |
095 | < short_name >台中市政府</ short_name > |
096 | < type >establishment</ type > |
097 | </ address_component > |
098 | < address_component > |
099 |
100 | < long_name >36號</ long_name > |
101 | < short_name >36號</ short_name > |
102 | < type >street_number</ type > |
103 | </ address_component > |
104 | < address_component > |
105 | < long_name >陽明街</ long_name > |
106 | < short_name >陽明街</ short_name > |
107 |
108 | < type >route</ type > |
109 | </ address_component > |
110 | < address_component > |
111 | < long_name >陽明里</ long_name > |
112 | < short_name >陽明里</ short_name > |
113 | < type >sublocality</ type > |
114 | < type >political</ type > |
115 |
116 | </ address_component > |
117 | < address_component > |
118 | < long_name >豐原區</ long_name > |
119 | < short_name >豐原區</ short_name > |
120 | < type >locality</ type > |
121 | < type >political</ type > |
122 | </ address_component > |
123 |
124 | < address_component > |
125 | < long_name >台中市</ long_name > |
126 | < short_name >台中市</ short_name > |
127 | < type >administrative_area_level_2</ type > |
128 | < type >political</ type > |
129 | </ address_component > |
130 | < address_component > |
131 |
132 | < long_name >台灣</ long_name > |
133 | < short_name >TW</ short_name > |
134 | < type >country</ type > |
135 | < type >political</ type > |
136 | </ address_component > |
137 | < address_component > |
138 | < long_name >420</ long_name > |
139 |
140 | < short_name >420</ short_name > |
141 | < type >postal_code</ type > |
142 | </ address_component > |
143 | < geometry > |
144 | < location > |
145 | < lat >24.2469820</ lat > |
146 | < lng >120.7208330</ lng > |
147 |
148 | </ location > |
149 | < location_type >APPROXIMATE</ location_type > |
150 | < viewport > |
151 | < southwest > |
152 | < lat >24.2451745</ lat > |
153 | < lng >120.7191970</ lng > |
154 | </ southwest > |
155 |
156 | < northeast > |
157 | < lat >24.2478725</ lat > |
158 | < lng >120.7218950</ lng > |
159 | </ northeast > |
160 | </ viewport > |
161 | < bounds > |
162 | < southwest > |
163 | < lat >24.2457830</ lat > |
164 |
165 | < lng >120.7197530</ lng > |
166 | </ southwest > |
167 | < northeast > |
168 | < lat >24.2472640</ lat > |
169 | < lng >120.7213390</ lng > |
170 | </ northeast > |
171 | </ bounds > |
172 |
173 | </ geometry > |
174 | </ result > |
175 | < result > |
176 | < type >establishment</ type > |
177 | < formatted_address >407台灣台中市西屯區台中港路二段89號台中市政府</ formatted_address > |
178 | < address_component > |
179 | < long_name >台中市政府</ long_name > |
180 |
181 | < short_name >台中市政府</ short_name > |
182 | < type >establishment</ type > |
183 | </ address_component > |
184 | < address_component > |
185 | < long_name >89號</ long_name > |
186 | < short_name >89號</ short_name > |
187 | < type >street_number</ type > |
188 |
189 | </ address_component > |
190 | < address_component > |
191 | < long_name >台中港路二段</ long_name > |
192 | < short_name >台中港路二段</ short_name > |
193 | < type >route</ type > |
194 | </ address_component > |
195 | < address_component > |
196 |
197 | < long_name >惠來里</ long_name > |
198 | < short_name >惠來里</ short_name > |
199 | < type >sublocality</ type > |
200 | < type >political</ type > |
201 | </ address_component > |
202 | < address_component > |
203 | < long_name >西屯區</ long_name > |
204 |
205 | < short_name >西屯區</ short_name > |
206 | < type >locality</ type > |
207 | < type >political</ type > |
208 | </ address_component > |
209 | < address_component > |
210 | < long_name >台中市</ long_name > |
211 | < short_name >台中市</ short_name > |
212 |
213 | < type >administrative_area_level_2</ type > |
214 | < type >political</ type > |
215 | </ address_component > |
216 | < address_component > |
217 | < long_name >台灣</ long_name > |
218 | < short_name >TW</ short_name > |
219 | < type >country</ type > |
220 |
221 | < type >political</ type > |
222 | </ address_component > |
223 | < address_component > |
224 | < long_name >407</ long_name > |
225 | < short_name >407</ short_name > |
226 | < type >postal_code</ type > |
227 | </ address_component > |
228 |
229 | < geometry > |
230 | < location > |
231 | < lat >24.1632378</ lat > |
232 | < lng >120.6477943</ lng > |
233 | </ location > |
234 | < location_type >APPROXIMATE</ location_type > |
235 | < viewport > |
236 |
237 | < southwest > |
238 | < lat >24.1618510</ lat > |
239 | < lng >120.6463045</ lng > |
240 | </ southwest > |
241 | < northeast > |
242 | < lat >24.1645490</ lat > |
243 | < lng >120.6490025</ lng > |
244 |
245 | </ northeast > |
246 | </ viewport > |
247 | < bounds > |
248 | < southwest > |
249 | < lat >24.1629090</ lat > |
250 | < lng >120.6472710</ lng > |
251 | </ southwest > |
252 | < northeast > |
253 |
254 | < lat >24.1634910</ lat > |
255 | < lng >120.6480360</ lng > |
256 | </ northeast > |
257 | </ bounds > |
258 | </ geometry > |
259 | </ result > |
260 | </ GeocodeResponse > |
文章出自:http://www.dotblogs.com.tw/bravo/archive/2012/02/14/69288.aspx