我有这个html / javascript文件可以正常工作:
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
function geocodePosition(pos) {
geocoder.geocode({
latLng: pos
}, function(responses) {
if (responses && responses.length > 0) {
updateMarkerAddress(responses[0].formatted_address);
} else {
updateMarkerAddress('Cannot determine address at this location.');
}
});
}
function updateMarkerStatus(str) {
document.getElementById('markerStatus').innerHTML = str;
}
function updateMarkerPosition(latLng) {
document.getElementById('info').innerHTML = [
latLng.lat(),
latLng.lng()
].join(', ');
}
function updateMarkerAddress(str) {
document.getElementById('address').innerHTML = str;
}
function initialize() {
var latLng = new google.maps.LatLng(40.767367, -111.848007);
var map = new google.maps.Map(document.getElementById('mapCanvas'), {
zoom: 8,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: latLng,
title: 'Point A',
map: map,
draggable: true
});
// Update current position info.
updateMarkerPosition(latLng);
geocodePosition(latLng);
// Add dragging event listeners.
google.maps.event.addListener(marker, 'dragstart', function() {
updateMarkerAddress('Dragging...');
});
google.maps.event.addListener(marker, 'drag', function() {
updateMarkerStatus('Dragging...');
updateMarkerPosition(marker.getPosition());
});
google.maps.event.addListener(marker, 'dragend', function() {
updateMarkerStatus('Drag ended');
geocodePosition(marker.getPosition());
});
}
// Onload handler to fire off the app.
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<style>
#mapCanvas {
# width: 1000px;
width: 102%;
height: 500px;
float: left;
margin-left: -7px;
margin-right: -10px;
margin-top: -7px;
margin-bottom: 10px;
}
#infoPanel {
float: center;
margin-left: 20px;
}
#infoPanel div {
margin-bottom: 10px;
}
</style>
<font size="3" color="black" face="verdana">
<div id="mapCanvas"></div>
<div id="infoPanel">
<font size="-10" color="white" face="verdana">
<!-- <b>Marker status:</b> -->
<div id="markerStatus"><i>Click and drag the marker.</i></div>
<font size="3" color="black" face="verdana">
<b>Current position:</b>
<div id="info"></div>
<b>Closest matching address:</b>
<div id="address"></div>
</div>
</body>
</html>
当用户移动标记时,它会更新标记的位置。但是,我还希望在同一个地图上显示一些标记,而不是可移动的,对应于用户输入的某些元素的位置。我将这个html / javacsript代码包含在一个更大的Python函数中。我有这个其他的javascript代码,但它不包含可移动标记的部分。在这种情况下,使用PyQt5,用户可以将从Python传递的标记(带有runJavaScript()
)新标记的坐标添加到javascript代码中的函数addMarker()
中:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=yes" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0px; padding: 0px }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
var map;
function initialize() {
var latlng = new google.maps.LatLng(40.767367, -111.848007);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
}
var goldStar = {
path: 'M 125,5 155,90 245,90 175,145 200,230 125,180 50,230 75,145 5,90 95,90 z',
fillColor: 'yellow',
fillOpacity: 0.8,
scale: 0.1,
strokeColor: 'gold',
strokeWeight: 1
};
function addMarker(lat, lon, city, url) {
var newmarker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lon),
//icon: {path: google.maps.SymbolPath.BACKWARD_CLOSED_ARROW,
// scale: 5},
icon: goldStar,
map: map,
title: city
});
newmarker['infowindow'] = new google.maps.InfoWindow({
content: url
});
google.maps.event.addListener(newmarker, 'click', function() {
this['infowindow'].open(map, this);
});
}
</script>
</head>
<body onload="initialize();">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
''')
如何合并这两个文件?基本上,我怎么能在同一张地图上:
runJavaScript()
)我可以从一开始就在html页面中设置稳定标记列表,因为用户可以决定它们的位置。
答案 0 :(得分:1)
addMarker
无效的原因是它无法访问map
中创建的initialize
对象。所以你需要map
全局:
var geocoder = new google.maps.Geocoder();
var map;
...
function addMarker(lat, lon, city, url) {
var newmarker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lon),
map: map,
title: city
});
newmarker['infowindow'] = new google.maps.InfoWindow({
content: url
});
google.maps.event.addListener(newmarker, 'click', function() {
this['infowindow'].open(map, this);
});
}
function initialize() {
var latLng = new google.maps.LatLng(40.767367, -111.848007);
// create as a global variable
map = new google.maps.Map(document.getElementById('mapCanvas'), {
zoom: 8,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
...
}
在尝试从PyQt运行任何javascript之前,您还应该等到页面完全加载。因此,将一个插槽连接到loadFinished
信号,并使用单次定时器添加一些额外的延迟(以允许加载谷歌地图控件):
class ShowMap_fun(QMainWindow):
def __init__(self):
super().__init__()
self.map_ui = Ui_tmy3page()
self.map_ui.setupUi(self)
# wait for page to load
self.map_ui.html_code.loadFinished.connect(self.onLoad)
self.map_ui.html_code.load(QtCore.QUrl.fromLocalFile('/tmp/test.html'))
self.map_ui.show_stat.clicked.connect(self.save_fun)
self.map_ui.html_code.page().profile().downloadRequested.connect(_downloadRequested)
def onLoad(self):
# extra one second delay
QtCore.QTimer.singleShot(1000, self.save_fun)