使用SWT browser呼叫java script
首先準備一個web.html裡面寫好要執行的js
參考來源是google map api v3 ,標紅色的key要由google api concole產生如下:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?key=key&sensor=false">
</script>
<script type="text/javascript">
var map;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(24.872036929422034, 120.03554529571534),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
}
function createMarker(lat, lng, r){
//var lat = map.getCenter().lat();
//var lng = map.getCenter().lng();
// Marker
var marker_latlng = new google.maps.LatLng(lat, lng);
var marker = new google.maps.Marker({
position: marker_latlng,
map: map,
title: lat+" : "+lng
});
var circle = new google.maps.Circle({
center: marker_latlng,
fillColor: "red",
map: map,
radius: r,
strokeOpacity: 0.0
});
// Click marker to open information box
google.maps.event.addListener(marker, 'click', function() {
InfoBox.open(map, marker);
});
google.maps.event.addListener(map, 'click', function() {
createMarker()
});
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
而Java端的呼叫方法分為兩種
似乎數字型態的參數在Java端要宣告成Number
參考來源是google map api v3 ,標紅色的key要由google api concole產生如下:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?key=key&sensor=false">
</script>
<script type="text/javascript">
var map;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(24.872036929422034, 120.03554529571534),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
}
function createMarker(lat, lng, r){
//var lat = map.getCenter().lat();
//var lng = map.getCenter().lng();
// Marker
var marker_latlng = new google.maps.LatLng(lat, lng);
var marker = new google.maps.Marker({
position: marker_latlng,
map: map,
title: lat+" : "+lng
});
var circle = new google.maps.Circle({
center: marker_latlng,
fillColor: "red",
map: map,
radius: r,
strokeOpacity: 0.0
});
// Click marker to open information box
google.maps.event.addListener(marker, 'click', function() {
InfoBox.open(map, marker);
});
google.maps.event.addListener(map, 'click', function() {
createMarker()
});
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
而Java端的呼叫方法分為兩種
- browser.evaluate("script function name")是有回傳值的呼叫
- browser.execute("script function name")則是無回傳值的呼叫
似乎數字型態的參數在Java端要宣告成Number
留言
張貼留言