我使用livecode和google地图api获取2个标记和距标记的距离。 html文件位于本地特殊文件夹(ios-android)。当应用程序打开时,地图显示正确,在html文件上存在一行来计算数据<body onload="calculateDistances(); ">
但我想获取数据(calculateDistances结果)来自特定<div id="outputDiv"></div>
的没有成功。 当我把娄代码我得到什么从livecode中的html文件中提取数据
put url theURL into tMap
,或者如果我把娄代码我得到的文件路径
put url "url",theURL into tMap
这里是代码livecode部分
global myarray
Global browserID
Global theUrl
global tTitles
global thehtmlFile
global theData
on preopencard
if the environment is not "mobile" then exit preopencard
-- Create our browser control and store the id
mobileControlCreate "browser"
put the result into BrowserID
mobileControlSet browserID, "rect", the rect of group "tbrowser"
mobileControlSet browserID, "visible", "true"
mobileControlSet BrowserID, "url", theURL
set the text of fld "content" to theData
end preOpenCard
on closeCard
put empty into fld "lat" of card drecords
put empty into fld "long" of card drecords
if the environment is not "mobile" then
exit closeCard
end if
-- Destroy the control, if we fail to do this native UIViews
-- will just accumulate
mobileControlDelete BrowserID
delete URL("file:" & thehtmlFile)
end closeCard
这里是我正在使用的html代码
<!DOCTYPE html>
<html>
<head>
<!-- <title>Distance Matrix service</title> -->
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&language=el&sensor=false"></script>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map-canvas {
height: 99%;
width: 100%;
}
#content-pane {
width: 100%;
}
#outputDiv {
font-size:smaller;
position: relative;
bottom: 60px;
}
</style>
<script>
var map;
var geocoder;
var bounds = new google.maps.LatLngBounds();
var markersArray = [];
var origin1 = new google.maps.LatLng(38.078459, 23.734718);
var destinationA = new google.maps.LatLng(38.078833, 23.736843);
var destinationIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=S|FF0000|000000';
var originIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=M|FFFF00|000000';
function initialize() {
var opts = {
center: origin1,
zoom: 10,
disableDefaultUI: true
};
map = new google.maps.Map(document.getElementById('map-canvas'), opts);
geocoder = new google.maps.Geocoder();
}
function calculateDistances() {
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin1,],
destinations: [destinationA,],
travelMode: google.maps.TravelMode.WALKING,
unitSystem: google.maps.UnitSystem.METRIC,
}, callback);
}
function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var outputDiv = document.getElementById('outputDiv');
outputDiv.innerHTML = '';
deleteOverlays();
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
addMarker(origins[i], false);
for (var j = 0; j < results.length; j++) {
addMarker(destinations[j], true);
outputDiv.innerHTML += origins[i] + ' to ' + destinations[j]
+ ': ' + results[j].distance.text + ' in '
+ results[j].duration.text + '<br>';
}
}
}
}
function addMarker(location, isDestination) {
var icon;
if (isDestination) {
icon = destinationIcon;
} else {
icon = originIcon;
}
geocoder.geocode({'address': location}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
bounds.extend(results[0].geometry.location);
map.fitBounds(bounds);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
icon: icon
});
markersArray.push(marker);
} else {
alert('Geocode was not successful for the following reason: '
+ status);
}
});
}
function deleteOverlays() {
for (var i = 0; i < markersArray.length; i++) {
markersArray[i].setMap(null);
}
markersArray = [];
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="content-pane">
<div id="inputs">
</div>
<body onload="calculateDistances(); ">
</div>
<div id="map-canvas"></div>
<div id="outputDiv"></div>
</body>
</html>
我已更新代码以显示文件如何加载到特殊文件夹中。
global myarray
Global browserID
Global theUrl
global tTitles
global thehtmlFile
global theData
on opencard
dbArrayToCard myarray
# quit if we are not on a mobile device
if the environment is not "mobile" then exit opencard
#Store Location
put fld"lat"& comma & fld"long" into Store_lng
if fld"lat"or fld"long" is empty then
//if store_lng is empty then
answer error "No Gps Data"
Answer "NOT able to track GPS location"
end if
#current location
//wait 2000 millisecs
put mobileCurrentLocation() into tLocation
put tLocation["latitude"] into fld "Dlat"
put tLocation["longitude"] into fld "Dlong"
put fld"Dlat"& comma & fld"Dlong" into lat_lng
if lat_lng is empty then
answer error "NOT able to track GPS location"
end if
put specialFolderPath("documents") & "/navi2.html" into documentFilePath
if there is not a file documentFilePath then
put specialFolderPath("engine") & "/navi2.html" into engineFilePath
put URL ("binfile:" & engineFilePath) into URL ("binfile:" & documentFilePath)
end if
put specialFolderPath("documents") & "/navi2.html" into thehtmlFile
put URL("file:" & thehtmlFile) into theData
put "var origin1 = new google.maps.LatLng(" & lat_lng & ");" into line 30 of theData
put "var destinationA = new google.maps.LatLng(" & Store_lng & ");" into line 31 of theData
put "zoom:17," into line 37 of theData
put theData into URL ("file:" & thehtmlFile)
put "file://" & specialFolderPath("documents") & "/navi2.html" into theURL
replace space with "%20" in theURL
end OpenCard
您的LiveCode脚本不包含任何尝试从HTML文件获取数据的语法。你的意思是你想要在浏览器控件中加载一个本地文件吗? – Mark 2014-09-02 22:54:28
一边的问题。你为什么不在'fld'和字段名称之间加空格?例如。 'fld“Lat”'而不是'fld“Lat”'? – Mark 2014-09-03 12:20:06
该代码是暂时的格式尚未;) – johnnyB 2014-09-03 13:20:26