user1120260
user1120260

Reputation: 385

Google Map set location?

I have this code that shows the current location as being specified in the "LatLng" coordinates. But what I want is have have an input boxes, lets say 2 input boxes "location from" and "location to." I wish to show the current location of the given input values from the 2 boxes as being indicated.

Is that possible in Google Map V3?

I want to have the mapOptions values will be the values inputs in the boxes. How to do that?

Here is my code.

<!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?key=AIzaSyBQ8OCC8En5vNHod25Ov3Qs5E1v7NPRSsg&sensor=true">
    </script>
    <script type="text/javascript">
      function initialize() {
                    // add mapOptions here to the values in the input boxes.
        var mapOptions = {
          center: new google.maps.LatLng(-34.397, 100.644),
          zoom: 2,
          mapTypeId: google.maps.MapTypeId.ROADMAP  
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
      }
    </script>
  </head>
  <body>

    From: <input type="text" name="From"><br>
    To: <input type="text" name="To"><br>

    <button type="button" onclick="initialize()">View example (map-simple.html)</button>

    <div id="map_canvas" style="width:100%; height:100%"></div>
  </body>
</html>

Thanks

Jason

Upvotes: 2

Views: 11250

Answers (3)

Pragnesh Chauhan
Pragnesh Chauhan

Reputation: 8476

you should use geocoder for that

geocoder = new google.maps.Geocoder();      
var address = document.getElementById('address').value; //input box value
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
    map.setCenter(results[0].geometry.location);
    var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
    });
}
});  

Map Fiddle

Upvotes: 4

You may check the examples on this address: https://developers.google.com/maps/documentation/javascript/demogallery

Here you can find the answer for your question also more advanced tecqniques that you may need on further.

Greets!

Upvotes: 1

Overnuts
Overnuts

Reputation: 803

Maybe something like :

boxLat.value = map.getCenter().getLat() ;
boxLng.value = map.getCenter().getLng() ;

Upvotes: 0

Related Questions