如何在img src链接中放置变量

时间:2016-12-29 06:59:19

标签: javascript html google-maps

我有两个变量:纬度和经度:    我想像这样插入它。

<img src = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=15&size=640x640&scale=2&key=example"; 这是我的代码:

<head>
<script type="text/javascript">


    function TestGeo() {
      if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition( TestMap, error, {maximumAge: 30000, timeout: 10000, enableHighAccuracy: false} );
      } else {
          alert("Sorry, but it looks like your browser does not support geolocation.");
            }
        function TestMap(position) {
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;}
        </script>

        </head>
        <body onload="TestGeo();">
        <img src = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=15&size=640x640&scale=2&key=example";
        </body>
        </html>

1 个答案:

答案 0 :(得分:0)

如果您使用的是javascript。试试这个

            function TestMap(position) {
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
            var imgsrc = "https://maps.googleapis.com/maps/api/staticmap?center="+latitude+","+longitude+"&zoom=15&size=640x640&scale=2&key=example";
            $('#imgselector').attr('src', imgsrc)
            }

html应该是 <img src="" id="imgselector"/>