在新的 window 反向地理编码中打开 google 地图
open google map in a new window reverse geocoding
我想打开一个新选项卡,显示具有特定标记样式和功能的 google 地图。我正在使用反向地理编码,因为我有经度和纬度数据。我可以在同一页面中打开地图,但无法在新的 window.
中打开它
提前致谢
这是我的当前代码:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Reverse Geocoding</title>
<style>
#map {
height: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<input id="latlng" type="text" value="40.714224,-73.961452" >
<button id="submit" onclick="geocodeLatLng()">knvhcmc</button>
<div id="map"> </div>
<script>
function geocodeLatLng() {
debugger;
var input = document.getElementById('latlng').value;
var latlngStr = input.split(',', 2);
var latlng = {lat: parseFloat(latlngStr[0]), lng:
parseFloat(latlngStr[1])};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: latlng
});
var geocoder = new google.maps.Geocoder;
var infowindow = new google.maps.InfoWindow;
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
map.setZoom(11);
var marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
var divText = document.getElementById("map").outerHTML;
var myWindow = window.open('', '', 'width=1000,height=800');
var doc = myWindow.document;
doc.open();
doc.write(divText);
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?
key=AIzaSyCcyyWSd6ETju45EKjArNtJXAJt5w1xecQ&callback=initMap">
</script>
</body>
</html>
您可以通过在 javascript 中手动创建元素和脚本来完成此操作,就像他们在 custom google map in new [=23= 中所做的一样]
。
function geocodeLatLng(){
var input = document.getElementById("latlng").value;
var w = window.open('', '_blank'); //you must use predefined window name here for IE.
var head = w.document.getElementsByTagName('head')[0];
w.document.head.innerHTML = '<title>Simple Map</title></head>';
w.document.body.innerHTML = '<body><H1>Loading</H1><div id="map_canvas" style="display: block; width: 500px; height: 300px; margin: 0; padding: 0;"></div></body>';
var loadScript = w.document.createElement('script');
//Link to script that load google maps from hidden elements.
loadScript.type = "text/javascript";
loadScript.async = true;
loadScript.src = "https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize";
var googleMapScript = w.document.createElement('script');
//Link to google maps js, use callback=... URL parameter to setup the calling function after google maps load.
googleMapScript.type = "text/javascript";
googleMapScript.async = false;
googleMapScript.text = 'var map;';
googleMapScript.text += 'function initialize() {';
googleMapScript.text += ' var latlng = new google.maps.LatLng('+input+');';
googleMapScript.text += ' var mapOptions = {';
googleMapScript.text += ' center: latlng,';
googleMapScript.text += ' zoom: 11, ';
googleMapScript.text += ' };';
googleMapScript.text += ' map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);';
googleMapScript.text += ' var marker = new google.maps.Marker({';
googleMapScript.text += ' position: latlng,';
googleMapScript.text += ' map: map';
googleMapScript.text += ' });';
googleMapScript.text += 'var geocoder = new google.maps.Geocoder;';
googleMapScript.text += 'var infowindow = new google.maps.InfoWindow;';
googleMapScript.text += 'geocoder.geocode({"location": latlng}, function(results, status) {';
googleMapScript.text += ' infowindow.setContent(results[0].formatted_address);';
googleMapScript.text += ' infowindow.open(map, marker);';
googleMapScript.text += '});';
googleMapScript.text += '}';
head.appendChild(loadScript);
head.appendChild(googleMapScript);
}
您可以在此处查看工作代码示例:http://jsbin.com/maroviq/edit?html,js,output
我想打开一个新选项卡,显示具有特定标记样式和功能的 google 地图。我正在使用反向地理编码,因为我有经度和纬度数据。我可以在同一页面中打开地图,但无法在新的 window.
中打开它提前致谢
这是我的当前代码:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Reverse Geocoding</title>
<style>
#map {
height: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<input id="latlng" type="text" value="40.714224,-73.961452" >
<button id="submit" onclick="geocodeLatLng()">knvhcmc</button>
<div id="map"> </div>
<script>
function geocodeLatLng() {
debugger;
var input = document.getElementById('latlng').value;
var latlngStr = input.split(',', 2);
var latlng = {lat: parseFloat(latlngStr[0]), lng:
parseFloat(latlngStr[1])};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: latlng
});
var geocoder = new google.maps.Geocoder;
var infowindow = new google.maps.InfoWindow;
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
map.setZoom(11);
var marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
var divText = document.getElementById("map").outerHTML;
var myWindow = window.open('', '', 'width=1000,height=800');
var doc = myWindow.document;
doc.open();
doc.write(divText);
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?
key=AIzaSyCcyyWSd6ETju45EKjArNtJXAJt5w1xecQ&callback=initMap">
</script>
</body>
</html>
您可以通过在 javascript 中手动创建元素和脚本来完成此操作,就像他们在 custom google map in new [=23= 中所做的一样] 。
function geocodeLatLng(){
var input = document.getElementById("latlng").value;
var w = window.open('', '_blank'); //you must use predefined window name here for IE.
var head = w.document.getElementsByTagName('head')[0];
w.document.head.innerHTML = '<title>Simple Map</title></head>';
w.document.body.innerHTML = '<body><H1>Loading</H1><div id="map_canvas" style="display: block; width: 500px; height: 300px; margin: 0; padding: 0;"></div></body>';
var loadScript = w.document.createElement('script');
//Link to script that load google maps from hidden elements.
loadScript.type = "text/javascript";
loadScript.async = true;
loadScript.src = "https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize";
var googleMapScript = w.document.createElement('script');
//Link to google maps js, use callback=... URL parameter to setup the calling function after google maps load.
googleMapScript.type = "text/javascript";
googleMapScript.async = false;
googleMapScript.text = 'var map;';
googleMapScript.text += 'function initialize() {';
googleMapScript.text += ' var latlng = new google.maps.LatLng('+input+');';
googleMapScript.text += ' var mapOptions = {';
googleMapScript.text += ' center: latlng,';
googleMapScript.text += ' zoom: 11, ';
googleMapScript.text += ' };';
googleMapScript.text += ' map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);';
googleMapScript.text += ' var marker = new google.maps.Marker({';
googleMapScript.text += ' position: latlng,';
googleMapScript.text += ' map: map';
googleMapScript.text += ' });';
googleMapScript.text += 'var geocoder = new google.maps.Geocoder;';
googleMapScript.text += 'var infowindow = new google.maps.InfoWindow;';
googleMapScript.text += 'geocoder.geocode({"location": latlng}, function(results, status) {';
googleMapScript.text += ' infowindow.setContent(results[0].formatted_address);';
googleMapScript.text += ' infowindow.open(map, marker);';
googleMapScript.text += '});';
googleMapScript.text += '}';
head.appendChild(loadScript);
head.appendChild(googleMapScript);
}
您可以在此处查看工作代码示例:http://jsbin.com/maroviq/edit?html,js,output