Tải bản đầy đủ - 0 (trang)
Hack 28. Get Access to the Google Maps API

Hack 28. Get Access to the Google Maps API

Tải bản đầy đủ - 0trang






PEMs2vnU_f0RhwHhZhSa_9HZXsWbc66iRLah8f17kmN8QRSryZ54UMgeX8GjfYz

type="text/javascript">



Theresultofimportingthisscriptwithaproperkeyisthatyour

ownJavaScriptcodecannowaccesstheobjectsthatarepartof

theGoogleMapsAPI.Theseincludethefollowing:

GMap,anobjectthatallowsyoutoembedadigitalmapwith



zoomingcontrolsinsideofablock-typeHTMLelement,such

asadiv

GMarker,aniconthatpointstoaspecificplaceonthemap

GPolyline,whichcanoverlayshapesontoamap

GPoint,representingamapcoordinate

GXmlHttp,a"wrapperobject"forourfamiliarXMLHttpRequest



object

AlltheseobjectsexceptforGPolylineareusedinthenexttwo

hacks.Inaddition,theAPIincludesGXmlandGXsltobjectsfor

parsingXMLandusingXSLTtechnology.



AsmentionedinChapter1,XSLTisatemplatinglanguagefor

transformingtheinformationembeddedinXMLfiles.



TheAPIalsoincludesageneral-purposefunction,

GBrowserIsCompatible(),whichreturnsTRueiftheuser'sbrowser

supportsGoogleMaps.AsofNovember2005,accordingtothe

Googledocumentation,GoogleMapssupportedrecentversions

ofFirefox/Mozilla,IE5.5+,andSafari1.2+,and"sortof"

supportedOpera.ItdidnotsupportIE5.0.

"UsetheGoogleMapsAPIRequestObject"[Hack#29]and

"UseAjaxwithaGoogleMapsandYahoo!MapsMash-up"[Hack

#30]showhowtousetheGoogleMapsAPI.VisittheabovementionedURLtoobtainanAPIkey,takealookatthe

developer'sdocumentationat

http://www.google.com/apis/maps/documentation/,andstart

cracking!



Hack29.UsetheGoogleMapsAPIRequest

Object



TheGoogleMapsAPIcomeswithitsownrequestobject

formakingHTTPrequestsfromJavaScriptcode.

ThishackinitiallydisplaysaGoogleMapbasedonauser's

preferences.Theseincludethelatitude/longitudeatwhichthe

mapshouldbecentered,andthezoomlevelormagnificationof

themapwhenitisfirstdisplayedonthewebpage.An

applicationtypicallyobtainsuser-specificpropertiesbyreading

acookie,asmallpieceofdatasavedonauser'sharddrive,or

havingausersignin.Thishackskipsthisopeningstepinorder

tofocusonthegistofthehack'stechnology:obtaininguser

preferencesfromaservercomponenttocontrolaGoogleMap

display.



"SendCookieValuestoaServerProgram"[Hack#38]discusses

readingcookiesinanAjaxapplication.



PersonalGoogling

Thishackdisplaysa500-by-300-pixelU.S.maponawebpage,

whichalsoshowstheuser'spreferredcoordinatesforcentering

themapandpreferredzoomlevel(atwo-digitnumberfromthe

highestzoomlevelof1toaround18).Azoomlevelof18,for

instance,showsthecontinentsandoceans,whereasazoom



levelof1displaysatown'sstreets.

Asmentionedpreviously,whentheuserrequeststhisweb

page,theapplicationcaneitherobtaintheuser'susername

fromapreviouslygeneratedcookie,orasktheusertosignin

andfetchthepreferencesfromadatabase.However,weare

notgoingtoshowthatstep(eventhoughitisimportantina

real-worldapplication)becausewesurmisethatthereaderis

moreinterestedintheAPI'sAjax-relatedobjectsandthemapdisplaycode.

HereistheHTMLforthehack:




"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">


"urn:schemas-microsoft-com:vml">




PEMs2vnU_f0RhwHhZhQ6pfwiB1eVXKVVHswEcdvw4p5NixS195EO7O7VmH483DM

type="text/javascript">



ViewMap





YourGoogleMap





Yourspecifications







Latitude:





Longitude:





Zoomlevel:





Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 28. Get Access to the Google Maps API

Tải bản đầy đủ ngay(0 tr)

×