dongzu0742 2014-04-29 20:11
浏览 34
已采纳

从mysql中检索纬度和经度,将其传递给google maps api

I am a complete beginner and just wanting to know if there is a simple was of passing these locations to google maps which is in a script in my HTML.

I can retrieve the coordinates fine like this

PHP (this works):

<?php
require_once "sql.php";

error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors','On');

    $result = array();
    $result['success'] = false;

    $order_by = $_REQUEST['sort_by'];

    $query = "SELECT venue_name, latitude, longitude, id  FROM venues";
        if ($result_set = Sql::query($query)) {
            $result['success'] = true;
            $result['message'] = "All Venues" ;
            $rows = mysqli_num_rows($result_set);
            $result['rows'] = array();
            for ($i = 0; $i<$rows; $i++) {
                $tmpRow = mysqli_fetch_assoc($result_set);
                $result['rows'][$i] = $tmpRow;
            }
        } else {
             $result['message'] = "Failed to read Cafes" ;
        }

        print(json_encode($result)); 

JQuery (part of a separate .js, this works):

function success_get_locations(response) {
if(response.success) {
    var cafe_loc = '';
    for (var i = 0; i < response.rows.length; i++) {
        var venue_name = response.rows[i].venue_name;
        var venue_lat = response.rows[i].latitude;
        var venue_long = response.rows[i].longitude;
        var id = response.rows[i].id;

        cafe_loc += venue_name + venue_lat + venue_long + id; 
    }
}

}

I just need to know how to add these locations from function success_locations() in my JQuery to google maps var locations [] which is in my HTML here:

<div id="map" style="width: 500px; height: 400px;"></div>

<script type="text/javascript">

var locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]
];

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 10,
  center: new google.maps.LatLng(-33.92, 151.25),
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {  
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
  });

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i][0]);
      infowindow.open(map, marker);
    }
  })(marker, i));
}
</script>

thanks James

  • 写回答

1条回答 默认 最新

  • duannai5879 2014-04-29 20:18
    关注

    This bit doesn't make much sense:

        cafe_loc += venue_name + venue_lat + venue_long + id; 
    

    You're taking the nice array of results, sucking out the bits into individual variables, then concatentating them all into one big string. e.g. For your sample locations in the JS code, you'd actually be building

    Bondi Beach-33.890542151.2748564
    

    Why not just leave them as the array they are already? Or if the ordering of the array isn't correct, you could build a NEW array with the elements in the proper order:

    locs_for_google = [];
    for (var i = 0; i < response.rows.length; i++) {
       locs_for_google[i] = [
           response.rows[i].venue_name,
           response.rows[i].latitude,
           response.rows[i].longitude,
           response.rows[i].id
       ];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料