javascript - PHP MySQL 中的地理编码动态标记

标签 javascript php mysql google-maps google-maps-api-3

我正在尝试从数据库中获取坐标值,并在所有保存的位置上放置一个标记。我没有显示任何错误,但它没有在 map 上放置标记。任何帮助将不胜感激。

<div id="floating-panel">
  <input id="address" type="textbox" placeholder="type your address here">
  <input id="submit" type="button" value="Click for location">
</div>

<script>
  function initMap() {
      var mapProp = {
         center: {lat: 33.641656100000000000, lng: 72.983588599999960000},
          zoom:8,
          panControl:true,
          zoomControl:true,
          mapTypeControl:true,
          scaleControl:true,
          streetViewControl:true,
          overviewMapControl:true,
          rotateControl:true,    
         mapTypeId: google.maps.MapTypeId.ROADMAP
      };



var map = new google.maps.Map(document.getElementById('map'),mapProp);
    var geocoder = new google.maps.Geocoder();


    document.getElementById('submit').addEventListener('click', function() {
      geocodeAddress(geocoder, map);
    });
  }




  function geocodeAddress(geocoder, resultsMap) {
    var address = document.getElementById('address').value;
    geocoder.geocode({'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        resultsMap.setCenter(results[0].geometry.location.lat(),results[0].geometry.location.lat());
        var marker = new google.maps.Marker({
          map: resultsMap,
          position: results[0].geometry.location,
          animation:google.maps.Animation.BOUNCE
        });
        //var latitude = results[0].geometry.location.lat();
        //var longitude = results[0].geometry.location.lng();
        window.location = "distance_cal.php?Latitude=" +  results[0].geometry.location.lat() + "&Longitude="+ results[0].geometry.location.lng();

      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }

    });




        <?php
        $query = mysql_query("select * from saved_positions") or die (mysql_error());
        while($row=mysql_fetch_array($query))
        { 
            $scl=$row['school_name'];
            $city=$row['city'];
            $lon2=urlencode($row['longitude_s']);
            $lat2=urlencode($row['latitude_s']);
    ?>
        var lat='<?php echo '$lat2'?>';
        var lon='<?php echo '$lon2'?>';
        var myLatLng = {lat, lng};
        var marker = new google.maps.Marker({
          map: ResultMap,
          position: myLatLng,
          animation:google.maps.Animation.BOUNCE
        });
    <?php
        }
    ?>

  }
</script>

最佳答案

这是非常奇怪的代码。

var lat='<?php echo '$lat2'?>';
var lon='<?php echo '$lon2'?>';
var myLatLng = {lat, lng};

应该是这样的:

var lat = <?php echo $lat2?>;
var lon = <?php echo $lon2?>;
var myLatLng = {lat: lat, lng: lon};

您在代码中的何处定义了变量ResultMap

var marker = new google.maps.Marker({
      map: ResultMap,  // <--- where does this value come from?
      position: myLatLng,
      animation:google.maps.Animation.BOUNCE
});

我建议你使用任何模板系统(比如 smart 之类的)来分离 PHP 代码和 JS 代码。

关于javascript - PHP MySQL 中的地理编码动态标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35826902/

相关文章:

javascript - 三、TrackballControls 旋转中心

php - 如何从数据库中为数组中的每个索引选择

mysql - 使用 3 个表检索单行

python - Django-创建中间多对多模型时出错

mysql - 如何从社交网络sql数据库中通过单个查询获取 friend 的社交 map ?

javascript - 缩放 div 以适合窗口但保持纵横比

javascript - 如果无法建立 Websocket 连接,请执行其他操作

javascript - 顺序主干 model.set 覆盖 model.changed 属性

php - 从 php 运行可执行文件而不生成 shell

php - 如何使用PHP preg_replace 正则表达式查找和替换文本?