自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET調(diào)用V3版本的Google Maps API

開發(fā) 后端
百度地圖API接口想必大家都很熟悉了,本文將給大家介紹Google Maps API(V3版本)使用的文章。也是一種開闊思路的文章。

  寫在最前面

  早就聽說(shuō)過Google Maps API了,但一直沒用過,今天在CodeProject上看到了這篇關(guān)于Google Maps API(V3版本)使用的文章,覺得很容易上手,就將他翻譯下來(lái)了,相信對(duì)初學(xué)者會(huì)有大的幫助。譯文允許轉(zhuǎn)載,但請(qǐng)?jiān)陧?yè)面明顯處標(biāo)明以下信息,且保留完整原文鏈接地址和譯文鏈接地址,謝謝合作!

  英文原文:Google Maps API V3 for ASP.NET

  譯文出處:青藤園

  譯文作者:王國(guó)峰

  譯文鏈接:ASP.NET中使用Google Maps API V3【譯】

  簡(jiǎn)介

  Google Maps為我們提供了一種非常靈活的方式來(lái)使用它的地圖服務(wù)。我們可以在Web應(yīng)用程序中通過調(diào)用Google Maps API來(lái)為我們的用戶提供方位信息、地理位置信息以及其他類型的東西。盡管已經(jīng)有很多文章介紹了Google Maps API的使用方法,但這次我要介紹的是最新V3版本的Google Maps API。在這篇文章中,我們將會(huì)看到一些使用Google Maps的常見技術(shù)。為了能更好的理解下面的示例代碼,你需要了解Javascript和C#的基本知識(shí)。

  你的第一個(gè)Google Maps

  在Google Maps API的早期版本中,我們需要將自己的web應(yīng)用程序注冊(cè)至Google,從而獲取一個(gè)API Key。然而隨著新版本的發(fā)布,Google Maps的注冊(cè)機(jī)制已經(jīng)被淘汰了,但是最近Google又提出了一些使用地圖的限制,你可以通過下面的鏈接獲取Google Maps API的使用方法和一些使用條款:http://code.google.com/apis/maps/documentation/javascript/usage.html#usage_limits?,F(xiàn)在我們就開始在自己的網(wǎng)站下創(chuàng)建一個(gè)Google Maps地圖示例,下面的一行代碼是用來(lái)連接Google Maps API服務(wù)的:

  1.   <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"> 
  2. </script> 

  然后你可以用下面的代碼來(lái)創(chuàng)建一個(gè)簡(jiǎn)單的地圖:

  1.   functionInitializeMap()  
  2.   {  
  3.   varlatlng = newgoogle.maps.LatLng(-34.397, 150.644);  
  4.   varmyOptions = {  
  5.   zoom: 8,  
  6.   center: latlng,  
  7.   mapTypeId: google.maps.MapTypeId.ROADMAP  
  8.   };  
  9.   varmap = newgoogle.maps.Map(document.getElementById("map"), myOptions);  
  10.   }  
  11.   window.onload = InitializeMap; 

Google Maps 設(shè)置選項(xiàng)

  在上面的例子中,我們使用了一個(gè)Map類,并設(shè)置了一個(gè)HTML ID作為參數(shù)。現(xiàn)在我們來(lái)更深入一點(diǎn),一起來(lái)看看下面的地圖選項(xiàng):

  1. Codefunctioninitialize() {  
  2.   varlatlng = newgoogle.maps.LatLng(-34.397, 150.644);  
  3.   varoptions =  
  4.   {  
  5.   zoom: 3,  
  6.   center: newgoogle.maps.LatLng(37.09, -95.71),  
  7.   mapTypeId: google.maps.MapTypeId.ROADMAP,  
  8.   mapTypeControl: true,  
  9.   mapTypeControlOptions:  
  10.   {  
  11.   style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,  
  12.   poistion: google.maps.ControlPosition.TOP_RIGHT,  
  13.   mapTypeIds: [google.maps.MapTypeId.ROADMAP,  
  14.   google.maps.MapTypeId.TERRAIN,  
  15.   google.maps.MapTypeId.HYBRID,  
  16.   google.maps.MapTypeId.SATELLITE]  
  17.   },  
  18.   navigationControl: true,  
  19.   navigationControlOptions:  
  20.   {  
  21.   style: google.maps.NavigationControlStyle.ZOOM_PAN  
  22.   },  
  23.   scaleControl: true,  
  24.   disableDoubleClickZoom: truefalse,  
  25.   streetViewControl: true,  
  26.   draggableCursor: 'move' 
  27.   };  
  28.   varmap = newgoogle.maps.Map(document.getElementById("map"), options);  
  29.   }  
  30.   window.onload = initialize; 

  上面的例子中,我們應(yīng)用了地圖的所有屬性,你可以根據(jù)需要來(lái)選擇使用它們。

Map類的屬性說(shuō)明如下表所示

屬性
MapTypeControl:true/false mapTypeControlOptions
屬性
style
1
2
3
DEFAULT
HORIZONTAL_BAR
DROPDOWN_MENU
position
mapTypeIds
navigationControl:true/false navigationControlOptions
屬性
Position
style
scaleControl:true/false scaleControlOptions: 和navigationControl有一樣的屬性 (position, style) 方法也一樣.
disableDoubleClickZoom: true/false
 
scrollwheel: true/false
 
draggable: true/false
 
streetViewControl: true/false
 

  Map Maker(地圖標(biāo)記)

  Maker類提供了這樣一個(gè)選項(xiàng),為用戶指定的位置顯示一個(gè)標(biāo)記,在我們的應(yīng)用中地圖標(biāo)記是十分常用的,下面的代碼將告訴大家如何創(chuàng)建一個(gè)簡(jiǎn)單的地圖標(biāo)記:

  1.   varmarker = newgoogle.maps.Marker  
  2.   (  
  3.   {  
  4.   position: newgoogle.maps.LatLng(-34.397, 150.644),  
  5.   map: map,  
  6.   title: 'Click me' 
  7.   }  
  8.   ); 

Info Window(信息窗口)

  我們已經(jīng)在地圖上某個(gè)位置加了標(biāo)記,也為標(biāo)記添加onclick了事件,點(diǎn)擊可以彈出一個(gè)窗口來(lái)顯示該地點(diǎn)的詳細(xì)信息。我們可以按照下面的代碼來(lái)創(chuàng)建信息窗口:

  1.   varinfowindow = newgoogle.maps.InfoWindow({  
  2.   content: 'Location info:  
  3.   Country Name:  
  4.   LatLng:'  
  5.   });  
  6.   google.maps.event.addListener(marker, 'click', function() {  
  7.   // 打開窗口  
  8.   infowindow.open(map, marker);  
  9.   }); 

  將它們結(jié)合起來(lái)的代碼如下:

  1. Codevarmap;  
  2.   functioninitialize() {  
  3.   varlatlng = newgoogle.maps.LatLng(-34.397, 150.644);  
  4.   varmyOptions = {  
  5.   zoom: 8,  
  6.   center: latlng,  
  7.   mapTypeId: google.maps.MapTypeId.ROADMAP  
  8.   };  
  9.   map = newgoogle.maps.Map(document.getElementById("map"), myOptions);  
  10.   varmarker = newgoogle.maps.Marker  
  11.   (  
  12.   {  
  13.   position: newgoogle.maps.LatLng(-34.397, 150.644),  
  14.  map: map,  
  15.   title: 'Click me' 
  16.   }  
  17.   );  
  18.   varinfowindow = newgoogle.maps.InfoWindow({  
  19.   content: 'Location info:  
  20.   Country Name:  
  21.   LatLng:'  
  22.   });  
  23.   google.maps.event.addListener(marker, 'click', function() {  
  24.   // Calling the open method of the infoWindow  
  25.   infowindow.open(map, marker);  
  26.   });  
  27.  }  
  28.   window.onload = initialize; 

  利用上面的代碼,我們將會(huì)在頁(yè)面上創(chuàng)建一張地圖,然后定位用戶所在的區(qū)域,在這個(gè)區(qū)域加上標(biāo)記,并且彈出一個(gè)顯示位置信息的窗口。

Multiple Makers(多標(biāo)記)

  有些時(shí)候,我們可以要在地圖上處理多個(gè)標(biāo)記,那么我們就可以用下面代碼來(lái)實(shí)現(xiàn):

  1. Codefunctionmarkicons() {  
  2.   InitializeMap();  
  3.   varltlng = [];  
  4.   ltlng.push(newgoogle.maps.LatLng(40.756, -73.986));  
  5.   ltlng.push(newgoogle.maps.LatLng(37.775, -122.419));  
  6.   ltlng.push(newgoogle.maps.LatLng(47.620, -122.347));  
  7.   ltlng.push(newgoogle.maps.LatLng(-22.933, -43.184));  
  8.   for(vari = 0; i <= ltlng.length;i++) {  
  9.   marker = newgoogle.maps.Marker({  
  10.   map: map,  
  11.   position: ltlng[i]  
  12.   });  
  13.   (function(i, marker) {  
  14.   google.maps.event.addListener(marker, 'click', function() {  
  15.   if(!infowindow) {  
  16.   infowindow = newgoogle.maps.InfoWindow();  
  17.   }  
  18.   infowindow.setContent("Message" + i);  
  19.   infowindow.open(map, marker);  
  20.   });  
  21.   })(i, marker);  
  22.   }  
  23.   } 

路線說(shuō)明

  一個(gè)最有用的特性之一是Google Maps API可以為任何指定的位置提供詳細(xì)的路線說(shuō)明,實(shí)現(xiàn)代碼如下:

  1. CodevardirectionsDisplay;  
  2.   vardirectionsService = newgoogle.maps.DirectionsService();  
  3.   functionInitializeMap() {  
  4.   directionsDisplay = newgoogle.maps.DirectionsRenderer();  
  5.   varlatlng = newgoogle.maps.LatLng(-34.397, 150.644);  
  6.   varmyOptions =  
  7.   {  
  8.   zoom: 8,  
  9.  center: latlng,  
  10.   mapTypeId: google.maps.MapTypeId.ROADMAP  
  11.   };  
  12.   varmap = newgoogle.maps.Map(document.getElementById("map"), myOptions);  
  13.   directionsDisplay.setMap(map);  
  14.   directionsDisplay.setPanel(document.getElementById('directionpanel'));  
  15.   varcontrol = document.getElementById('control');  
  16.   control.style.display = 'block';  
  17.   }  
  18.   calcRoute() {  
  19.   varstart = document.getElementById('startvalue').value;  
  20.   varend = document.getElementById('endvalue').value;  
  21.   varrequest = {  
  22.   origin: start,  
  23.   destination: end,  
  24.  travelMode: google.maps.DirectionsTravelMode.DRIVING  
  25.   };  
  26.   directionsService.route(request, (response, status) {  
  27.   if(status== google.maps.DirectionsStatus.OK) {  
  28.   directionsDisplay.setDirections(response);  
  29.   }  
  30.   });  
  31.  }  
  32.   functionwindow.onload = InitializeMap; 

Layers

  Google Maps API為你提供了多層的選項(xiàng),其中有一個(gè)是自行車層。通過自行車層,可以為一些特別的位置顯示自行車路線。下面的代碼是讓你在地圖上添加自行車層:

  1. Codevarmap  
  2.   functionInitializeMap() {  
  3.   varlatlng = newgoogle.maps.LatLng(-34.397, 150.644);  
  4.   varmyOptions = {  
  5.   zoom: 8,  
  6.   center: latlng,  
  7.   mapTypeId: google.maps.MapTypeId.ROADMAP  
  8.   };  
  9.   map = newgoogle.maps.Map(document.getElementById("map"), myOptions);  
  10.   }  
  11.   window.onload = InitializeMap;  
  12.   varbikeLayer = newgoogle.maps.BicyclingLayer();  
  13.   bikeLayer.setMap(map); 

  Geocoding

  到目前為止,我們已經(jīng)學(xué)習(xí)創(chuàng)建Google地圖的基本思想,同時(shí)也學(xué)習(xí)了如何顯示位置相關(guān)的信息。下面我們來(lái)看看用戶是如何來(lái)計(jì)算位置的,Geocoding可以計(jì)算出指定區(qū)域的經(jīng)度和緯度,下面的代碼就告訴你如何利用API計(jì)算某個(gè)位置的經(jīng)度和緯度的:

  1.   Codegeocoder.geocode({ 'address': address }, function(results, status) {  
  2.   if(status== google.maps.GeocoderStatus.OK) {  
  3.   map.setCenter(results[0].geometry.location);  
  4.   varmarker = newgoogle.maps.Marker({  
  5.  map: map,  
  6.   position: results[0].geometry.  
  7.   });  
  8.   }  
  9.   else{  
  10.   alert("Geocode was not successful for the following reason: " + status);  
  11.   }  
  12.   }); 

  Geocoding C#

  同樣我們可以利用C#代碼來(lái)計(jì)算位置:

  1. CodepublicstaticCoordinate GetCoordinates(string region)  
  2.   {  
  3.  using (varclient = newWebClient())  
  4.   {  
  5.   string uri = "http://maps.google.com/maps/geo?q='" + region +  
  6.  "'&output=csv&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1" +  
  7.   "-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA";  
  8.   string[] geocodeInfo = client.DownloadString(uri).Split(',');  
  9.   returnnewpublicstruct Coordinate  
  10.   {  
  11.   privatedoublelat;  
  12.   private 

  Reverse Geocoding

  顧名思義,這個(gè)是Geocoding的反操作,我們可以根據(jù)經(jīng)度和緯度來(lái)找出該位置的名稱。代碼如下:

  1. Codevarmap;  
  2.   vargeocoder;  
  3.   functionInitializeMap() {  
  4.   varlatlng = newgoogle.maps.LatLng(-34.397, 150.644);  
  5.   myOptions =  
  6.   {  
  7.   zoom: 8,  
  8.   center: latlng,  
  9.   mapTypeId: google.maps.MapTypeId.ROADMAP,  
  10.   disableDefaultUI: true 
  11.   };  
  12.   map = newgoogle.maps.Map(document"), myOptions);  
  13.   }  
  14.   functionFindLocaiton() {  
  15.   geocoder = newgoogle.maps.Geocoder();  
  16.   InitializeMap();  
  17.   varaddress = document.getElementById("addressinput").value;  
  18.   geocoder.geocode({ 'address': address }, function(results, 

  Reverse Geocoding in C#

  同樣用C#也可以實(shí)現(xiàn)Reverse Geocoding操作:

  1. CodestaticstringbaseUri =  
  2.   "http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=falsestringlocation = string.Empty;  
  3.   publicstaticvoidRetrieveFormatedAddress(stringlat, stringlng)  
  4.   {  
  5.   requestUri = string.Format(baseUri, lat, lng);  
  6.   using(WebClient wc = newWebClient())  
  7.   {  
  8.   stringinxmlElm.Descendants() where  
  9.   elm.Name == "status 

  總結(jié)

  在這篇文章,我嘗試將V3版本的Google Maps API中的最基本和最常用的功能解說(shuō)清楚。希望這篇文章能幫你順利完成任務(wù)。然后,API中還有很多我沒有討論到的,我將嘗試在今后的文章中來(lái)討論。當(dāng)然希望能得到大家的點(diǎn)評(píng)和反饋。

源碼下載:http://files.cnblogs.com/sxwgf/GMAP.zip

原文:http://www.cnblogs.com/jz1108/archive/2011/10/21/2220574.html

【編輯推薦】

  1. 百度地圖API如何批量轉(zhuǎn)換為百度經(jīng)緯度
  2. 百度地圖API如何給自定義覆蓋物添加事件
  3. 詳解百度地圖API之自定義地圖類型
  4. 詳解百度地圖API之地圖操作
  5. 百度地圖API之如何制作駕車導(dǎo)航
  6. 詳解百度地圖API之地圖標(biāo)注
責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2020-10-27 10:40:18

JavaAPI V3支付

2015-07-21 10:46:43

ASP.NET微軟

2009-07-27 17:54:39

WCF服務(wù)ASP.NET

2009-07-20 17:59:07

JavaScript調(diào)ASP.NET AJA

2009-07-21 09:43:36

調(diào)用UpdatePanASP.NET

2013-03-04 14:24:58

Google Maps

2013-02-28 13:35:02

Google Maps

2009-04-01 12:00:43

ASP.NETMVC

2009-08-05 16:59:38

ASP.NET調(diào)用Ex

2009-07-24 16:05:05

調(diào)用Web ServiASP.NET

2009-08-03 14:22:33

什么是ASP.NET

2009-07-28 17:17:19

ASP.NET概述

2014-06-09 15:29:13

OData v4.0

2009-07-22 17:45:35

ASP.NET教程

2021-03-12 00:04:52

網(wǎng)關(guān)Api

2010-08-16 09:14:37

ASP.NET MVC

2010-07-30 13:17:33

NFS V3

2009-07-27 10:35:33

TypeConvertASP.NET

2009-07-29 11:19:03

JavaScriptASP.NET

2009-07-29 14:52:12

IScriptContASP.NET
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)