douwei1128 2015-10-27 00:18
浏览 132
已采纳

将PHP变量传递给Google Maps API

When a user signs up to my application (college project), they are asked for their address. I convert this address to latitude and longitude and then store it in my database.

I access the latitude and longitude like so:

<?php
    require '../connect.php';
    session_start();
    $_SESSION['username'];

    $sql = "SELECT latitude, longitude FROM userinformation WHERE username = '". $_SESSION['username']. "'";
$result = $db->query($sql);

    if($result->num_rows > 0){
        while($row = $result->fetch_assoc()){
            $lat = $row["latitude"];
            $lng = $row["longitude"];
            echo $lat;
            echo $lng;

        }
    }
?>

I then try to pass the $lat and $lng variable into the Maps API script like so:

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 13,
    center: {lat: '<?php echo $lat;?>', lng: '<?php echo $lng;?>'}
  });

  marker = new google.maps.Marker({
    map: map,
    draggable: true,
    animation: google.maps.Animation.DROP,
    position: {lat: '<?php echo $lat;?>', lng: '<?php echo $lng;?>'}
  });
  marker.addListener('click', toggleBounce);
}

This gives me the following page:

PHP Page

I have also tried converted the PHP variables to JS:

var js_variable  = '<?php echo $lat;?>';
document.write(js_variable);
var js_variable1  = '<?php echo $lng;?>';
document.write(js_variable1);

I then pass in the JS variables instead of the PHP variables, however the output is still the same. What would be the correct way to pass a variable as lat/lng instead of a hardcoded value:

center: {lat: 59.325, lng: 18.070}
position: {lat: 59.327, lng: 18.067}
  • 写回答

2条回答 默认 最新

  • doushou6480 2015-10-27 01:37
    关注

    When I replace the values in the fiddle with strings {lat: '30.365273', lng: '-81.699600'}, I get a javascript error reported by the API: Assertion failed: InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in property lat: not a number.

    If I use parseFloat on those strings, it works:

    code snippet:

    function initMap() {
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 13,
        center: {
          lat: parseFloat('30.365273'),
          lng: parseFloat('-81.699600')
        }
      });
    
      marker = new google.maps.Marker({
        map: map,
        draggable: true,
        animation: google.maps.Animation.DROP,
        position: {
          lat: parseFloat('30.365273'),
          lng: parseFloat('-81.699600')
        }
      });
      marker.addListener('click', toggleBounce);
    }
    google.maps.event.addDomListener(window, "load", initMap);
    html,
    body,
    #map {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="map"></div>

    change your PHP to be:

    function initMap() {
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 13,
        center: {lat: parseFloat('<?php echo $lat;?>'), lng: parseFloat('<?php echo $lng;?>')}
      });
    
      marker = new google.maps.Marker({
        map: map,
        draggable: true,
        animation: google.maps.Animation.DROP,
        position: {lat: parseFloat('<?php echo $lat;?>'), lng: parseFloat('<?php echo $lng;?>')}
      });
      marker.addListener('click', toggleBounce);
    }
    
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值