【问题标题】:Google Map Radius around Marker标记周围的谷歌地图半径
【发布时间】:2014-06-29 01:24:13
【问题描述】:

我有从我的 XML 文件中获取的标记

在 XML 文件中,我有一个距离,它是标记周围的半径,我试图获取半径,但不确定如何告诉它使用距离作为半径

我的地图 HTML 文件如下 map.html

 <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
     <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
     <title>Google Maps AJAX + mySQL/PHP Example</title>
     <script src="http://maps.google.com/maps/api/js?sensor=false"
        type="text/javascript"></script>
     <script type="text/javascript">
     //<![CDATA[

     var customIcons = {
       restaurant: {
    icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
    shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
       },
      bar: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
       shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
     }
     };

     function load() {
       var map = new google.maps.Map(document.getElementById("map"), {
         center: new google.maps.LatLng(-26.2615, 28.2037),
         zoom: 13,
         mapTypeId: google.maps.MapTypeId.ROADMAP
       });
       var infoWindow = new google.maps.InfoWindow();

       // Change this depending on the name of your PHP file
       downloadUrl("map_xml.php", function(data) {
         var xml = data.responseXML;
         var markers = xml.documentElement.getElementsByTagName("marker");
         for (var i = 0; i < markers.length; i++) {
           var lbs_msisdn = markers[i].getAttribute("lbs_msisdn");
           var date_time = markers[i].getAttribute("date_time");
           var distance = markers[i].getAttribute("distance");
           var point = new google.maps.LatLng(
               parseFloat(markers[i].getAttribute("lat")),
               parseFloat(markers[i].getAttribute("lng")));
           var html = "<b>" + lbs_msisdn + "</b> <br/>" + distance ;
           var icon = customIcons['simple'] || {};
         var marker = new google.maps.Marker({
           map: map,
           position: point,
           icon: icon.icon,
           shadow: icon.shadow
         });
var cirlcle = new google.maps.Circle({
       strokeColor: "#00F",
       strokeOpacity: 0.8,
       strokeWeight: 1,
       fillColor: "#00F",
       fillOpacity: 0.10,
    map: map,
       center: point,
    radius: <?php echo $distance?> // But cannot Echo in HTML so not sure how to get radius
           bindInfoWindow(marker, map, infoWindow, html);
         }
       });
      }

     function bindInfoWindow(marker, map, infoWindow, html) {
       google.maps.event.addListener(marker, 'click', function() {
         infoWindow.setContent(html);
         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() {}

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

   <body onload="load()">
     <div id="map" style="width: 500px; height: 300px"></div>
   </body>
   </html>

在其他地图上,我只是回显距离,但从 XML 中提取距离对我来说是新的需要距离作为新半径,任何帮助将不胜感激

【问题讨论】:

  • radius: distance 应该足够了
  • 如果我添加半径:距离它只显示一个标记但仍然没有圆圈
  • bindInfoWindow 的调用显然被放置在错误的位置(在循环创建内)。

标签: xml google-maps


【解决方案1】:

属性通常是字符串,但半径应该是数字。

将其转换为数字:

var cirlcle = new google.maps.Circle({
                strokeColor   : "#00F",
                strokeOpacity : 0.8,
                strokeWeight  : 1,
                fillColor     : "#00F",
                fillOpacity   : 0.10,
                map           : map,
                center        : point,
                radius        : Math.round(distance)
              });

【讨论】:

    猜你喜欢
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    相关资源
    最近更新 更多