dsdzvprlp51692469 2017-05-03 07:15
浏览 66
已采纳

我写了一个代码使用谷歌地图示例使用php和mysqli在服务附近找到,代码工作正常,但它不能在chrome中工作

I wrote a code using the example of the google maps with php and mysqli, the code is working in other browsers but in the chrome whe I load this program the map displays and turns into blue or grey without markers. I am getting the grey screen or some times blue screen when I load this page in chrome browser.but the results will show inside div. After clearing the location settings,the markers appears. I have posted my code Please check it if I have missed anything.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>Services Near To You</title>
<script src="https://maps.googleapis.com/maps/api/js?key="
        type="text/javascript"></script>

<script 
  src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js">
</script> 
<script type="text/javascript">
//<![CDATA[
var map;
var markers = [];
var infoWindow;
var locationSelect;



$(document).ready(function(){

if (navigator.geolocation) {

    navigator.geolocation.getCurrentPosition(showLocation);

} else {

    $('#location').html('Geolocation is not supported by this browser.');

}

});

/*$(document).ready(function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(newLocation);
} else {
alert("Geolocation is not supported by this browser.");
}
}

function newLocation(position) {
alert(position.longitude);
}*/

function showLocation(position) {

var  lati = position.coords.latitude;

var  lngi = position.coords.longitude;

load(lati,lngi);

 }

function load(lati,lngi) {


  map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(lati,lngi),
    zoom: 10,
    mapTypeId: 'roadmap',
    mapTypeControlOptions: {style: 
   google.maps.MapTypeControlStyle.DROPDOWN_MENU}
  });
  infoWindow = new google.maps.InfoWindow();

  locationSelect = document.getElementById("locationSelect");
  searchLocationsNear(lati,lngi);
  locationSelect.onchange = function() {
    var markerNum = 
  locationSelect.options[locationSelect.selectedIndex].value;
    if (markerNum != "none"){
      google.maps.event.trigger(markers[markerNum], 'click');
    }
  };
  }

 /*function searchLocations() {
 var address = document.getElementById("addressInput").value;
 var geocoder = new google.maps.Geocoder();
 geocoder.geocode({address: address}, function(results, status) {
   if (status == google.maps.GeocoderStatus.OK) {
    searchLocationsNear(results[0].geometry.location);
   } else {
     alert(address + ' not found');
   }
 });
 }*/

 function clearLocations() {
 infoWindow.close();
 for (var i = 0; i < markers.length; i++) {
   markers[i].setMap(null);
 }
 markers.length = 0;

 locationSelect.innerHTML = "";
 var option = document.createElement("option");
 option.value = "none";
 option.innerHTML = "See all results:";
 locationSelect.appendChild(option);
 }

 function searchLocationsNear(lati,lngi) {
 clearLocations();

 var radius = 2;
 //var radius = document.getElementById('radi').value;    

 var searchUrl = 'sn.php?lat=' + lati + '&lng=' + lngi + '&radius=' +radius;
 downloadUrl(searchUrl, function(data) {
   var xml = parseXml(data);
   var markerNodes = xml.documentElement.getElementsByTagName("marker");
   var bounds = new google.maps.LatLngBounds();
   for (var i = 0; i < markerNodes.length; i++) {
     var name = markerNodes[i].getAttribute("name");
     var address = markerNodes[i].getAttribute("address");
     var icn = markerNodes[i].getAttribute("icon");
  var offers = markerNodes[i].getAttribute("offers");
      var link = markerNodes[i].getAttribute("link");
      var cat = markerNodes[i].getAttribute("cat");

     var distance = parseFloat(markerNodes[i].getAttribute("distance"));
     var latlng = new google.maps.LatLng(
          parseFloat(markerNodes[i].getAttribute("lat")),
          parseFloat(markerNodes[i].getAttribute("lng")));

     createOption(name, distance, i);
     createMarker(latlng, name, address,icn,offers,link,cat);
     bounds.extend(latlng);
   }
   map.fitBounds(bounds);
   locationSelect.style.visibility = "visible";
   locationSelect.onchange = function() {
     var markerNum = 
   locationSelect.options[locationSelect.selectedIndex].value;
     google.maps.event.trigger(markers[markerNum], 'click');
   };
  });
  }

 function createMarker(latlng, name, address,icn,offers,link,cat) {
  var html = "<b><u>Service Category</u> :" + cat + "</b>" + "</br>" + "<b>" 
 + name + "</b><br/>" + address + "</br><b>OFFERS: " +offers +"</b> </br>"+"
 <a href= " + link +" ><center>View Member</center></a>" ;
  var iconBase = 'https://freerewards2u.com/cicons/'+ icn; 

  var marker = new google.maps.Marker({
    map: map,
    position: latlng,
    icon:iconBase
  });
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
  markers.push(marker);
}

function createOption(name, distance, num) {
  var option = document.createElement("option");
  option.value = num;
  option.innerHTML = name + "(" + distance.toFixed(1) + ")";
  locationSelect.appendChild(option);
}

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.responseText, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
}

function parseXml(str) {
  if (window.ActiveXObject) {
    var doc = new ActiveXObject('Microsoft.XMLDOM');
    doc.loadXML(str);
    return doc;
  } else if (window.DOMParser) {
    return (new DOMParser).parseFromString(str, 'text/xml');
  }
}




function doNothing() {}

//]]>
</script>
</head>

 <style>
div.polaroid {
width: 300px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0,0.19);
text-align: center;
}
</style>
<body style="margin:0px; padding:0px;" onload="load()">

<div><select id="locationSelect" style="width:100%;visibility:hidden">
</select></div>
<center><div class="polaroid" id="map" style="width: 75%; height: 75%">
</div></center>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • duan198811 2017-05-03 07:37
    关注

    In createMarker() you concatenate the line by using a new line. JavaScript however doesn't support this. Try:

    var html =    "<b><u>Service Category</u> :" + cat + "</b></br><b>" + 
                  name + "</b><br/>" + address + "</br><b>OFFERS: " + offers +
                  "</b> </br><a href= " + link +" ><center>View Member</center></a>";
    

    You can also break up a code line within a text string with a single backslash:

    var demo = "Hello \
    Dolly!";
    

    Best of luck!

    EDIT: You might also want to provide the onload() function in your <body> with some parameters. This will work fine for now, but will make you by default end up in the middle of the ocean at coordinates 0, 0.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料