【问题标题】:Fetching longitude and latitude from the database从数据库中获取经度和纬度
【发布时间】:2019-08-04 23:51:41
【问题描述】:

我希望位置变量从数据库中获取经度和纬度并显示它们的集群 请在这方面需要帮助我希望从具有存储这些数据的表的数据库中获取所有经度和纬度

<script>

  function initMap() {

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 3,
      //mkoa wa dodoma location
      center: {lat: -6.1630, lng: 35.7516}
    });

    // Create an array of alphabetical characters used to label the markers.
    var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

    // Add some markers to the map.
    // Note: The code uses the JavaScript Array.prototype.map() method to
    // create an array of markers based on a given "locations" array.
    // The map() method here has nothing to do with the Google Maps API.
    var markers = locations.map(function(location, i) {
      return new google.maps.Marker({
        position: location,
        label: labels[i % labels.length]
      });
    });

    // Add a marker clusterer to manage the markers.
    var markerCluster = new MarkerClusterer(map, markers,
        {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
  }
   //kwajiri ya miji
  var locations = [
    {lat: -6.1730, lng: 35.7516},
    {lat: -6.2330, lng: 35.7523},
    {lat: -6.2330, lng: 35.7523},
    {lat: -6.2330, lng: 35.7523},
    {lat: -6.2330, lng: 35.7523},
    {lat: -6.1730, lng: 35.7516},
    {lat: -6.1730, lng: 35.7516},
    {lat: -7.773094,lng: 35.699120},
    {lat: -7.773094,lng: 35.699120},
    {lat: -7.773094,lng: 35.699120},
    {lat: -5.030461,lng: 32.819431},
    {lat: -5.030461,lng: 32.819431},
    {lat: -5.030461,lng: 32.819431},
    {lat: -5.030461,lng: 32.819431},
    {lat: -8.909401,lng: 33.460773},
    {lat: -8.909401,lng: 33.460773},
    {lat: -8.909401,lng: 33.460773},
    {lat: -6.802353,lng: 39.279556},
    {lat: -6.802353,lng: 39.279556},
    {lat: -6.802353,lng: 39.279556},
    {lat: -6.802100,lng: 39.279119},
    {lat: -6.1730, lng: 35.7516}
  ]
</script>

如你所见,变量位置在数组中

【问题讨论】:

  • 你的问题不完整,告诉我们你到目前为止做了什么,你想用它做什么。
  • 我已经更新了,请检查
  • 您必须提供表格样本以及经纬度数据存储在表格中的方式,以及您的 PHP 代码。它是 javascript 你在使用 AJAX 吗?或者您想在发布表单后获取。
  • 我想使用 Ajax 从数据库中获取数据,但是数据库中的结构具有 ID LONGITUDE LATITUDE
  • 我只想修改变量位置只有 long 和 lat 不能是静态的我希望变量从数据库中获取这些字段@SayedMohdAli

标签: javascript php json mysqli


【解决方案1】:

客户端脚本 AJAX 调用可能如下所示:

$.ajax({
    type: "GET",
    url : "https://epicsite.com/getLocations.php",
    success : function (data) 
    {
        if(data.error) {
            console.log(data.error);
        } else {
            console.log(data);
            callFunctionToAddMarkersToMap(data);
        }
    }
});

服务器端脚本可能如下所示:

<?php
    $con = mysqli_connect('localhost','user','pass','db');
    if (!$con) {
        die('Could not connect: ' . mysqli_error($con));
    }

    mysqli_select_db($con,"locations");
    $sql="SELECT * FROM locations";
    $result = mysqli_query($con,$sql);
    mysqli_close($con);

    return $result;
?>

因此,首先在您的客户端脚本中,您要调用将获取标记坐标的 AJAX 路由。然后它将调用函数callFunctionToAddMarkersToMap(data),并将查询结果作为参数传递。然后在函数中,您只需循环给定查询结果并向地图添加标记。

【讨论】:

  • @pangrasmwanossa 是的
  • 仍然未能实现@mpak
猜你喜欢
  • 2012-12-06
  • 1970-01-01
  • 2013-07-07
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多