douji6667 2015-08-28 17:54
浏览 25
已采纳

如何在javascript中使用php

I have created a variable ($latlon) in php via an sql query like so...

// ...
if ($result->num_rows >0) {
    while ($row = $result->fetch_assoc()) {
        echo "id: " . $row["userid"] . latitude: . $row["latitude"] . 
        longitude: . $row["longitude"] ;

         $latlon = $row["latitude"] . "," . $row["longitude"];

and now i would like to use either the variable $latlon or $row["latitude"] & $row["longitude"] in some javascript code that displays a map which looks like this. Ideally i would like my php variable to go in here (51.508742,-0.120850) because these coords will continually change.

<script>
function initialize()
{
    var mapProp = {
        center: new google.maps.LatLng**(51.508742,-0.120850)**,
        zoom:7,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
}

function loadScript()
{
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.googleapis.com/maps/api/
    js?key=&sensor=false&callback=initialize";
    document.body.appendChild(script);
}
window.onload = loadScript;
</script>

any help will be appreciated. thank you

  • 写回答

3条回答 默认 最新

  • duanpo7282 2015-08-28 18:01
    关注

    You should echo the $latlon variable inside php tag. So the line will be as following: new google.maps.LatLng(<?php echo $latlon;?>). I assume you have the correct syntax for the js in the php $latlon variable.

    Remember, this will only work if it is a php file you are placing the code on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分