如何使用php和jquery在Google地图中动态添加纬度和经度?

时间:2019-03-29 06:38:40

标签: javascript php jquery

我正在尝试使用php或jquery将纬度经度值加载到下面的Google Maps链接中,但是找不到做到这一点的方法。

  

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

我在下面的 demo 中以引导程序模式创建了一个静态的google map iframe加载器。但我正在寻找的是动态地将纬度经度添加到 q = 31.1033,77.1722 中。我在数据库中创建了2个,我想将这些值添加到 q = $ lat,$ lon 中,但是如果有人有任何想法,将不胜感激。

here

	 $('#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>

	

1 个答案:

答案 0 :(得分:1)

要动态声明纬度或经度,您可以将查询数据存储到数组中:

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

您可以根据选择更改值来进行操作:

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>');

示例:

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>