360 likes | 649 Views
Using the Google Maps API. 1. Objectives. You will be able to Use the Google Maps API to display a map of any location on an HTML page. Programatically modify the location and zoom level of the map. The Google Maps API. Permits web applications to use functionality of google maps.
E N D
Objectives You will be able to • Use the Google Maps API to display a map of any location on an HTML page. • Programatically modify the location and zoom level of the map.
The Google Maps API • Permits web applications to use functionality of google maps. • Tutorial: https://developers.google.com/maps/documentation/javascript/tutorial 3
Getting Started • Create a new Empty Web Site in Visual Studio. • Google_Maps_Demo • .NET Framework 3.5 • Add an HTML page. • Map.html
Google Maps Tutorial 5 Scroll down
Obtaining an API Key No longer necessary. Ignore for now. Scroll down to Hello, World
You can copy and paste from this page, but some modification is necessary. Hello World
<!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <style type="text/css"> html { height: 100% } body { height: 100%; margin: 0; padding: 0 } #map_canvas { height: 100% } </style> <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"> </script> <script type="text/javascript"> function initialize() { var myOptions = { center: new google.maps.LatLng(-34.397, 150.644), zoom: 8, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); } </script> </head> <body onload="initialize()"> <div id="map_canvas" style="width:100%; height:100%"></div> </body> </html>
Map Options <script type="text/javascript"> function initialize() { var myOptions = { center: new google.maps.LatLng(-34.397, 150.644), zoom: 8, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); } </script> How can we determine latitude and longitude of the place we want to map? http://www.tech-recipes.com/rx/2403/google_maps_get_latitude_longitude_values/
Lat-Long appears in the Address search bar. Copy and paste into script.
Map with New Center Location Set zoom to 15
Adding Our Own Stuff • Let’s add text entry boxes to show and set the latitude and longitude. <body onload="initialize()"> <div id="map_canvas" style="width: 100%; height: 95%"> </div> <div> Latitude: <input type="text" id="tbLat" name="tbLat" /> Longitude: <input type="text" id="tbLong" name="tbLong" /> <input type="button" id="Set" value="Set" onclick="Set_Position();" name="btnSetPosition" /> </div> </body> 20
Show New Position At end of function initialize(), still inside the function body : google.maps.event.addListener(map, "idle", function() { var center = map.getCenter().toString(); var latlong = center.split(","); var tbLat = document.getElementById("tbLat"); tbLat.value = latlong[0].substring(1,8); latitude = tbLat.value; var tbLong = document.getElementById("tbLong"); tbLong.value = latlong[1].substring(0,9); longitude = tbLong.value; }); 21
Let the User Set the Position Add to the script below function initialize: function Set_Position() { var tbLat = document.getElementById("tbLat"); var tbLong = document.getElementById("tbLong"); latitude = tbLat.value; longitude = tbLong.value; map.setCenter( new google.maps.LatLng(latitude,longitude )); } Remove var from map = new google.maps.Map() 22
Setting the Position • Type lat-long into the text boxes • Click “Set” • Let’s look at Miami • Latitude 25.77 • Longitude -80.17 24
Miami 25
Find the Lat/Long of the Empire State Building • Zoom out • Move to NYC • Zoom in • Move to 5th Ave and 34th Street 26
Locate NYC 27
Zooming In 28
April 2009 29
Dec. 2009 30
April 2010 31
April 2011 32