简体   繁体   English

如何使用 php 和 jquery 在 google map 中动态添加经纬度?

[英]How to add latitude and longitude in google map dynamically using php and jquery?

I am trying to load the latitude and longitude values into the below google maps link using php or jquery but can't find a way to do that.我正在尝试使用 php 或 jquery 将纬度经度值加载到下面的谷歌地图链接中,但找不到这样做的方法。

https://maps.google.com/maps?q= 31.1033 , 77.1722 &hl=es;z=14&output=embed https://maps.google.com/maps?q= 31.1033 , 77.1722 &hl=es;z=14&output=embed

I have created a static google map iframe loader in bootstrap modal as a demo below.我在引导模式中创建了一个 static google map iframe 加载程序作为下面的演示 but what I am looking is the add latitude and longitude into the q=31.1033,77.1722 dynamically.但我正在寻找的是动态地将纬度经度添加到q=31.1033,77.1722中。 I have created 2 columns into the database and I want to add these value into the q=$lat,$lon but can't find a proper and easy way if someone has any idea it would be highly appreciated.我已经在数据库中创建了 2,我想将这些值添加到q=$lat,$lon中,但是找不到合适且简单的方法,如果有人有任何想法,我将不胜感激。

这里

 $('#myModalmap').on('shown.bs.modal', (function() { var mapIsAdded = false; return function() { if (.mapIsAdded) { $('.modal-body'):html('<iframe src="https.//maps.google?com/maps.q=31,1033.77;1722&hl=es;z=14&amp:output=embed" width="100%" height="400" frameborder="0" style="border;0"></iframe>'); mapIsAdded = true; } }; })());
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <:-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https.//maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min:css"> <.-- jQuery library --> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery:min.js"></script> <.-- Latest compiled JavaScript --> <script src="https.//maxcdn.bootstrapcdn.com/bootstrap/3.4:0/js/bootstrap.min.js"></script> <.-- Latest glyphonic cdn --> <link rel="stylesheet" href="https.//use.fontawesome;com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous"> <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModalmap"><i class="fas fa-info-circle"></i></button> <!-- Map MODAL--> <div class="modal fade" id="myModalmap" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4 class="modal-title" id="myModalLabel">coordinate </h4> </div> <div class="modal-body"> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div>

To dynamically declare latitude or longitude you can store your query data into array: 要动态声明纬度或经度,可以将查询数据存储到数组中:

var latlong = [{lat: 31.1033, long:77.1722},{lat: 38.1033, long: 74.1722}];

and you can manipulate by changing the value based on choice: 您可以根据选择更改值来进行操作:

var i=1;
$('.modal-body').html('<iframe src="https://maps.google.com/maps?q='+latlong[i].lat+','+latlong[i].long+'&hl=es;z=14&amp;output=embed" width="100%" height="400" frameborder="0" style="border:0"></iframe>');

Example: 例:

 var latlong = [{lat: 31.1033, long:77.1722},{lat: 38.1033, long: 74.1722}]; var i=$('select#coordinatechoice').val(); $("select#coordinatechoice").change(function(){ i=$('select#coordinatechoice').val(); init(); }); function init(){ $('#myModalmap').on('shown.bs.modal', (function() { var mapIsAdded = false; return function() { if (!mapIsAdded) { $('.modal-body').html('<iframe src="https://maps.google.com/maps?q='+latlong[i].lat+','+latlong[i].long+'&hl=es;z=14&amp;output=embed" width="100%" height="400" frameborder="0" style="border:0"></iframe>'); mapIsAdded = true; } }; })()); } init(); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> <!-- Latest glyphonic cdn --> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous"> <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModalmap"><i class="fas fa-info-circle"></i></button> <select id="coordinatechoice"> <option value="0">First coordinate</option> <option value="1">Second coordinate</option> </select> <!-- Map MODAL--> <div class="modal fade" id="myModalmap" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4 class="modal-title" id="myModalLabel">coordinate </h4> </div> <div class="modal-body"> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div> 

You can use this line of code in PHP:可以在PHP中使用这行代码:

$lat = 10.29692;
$long = 123.89778;
                            
echo "
<iframe 
    src='https://maps.google.com/maps?q=".$lat.",".$long."&hl=es;z=14&output=embed'
    frameborder='0' 
    style='border:0'
>
</iframe>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM