admin管理员组

文章数量:1318335

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=";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

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./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

Share Improve this question edited Oct 16, 2012 at 7:05 user1120260 asked Oct 16, 2012 at 6:43 user1120260user1120260 3854 gold badges13 silver badges29 bronze badges 1
  • 1 yes it is possible, please show what you have tried besides finding the tutorial code – Michal Klouda Commented Oct 16, 2012 at 6:48
Add a ment  | 

3 Answers 3

Reset to default 4

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

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

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

Greets!

Maybe something like :

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

本文标签: javascriptGoogle Map set locationStack Overflow