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
       ];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)