gpt4 book ai didi

javascript - 如何合并这两个 html/javascript 文件

转载 作者:行者123 更新时间:2023-12-03 03:04:29 27 4
gpt4 key购买 nike

我有这个工作正常的 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>

当用户移动标记时,它会更新标记的位置。但是,我还想在同一张 map 上显示一些不可移动的标记,这些标记对应于用户输入的某些元素的位置。我将此 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>
''')

如何合并这两个文件?基本上,我怎样才能在同一张 map 上:

  1. 用户可以在 map 上移动的标记
  2. 用户通过 Python 输入的一堆其他稳定标记(在 PyQt5 中使用 runJavaScript())

我可以从一开始就在html页面中设置稳定标记列表,因为是用户决定它们的位置。

最佳答案

addMarker 不起作用的原因是它无法访问 initialize 中创建的 map 对象。所以你需要使 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 信号,并使用单次计时器来添加一些额外的延迟(以允许加载 google map 控件):

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)

关于javascript - 如何合并这两个 html/javascript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47232179/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com