合并谷歌地图(基本Javascript问题)
希望有人能在这里指出一些非常明显的东西(我根本不懂Javascript)。基本上我有两个映射,一个基于通过PHP传递的地址进行地理编码,另一个将位置列表从MySQL数据库拉入XML文件 两张地图在隔离状态下都能完美工作。但是,我需要将它们组合起来,这样一个映射就可以标记XML文件中的位置,并显示地理编码的标记。这两个组合的代码如下所示:合并谷歌地图(基本Javascript问题),javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,希望有人能在这里指出一些非常明显的东西(我根本不懂Javascript)。基本上我有两个映射,一个基于通过PHP传递的地址进行地理编码,另一个将位置列表从MySQL数据库拉入XML文件 两张地图在隔离状态下都能完美工作。但是,我需要将它们组合起来,这样一个映射就可以标记XML文件中的位置,并显示地理编码的标记。这两个组合的代码如下所示: <script> var customLabel = { restaurant: { label: 'R' },
<script>
var customLabel = {
restaurant: {
label: 'R'
},
bar: {
label: 'B'
}
};
var geocoder;
var address ="<?php
foreach ($records as $record)
{
print htmlspecialchars($record['Address']);
}
?>";
/// Start of XML Lookup Map
function initialize() {
var map = new google.maps.Map(document.getElementById('map_canvas'), {
center: new google.maps.LatLng(-33.863276, 151.207977),
zoom: 6
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP or XML file
downloadUrl('test.php', function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function(markerElem) {
var id = markerElem.getAttribute('id');
var name = markerElem.getAttribute('name');
var address = markerElem.getAttribute('address');
var type = markerElem.getAttribute('type');
var point = new google.maps.LatLng(
parseFloat(markerElem.getAttribute('lat')),
parseFloat(markerElem.getAttribute('lng')));
var infowincontent = document.createElement('div');
var strong = document.createElement('strong');
strong.textContent = name
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = address
infowincontent.appendChild(text);
var icon = customLabel[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
label: icon.label
});
var circle = new google.maps.Circle({
map: map,
radius: 200000, // 10 miles in metres
fillColor: '#AA0000'
});
circle.bindTo('center', marker, 'position');
});
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
/// End of XML Lookup Map
/// Start of Geocoding Map
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 7,
center: latlng,
mapTypeControl: true,
mapTypeControlOptions: {style:
google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
if (geocoder) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
map.setCenter(results[0].geometry.location);
var infowindow = new google.maps.InfoWindow(
{ content: '<b>'+address+'</b>',
size: new google.maps.Size(150,50)
});
var markerz = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title:address
});
// Add circle overlay and bind to marker
var circle = new google.maps.Circle({
map: map,
radius: 200000, // 10 miles in metres
fillColor: '#AA0000'
});
circle.bindTo('center', markerz, 'position');
google.maps.event.addListener(marker, markerz, 'click', function() {
infowindow.open(map,marker);
});
} else {
alert("No results found");
}
} else {
alert("Geocode was not successful for the following reason: " +
status);
}
});
}
}
/// End of Geocoding Map
</script>
var customLabel={
餐厅:{
标签:“R”
},
酒吧:{
标签:“B”
}
};
var地理编码器;
var地址=”;
///开始XML查找映射
函数初始化(){
var map=new google.maps.map(document.getElementById('map_canvas'){
中心:新google.maps.LatLng(-33.863276151.207977),
缩放:6
});
var infoWindow=new google.maps.infoWindow;
//根据PHP或XML文件的名称进行更改
下载URL('test.php',函数(数据){
var xml=data.responseXML;
var markers=xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers,function(markereem)){
var id=markereem.getAttribute('id');
var name=markereem.getAttribute('name');
var address=markerem.getAttribute('address');
var type=markereem.getAttribute('type');
var point=new google.maps.LatLng(
parseFloat(markerem.getAttribute('lat')),
parseFloat(markerem.getAttribute('lng'));
var infowincontent=document.createElement('div');
var strong=document.createElement('strong');
strong.textContent=名称
infowincontent.appendChild(强);
infowincontent.appendChild(document.createElement('br'));
var text=document.createElement('text');
text.textContent=地址
infowincontent.appendChild(文本);
var icon=customLabel[type]| |{};
var marker=new google.maps.marker({
地图:地图,
位置:点,,
标签:icon.label
});
var circle=new google.maps.circle({
地图:地图,
半径:200000,//10英里(米)
填充颜色:“#AA0000”
});
圆圈.bindTo('中心',标记,'位置');
});
});
}
函数下载url(url,回调){
var请求=window.ActiveXObject?
新的ActiveXObject('Microsoft.XMLHTTP'):
新的XMLHttpRequest;
request.onreadystatechange=函数(){
if(request.readyState==4){
request.onreadystatechange=doNothing;
回调(请求、请求、状态);
}
};
打开('GET',url,true);
请求发送(空);
}
函数doNothing(){}
///XML查找映射结束
///地理编码地图的开始
函数初始化(){
geocoder=新的google.maps.geocoder();
var latlng=新的google.maps.latlng(-34.397150.644);
变量myOptions={
缩放:7,
中心:拉特林,
mapTypeControl:true,
mapTypeControlOptions:{样式:
google.maps.MapTypeControlStyle.DROPDOWN_MENU},
导航控制:对,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=new google.maps.map(document.getElementById(“map_canvas”),myOptions);
if(地理编码器){
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
if(status!=google.maps.GeocoderStatus.ZERO\u结果){
map.setCenter(结果[0].geometry.location);
var infowindow=new google.maps.infowindow(
{内容:''+地址+'',
尺寸:新谷歌地图尺寸(150,50)
});
var markerz=新的google.maps.Marker({
位置:结果[0]。geometry.location,
地图:地图,
标题:地址
});
//添加圆形覆盖并绑定到标记
var circle=new google.maps.circle({
地图:地图,
半径:200000,//10英里(米)
填充颜色:“#AA0000”
});
圆圈.bindTo('center',markerz',position');
google.maps.event.addListener(marker,markerz,'click',function(){
信息窗口。打开(地图、标记);
});
}否则{
警报(“未发现结果”);
}
}否则{
警报(“地理代码未成功,原因如下:”+
地位);
}
});
}
}
///地理编码地图结束
感谢您的指导 谢谢@John Smith,解决了这个问题。在下面发布工作代码。这将允许查找和固定地理编码地址,以及通过PHP从XML文件中拉取和固定lat/lng坐标到一个地图上
<script>
var customLabel = {
restaurant: {
label: 'R'
},
bar: {
label: 'B'
},
stockingcenter: {
label: 'S'
}
};
var geocoder;
var address ="<?php
foreach ($records as $record)
{
print htmlspecialchars($record['Address__c']);
}
?>";
/// Start of XML Lookup Map
function initialize() {
var map = new google.maps.Map(document.getElementById('map_canvas'), {
center: new google.maps.LatLng(51.5477187, -0.3150843),
zoom: 6
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP or XML file
downloadUrl('test.php', function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function(markerElem) {
var id = markerElem.getAttribute('id');
var name = markerElem.getAttribute('name');
var address = markerElem.getAttribute('address');
var type = markerElem.getAttribute('type');
var point = new google.maps.LatLng(
parseFloat(markerElem.getAttribute('lat')),
parseFloat(markerElem.getAttribute('lng')));
var infowincontent = document.createElement('div');
var strong = document.createElement('strong');
strong.textContent = name
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = address
infowincontent.appendChild(text);
var icon = customLabel[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
label: icon.label
});
marker.addListener('click', function() {
infoWindow.setContent(infowincontent);
infoWindow.open(map, marker);
});
});
});
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
/// End of XML Lookup Map
/// Start of Geocoding Map
geocoder = new google.maps.Geocoder();
var myOptions = {
zoom: 6};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
if (geocoder) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
map.setCenter(results[0].geometry.location);
var infowindow = new google.maps.InfoWindow(
{ content: '<b>'+address+'</b>',
size: new google.maps.Size(150,50)
});
var markerz = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title:address
});
// Add circle overlay and bind to marker
var circle = new google.maps.Circle({
map: map,
radius: 200000, // 10 miles in metres
fillColor: '#4e63a1'
});
circle.bindTo('center', markerz, 'position');
google.maps.event.addListener(markerz, 'click', function() {
infowindow.open(map,markerz);
});
} else {
alert("No results found");
}
} else {
alert("Geocode was not successful for the following reason: " +
status);
}
});
}
}
/// End of Geocoding Map
</script>
var customLabel={
餐厅:{
标签:“R”
},
酒吧:{
标签:“B”
},
采购中心:{
标签:“S”
}
};
var地理编码器;
var地址=”;
///开始XML查找映射
函数初始化(){
var map=new google.maps.map(document.getElementById('map_canvas'){
中心:新google.maps.LatLng(51.5477187,-0.3150843),
缩放:6
});
var infoWindow=new google.maps.infoWindow;
//根据PHP或XML文件的名称进行更改
下载URL('test.php',函数(数据){
var xml=data.responseXML;
var markers=xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers,function(markereem)){
var id=markereem.getAttribute('id');
var name=markereem.getAttribute('name');
var address=markerem.getAttribute('address');
var type=markereem.getAttribute('type');
var point=new google.maps.LatLng(
parseFloat(markerem.getAttribute('lat')),
parseFloat(markerem.getAttribute('lng'));
var infowincontent=document.createElement('div');
var stro