javascript - How to pass Latitude Longitude from HTML form to Google Maps - Stack Overflow

I want to pass latitude and longitude from user. How I can pass parameters from html form to google map

I want to pass latitude and longitude from user. How I can pass parameters from html form to google map API? How to learn plete google map API step by step which start to end. and how to add more then one lat and lng to one map?

  <!DOCTYPE html>
    <html>
    <head>
    <script
    src="">
    </script>

    <script>
    var lat=51.508742;
    var lng=8.120850;
    var myCenter=new google.maps.LatLng(lat,lng);

    function initialize()
    {
    var mapProp = {
      center:myCenter,
      zoom:5,
      mapTypeId:google.maps.MapTypeId.ROADMAP
      };

    var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

    var marker=new google.maps.Marker({
      position:myCenter,
      });

    marker.setMap(map);
    }

    google.maps.event.addDomListener(window, 'load', initialize);
    </script>
    </head>

    <body>
<form id="form1">
    <table>
        <tr>
            <td>Enter Latitude:</td>
            <td>
                <input type="text" name="lat" value="13.053147716796578" />
            </td>
        </tr>
        <tr>
            <td>Enter Longitude:</td>
            <td>
                <input type="text" name="lng" value="80.2501953125" />
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button" value="Submit" />
            </td>
        </tr>
    </table>
    <div id="googleMap" style="width:500px;height:380px;"></div>
</form>

    </body>
    </html>

I want to pass latitude and longitude from user. How I can pass parameters from html form to google map API? How to learn plete google map API step by step which start to end. and how to add more then one lat and lng to one map?

  <!DOCTYPE html>
    <html>
    <head>
    <script
    src="http://maps.googleapis./maps/api/js">
    </script>

    <script>
    var lat=51.508742;
    var lng=8.120850;
    var myCenter=new google.maps.LatLng(lat,lng);

    function initialize()
    {
    var mapProp = {
      center:myCenter,
      zoom:5,
      mapTypeId:google.maps.MapTypeId.ROADMAP
      };

    var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

    var marker=new google.maps.Marker({
      position:myCenter,
      });

    marker.setMap(map);
    }

    google.maps.event.addDomListener(window, 'load', initialize);
    </script>
    </head>

    <body>
<form id="form1">
    <table>
        <tr>
            <td>Enter Latitude:</td>
            <td>
                <input type="text" name="lat" value="13.053147716796578" />
            </td>
        </tr>
        <tr>
            <td>Enter Longitude:</td>
            <td>
                <input type="text" name="lng" value="80.2501953125" />
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button" value="Submit" />
            </td>
        </tr>
    </table>
    <div id="googleMap" style="width:500px;height:380px;"></div>
</form>

    </body>
    </html>
Share Improve this question edited Mar 7, 2015 at 1:33 Rodrigo Taboada 2,7054 gold badges26 silver badges27 bronze badges asked Mar 2, 2015 at 16:36 Muhammad Sohaib RoomiMuhammad Sohaib Roomi 451 gold badge2 silver badges8 bronze badges 1
  • I am sure that Google's documentation and tutorials should explain this. Have you looked there? – RudolphEst Commented Mar 2, 2015 at 16:52
Add a ment  | 

2 Answers 2

Reset to default 2
  1. Use a google.maps.event.addDomListener to listen to the form submit event.
  2. Give your lat and lng input fields an ID so that you can identify them.
  3. Create a marker from the form values (and/or center the map on the location).

You also want to validate the user input. You should check that the user entered numbers and that they are valid lat and lng values.

More information here on how to validate the latitude value on a mercator projection.

Here is a plete and working example to achieve what you want:

HTML

<form id="mapCenterForm">
    Lat: <input type="text" id="lat" />
    <br />
    Lng: <input type="text" id="lng" />
    <br />
    <input type="submit" value="Center map" />
</form>
<br />
<div id="map-canvas"></div>

JavaScript

// Calculate maximum latitude value on mercator projection
var maxLat = Math.atan(Math.sinh(Math.PI)) * 180 / Math.PI;

function initialize() {

    var center = new google.maps.LatLng(0, 0);

    var mapOptions = {
        zoom: 3,
        center: center,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

    // DOM event listener for the center map form submit
    google.maps.event.addDomListener(document.getElementById('mapCenterForm'), 'submit', function(e) {

        e.preventDefault();

        // Get lat and lng values from input fields
        var lat = document.getElementById('lat').value;
        var lng = document.getElementById('lng').value;

        // Validate user input as numbers
        lat = (!isNumber(lat) ? 0 : lat);
        lng = (!isNumber(lng) ? 0 : lng);

        // Validate user input as valid lat/lng values
        lat = latRange(lat);
        lng = lngRange(lng);

        // Replace input values
        document.getElementById('lat').value = lat;
        document.getElementById('lng').value = lng;

        // Create LatLng object
        var mapCenter = new google.maps.LatLng(lat, lng);

        new google.maps.Marker({

            position: mapCenter,
            title: 'Marker title',
            map: map
        });

        // Center map
        map.setCenter(mapCenter);
    });
}

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

function latRange(n) {
    return Math.min(Math.max(parseInt(n), -maxLat), maxLat);
}

function lngRange(n) {
    return Math.min(Math.max(parseInt(n), -180), 180);
}

initialize();

Demo

JSFiddle demo

Give the elements IDs, like:

<input type="text" id="lat" name="lat" value="13.053147716796578" />

Just use Javascript to get the values:

var lat=document.getElementById('lat').value;
var lng=document.getElementById('lng').value;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744217133a4563606.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信