【问题标题】:How to change a google map when click button input from form单击表单中的按钮输入时如何更改谷歌地图
【发布时间】:2019-07-31 04:13:45
【问题描述】:

我有这个工作代码,我的问题是当我点击地理定位按钮时,地图没有根据输入值加载。怎么做成jquery?,点击按钮不刷新页面,只有地图改变?

   <p>Click the button to get your coordinates.</p>

    <button onclick="getLocation()">Try It</button>

    <input id="lat" value="47.6145" />
    <input id="lng" value="-122.3418" />
    <div id="google_map"></div>


    <!-- Replace the value of the key parameter with your own API key. -->
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
    <script>


    function getLocation() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
      } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
      }
    }

    function showPosition(position) {

         $('#lat').val(position.coords.latitude);
        $('#lng').val(position.coords.longitude);
         //location.reload();

      x.innerHTML = "<p id='babi'>Latitude: " + position.coords.latitude + 
      "</p><br><p id='setan'>Longitude: " + position.coords.longitude +"</p>";
    }

    $(document).ready(function() {
    var lng = $("#lng").val();
    var lat = $("#lat").val();
    var mapCenter = new google.maps.LatLng(lat, lng);
    // var mapCenter = new google.maps.LatLng(47.6145, -122.3418); //Google map Coordinates
        var map;
        map_initialize(); // load map
        function map_initialize(){

            //Google map option
            var googleMapOptions = 
            { 
                center: mapCenter, // map center
                zoom: 17, //zoom level, 0 = earth view to higher value
                panControl: true, //enable pan Control
                zoomControl: true, //enable zoom control
                zoomControlOptions: {
                style: google.maps.ZoomControlStyle.SMALL //zoom control size
            },
                scaleControl: true, // enable scale control
                mapTypeId: google.maps.MapTypeId.ROADMAP // google map type
            };

            map = new google.maps.Map(document.getElementById("google_map"), googleMapOptions);     
        }
    });

    </script>

以下是工作示例的链接 https://jsfiddle.net/designblog4u/v7ofhem4/4/

点击按钮后以下代码不变:

var lng = $("#lng").val();
var lat = $("#lat").val();
var mapCenter = new google.maps.LatLng(lat, lng);

【问题讨论】:

    标签: jquery google-maps google-maps-api-3


    【解决方案1】:

    您正在搜索map.setCenter(latlng)

    function showPosition(position) {    
      $('#lat').val(position.coords.latitude);
      $('#lng').val(position.coords.longitude);
    
      map.setCenter(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));    
      ...
    }
    

    Fiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      相关资源
      最近更新 更多