通过AJAX获取位置信息后创建地图
我需要通过AJAX从文本文件中获取位置的经度和纬度,然后使用该信息创建地图。这就是我所做的:通过AJAX获取位置信息后创建地图,ajax,google-maps,google-maps-api-3,Ajax,Google Maps,Google Maps Api 3,我需要通过AJAX从文本文件中获取位置的经度和纬度,然后使用该信息创建地图。这就是我所做的: function createMap(){ var request; if (window.XMLHttpRequest) { request = new XMLHttpRequest(); } if (request) { var number = Math.floor( (Math.random() * 3) + 1 ); var url = "text_" + nu
function createMap(){
var request;
if (window.XMLHttpRequest)
{
request = new XMLHttpRequest();
}
if (request)
{
var number = Math.floor( (Math.random() * 3) + 1 );
var url = "text_" + number + ".txt";
request.open("GET", url,true);
request.onreadystatechange = function()
{
if (request.readyState == 4 && request.status == 200)
{
var syd=new google.maps.LatLng(-33.884183, 151.214944);
var woll=new google.maps.LatLng(request.responseText);
function initialize()
{
var mapProp = {
center:syd,
zoom:6,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map=new google.maps.Map(document.getElementById("outputAJAX"),mapProp);
var myTrip=[syd,woll];
var flightPath=new google.maps.Polyline({
path:myTrip,
strokeColor:"#0000FF",
strokeOpacity:0.8,
strokeWeight:2
});
flightPath.setMap(map);
}
initialize();
}
}
request.send();
} else {
document.getElementById("outputAJAX").innerHTML = "<span style='font-weight:bold;'>AJAX</span> is not supported by this browser!";
}}
函数createMap(){
var请求;
if(window.XMLHttpRequest)
{
请求=新的XMLHttpRequest();
}
如果(请求)
{
变量编号=数学楼层((数学随机()*3)+1);
var url=“text”+number+“.txt”;
打开(“获取”,url,true);
request.onreadystatechange=函数()
{
if(request.readyState==4&&request.status==200)
{
var syd=new google.maps.LatLng(-33.884183151.214944);
var woll=new google.maps.LatLng(request.responseText);
函数初始化()
{
var mapProp={
中心:syd,
缩放:6,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“outputAJAX”),mapProp);
var myTrip=[syd,woll];
var flightPath=new google.maps.Polyline({
路径:myTrip,
strokeColor:#0000FF“,
笔划不透明度:0.8,
冲程重量:2
});
flightPath.setMap(map);
}
初始化();
}
}
request.send();
}否则{
document.getElementById(“outputAJAX”).innerHTML=“此浏览器不支持AJAX!”;
}}
然而,地图没有出现。你们有什么建议吗?A以两个数字作为参数:
LatLng(lat:number, lng:number, noWrap?:boolean) Creates a LatLng object representing a geographic point. Latitude is specified in degrees within the range [-90, 90]. Longitude is specified in degrees within the range [-180, 180]. Set noWrap to true to enable values outside of this range. Note the ordering of latitude and longitude.
假设request.responseText
是一个逗号分隔的字符串,包含两个数值,则应该可以:
var coordStr = request.responseText;
var coords = coordStr.split(",");
var woll = new google.maps.LatLng(parseFloat(coords[0]),
parseFloat(coords[1]));
什么是
响应.请求.文本
?我有3个文本文件,它们的经纬度是3个不同的地方,如下所示:-31.946922,115.853511
-34.424299,150.885959
-37.854861,145.052150谢谢,我想你指出了其中一个问题。然而,当我把它添加到我的代码中时,它仍然不起作用。我的问题是,它能用AJAX运行吗?应该能够。你现在犯了什么错误?回调函数中的request.responseText
值是多少?你的HTML是什么样子的?我没有收到任何错误。我正在使用xampp并尝试使用它运行代码。request.responseText的值是从文本文件中提取的字符串。我的HTML没有太多内容,只需:单击按钮创建一个新的映射
var coordStr = request.responseText;
var coords = coordStr.split(",");
var woll = new google.maps.LatLng(parseFloat(coords[0]),
parseFloat(coords[1]));