gmaps.js从PHP数组添加标记 [英] gmaps.js add marker from PHP array

查看:81
本文介绍了gmaps.js从PHP数组添加标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我的gmaps.js地图添加一个标记.长.和拉特.值存储在数据库中,我使用PHP脚本选择该数据库.

I am trying to add a marker to my gmaps.js map. The long. and lat. values are stored in a database, which I select using a PHP Script.

一次取回,两者都很长.和拉特.值存储在变量$ data下的数组中.

Once fetched, both of the long. and lat. values are stored in an array, under the variable $data.

我想使用json_encode($ data);为了传递变量,但是我不得不将标头更改为json应用程序,我已经在页面上回显了一些PHP,因此这是不可行的.

I wanted to use json_encode($data); in order to pass the variables, however I would have had to change the header to a json application, I have already echoed some PHP onto the page, therefore this is unfeasable.

我现在尝试使用$ .each循环将json_encode回显到jQuery函数中,但是它并不成功,因为并非数据库检索的每个条目都会有一个标记.

I have now tried echoing the json_encode into the jQuery function using an $.each loop, however it has been unsuccessful, as not every entry retrieved by the database will have a marker.

获得ID的最佳解决方案是什么?和拉特.最多10条记录的值,将此值传递给jQuery函数以在地图上定位?

What is the best solution, to get an ID, long. and lat. value for max 10 records, pass this to a jQuery function to position on a map?

推荐答案

由于并非所有记录都具有标记,因此您必须解析结果,以便仅将必需的数据传递给js函数.我将创建一个php数组并添加标记,例如lat,long等.在同一键下:

As not all records have markers, youll have to parse the results so that only the required data gets passed to your js function. I'd create a php array and add the marker I'd, lat,long etc... Under the same key:

$markersarray = array ()
foreach($records as $record){

  if($record['lat'] != ""){
    $markersarray[] = array(
         'id'=>$record['id'], 
         'lat'=>$record['lat'],
         'long'=>$record['long']
    );
  }
}

$markersjson = json_encode($markersarray);

然后,您可以使用隐藏的输入来存储编码的json字符串,然后可以将其传递到您的js中.

Then you could use a hidden input to store the encoded json string which then could be passed into your js.

 <input id="markersjson" type="hidden" value="<?php echo $markersjson; ?>"/>

在您的js函数中

var markersdata = $('#markersjson').val();

您可能需要将此值解析为一个对象

You may need to parse this value to an object

var obj = jQuery.parseJSON(markersdata);

然后使用$ .each遍历您的obj

then use a $.each to loop through your obj

这篇关于gmaps.js从PHP数组添加标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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