从mysql检索纬度和经度,并将其传递给Google Maps API [英] Retrieving latitude and longitude from mysql, passing it to google maps api

查看:54
本文介绍了从mysql检索纬度和经度,并将其传递给Google Maps API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个完整的初学者,只是想知道是否存在将这些位置传递到我的HTML脚本中的google maps的简单方法.

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(可行):

<?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(这是单独的.js的一部分,它可以工作):

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; 
    }
}

}

我只需要知道如何从我的JQuery中的 function success_locations()中添加这些位置到Google地图 var location [] 中,这些位置在我的HTML中:

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>

谢谢詹姆斯

推荐答案

此位没有多大意义:

    cafe_loc += venue_name + venue_lat + venue_long + id; 

您将获得一组不错的结果,将这些位吸入单个变量中,然后将它们全部合并为一个大字符串.例如对于JS代码中的示例位置,您实际上正在构建

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
   ];
}

这篇关于从mysql检索纬度和经度,并将其传递给Google Maps API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆