Javascript 使用Google Maps API V3构建坐标数组
我到处寻找,找到了一些很好的解决方案……但由于某些原因,它们对我不起作用 基本上,在一个div悬停事件中,我想根据数据库地理编码和检索的坐标绘制一条多段线。现在,控制台似乎可以很好地显示数组元素,但我确实认为这是我找不到的问题。我想我传递了一个构造不正确的数组来创建Javascript 使用Google Maps API V3构建坐标数组,javascript,php,ajax,maps,Javascript,Php,Ajax,Maps,我到处寻找,找到了一些很好的解决方案……但由于某些原因,它们对我不起作用 基本上,在一个div悬停事件中,我想根据数据库地理编码和检索的坐标绘制一条多段线。现在,控制台似乎可以很好地显示数组元素,但我确实认为这是我找不到的问题。我想我传递了一个构造不正确的数组来创建LatLng对象 这是我的地图: wgmpmap = new google.maps.Map(document.getElementById("wgmpmap"), mapOptions); 以下是我的功能: function hi
LatLng
对象
这是我的地图:
wgmpmap = new google.maps.Map(document.getElementById("wgmpmap"), mapOptions);
以下是我的功能:
function highlightRoute(zip,locationId){
jQuery.ajax({
type: "GET",
url: "goGetRoute.php?location_id=" + locationId + "&zip=" +zip,
dataType: "text", //expect html to be returned
success: function(response){
var drivePathCoords = [];
var tmpCoords = response.split("|");
for (i = 0; i < tmpCoords.length; i++) {
var tmpLatLng = tmpCoords[i].split(",");
drivePathCoords.push(new google.maps.LatLng(tmpLatLng[0], tmpLatLng[1]));
}
console.log(drivePathCoords.toString());
var drivePath = new google.maps.Polyline({
path: drivePathCoords,
geodesic: true,
strokeColor: '#008457',
strokeOpacity: 1.0,
strokeWeight: 2
});
drivePath.setMap(wgmpmap);
}//end success function
}); //end ajax call
}
您的
SELECT
语句中存在SQL注入漏洞。切换到受支持的数据库接口(例如PDO)并使用参数化来解决此问题。我同意PDO是一种解决方法,但您也可以使用mysqli(因为mysql已被弃用),它更接近mysql语句的“风格”。感谢您的建议,我将对此进行研究。@Shmoopy-是,尽管我最近向其他人建议这两种方法都很好,他们发现mysqli\u fetch\u all()
(我认为这可能是经常需要的)需要安装另一个PHP模块mysqlnd
。我不知道这是否通常包括在共享主机中,所以我倾向于安全起见,选择PDO.OP,我很难确定您的确切问题。您是否还会创建一个JS提琴来演示它?另外,您的浏览器控制台中是否有JS错误?
/////First geocode given zipcode
$tmpZip = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=$_GET[zip]"));
$tmpZipLat = $tmpZip->results[0]->geometry->location->lat;
$tmpZipLng = $tmpZip->results[0]->geometry->location->lng;
$fromLatLng = "$tmpZipLat,$tmpZipLng";
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'pass';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db("myDB",$conn);
$locMatchesQuery = mysql_query("SELECT location_latitude, location_longitude FROM wp_map_locations WHERE location_id = '$_GET[location_id]'");
if(mysql_num_rows($locMatchesQuery) != 0){
$rowLocation = mysql_fetch_array($locMatchesQuery);
$toLatLng = "$rowLocation[location_latitude],$rowLocation[location_longitude]";
$outString = "";
$tmp = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/directions/json?origin=$fromLatLng&destination=$toLatLng&mode=driving&alternatives=false&units=imperial"));
foreach($tmp->routes[0]->legs[0]->steps as $step){
$tmpLat = $step->start_location->lat;
$tmpLng = $step->start_location->lng;
$outString .= "$tmpLat,$tmpLng|";
}
echo rtrim($outString, "|");
}//End If
else{
}//End Else