Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head>
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
- <style type="text/css">
- html { height: 100% }
- body { height: 100%; margin: 0px; padding: 0px }
- #map_canvas { height: 100% }
- </style>
- <script type="text/javascript"
- src="http://maps.google.com/maps/api/js?sensor=false">
- </script>
- <script type="text/javascript">
- //Declare the variable that will store the geocode object
- var geocoder;
- var map;
- var markers = [];
- function initialize() {
- //Set the geocoder variable equal to an instance of the google maps geocoder object as new google.maps.Geocoder()
- geocoder = new google.maps.Geocoder();
- var latlng = new google.maps.LatLng(42.095287, -79.3185139);
- var myOptions = {
- zoom: 10,
- center: latlng,
- mapTypeId: google.maps.MapTypeId.ROADMAP
- };
- map = new google.maps.Map(document.getElementById("map_canvas"),
- myOptions);
- }
- // Sets the map on all markers in the array.
- function setAllMap(map) {
- for (var i = 0; i < markers.length; i++) {
- markers[i].setMap(map);
- }
- }
- // Removes the overlays from the map, but keeps them in the array.
- function clearOverlays() {
- setAllMap(null);
- }
- // Shows any overlays currently in the array.
- function showOverlays() {
- setAllMap(map);
- }
- // Deletes all markers in the array by removing references to them.
- function deleteOverlays() {
- clearOverlays();
- markers = [];
- }
- //Add a second function to your javascript code, call it codeAddress. It will not have any values passed to it.
- function codeAddress() {
- //The first line of the function should use getElementById to get the address from the text box and place it
- //into a variable we'll call sAddress.
- var sAddress = document.getElementById("inputTextAddress").value;
- //Call the geocode method of the geocoder object, this will take two passed in parameters. The first is
- //the GeocoderRequest, this says what kind of request is being made and what the request value is.
- //The second is the callback function that will be used to process the results.
- geocoder.geocode( { 'address': sAddress}, function(results, status) {
- //The callback function should first check the status value of the callback function. Use an IF statement
- //to test the result, check to see if the status equal google.maps.GeocoderStatus.OK. Also add an
- //ELSE clause to the if statement as well.
- if (status == google.maps.GeocoderStatus.OK) {
- //If the status equals OK, call the setCenter method of the map object variable. You will pass this
- //method the results first geometry location.
- map.setCenter(results[0].geometry.location);
- //Next use the same result geometry location to add a map marker to the map object variable. Create a new
- //variable, we'll call it oMarker, it will be created as a new google.maps.Marker. The new method take two
- //parmaters, the first is the map object that you're adding the marker to, and the second is the
- //position to place the marker which is again the first results geometry location.
- deleteOverlays();
- alert("mgagg"+results[0]);
- var marker = new google.maps.Marker({
- map: map,
- position: results[0].geometry.location
- });
- markers.push(marker);
- } else {
- //Finally we're going to add an alert message to the ELSE to let the user know that the Geocode didn't
- //work like it should have. You can use the status to give a bit more information rather than just saying
- //that it didn't work.
- alert("Geocode was not successful for the following reason: " + status);
- }
- });
- }
- </script>
- </head>
- <body onload="initialize()">
- <!--Create a text box input for the user to enter the street address-->
- Address: <input type="text" id="inputTextAddress" style=" width:300px" title="Address to Geocode"/>
- <!--Create a button input for the user to click to geocode the address-->
- <input type="button" onclick="codeAddress()" id="inputButtonGeocode" style="width:20px" title="Click" value="Click" />
- <div id="map_canvas" style="width:800px; height:600px"></div>
- </div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement