在Geolocation中添加click事件的问题

时间:2013-09-12 11:27:40

标签: javascript html5 google-maps user-interface google-maps-markers

我正在研究HTML5和javascript。我正在开发一个应用程序,使用谷歌地图找到用户的位置,我试图在用户的位置添加一个点击事件,这样当他们点击他们点击消息“点击这里”他们必须被定向到一个新的HTML页面。任何帮助或想法将不胜感激。

这是我的代码:

          <!DOCTYPE html>
       <html>
        <head>
     <title>Getting Geolocation for Weather Details</title>
       <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
       <meta charset="UTF-8">
         <style type="text/css">
        html, body {
    height: 100%;
    margin: 0;
    padding: 0;
  }
  #map_canvas {
    height: 100%;
  }

  @media print {
    html, body {
      height: auto;
    }

    #map_canvas {
      height: 650px;
    }
        }
      </style>
       <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=true"></script>
      <script type="text/javascript">
     var map;

      function initialize() {
    var myOptions = {
      zoom: 16,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
      map = new google.maps.Map(document.getElementById('map_canvas'),
        myOptions);

    // Try HTML5 geolocation
    if(navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        var pos = new google.maps.LatLng(position.coords.latitude,
                                         position.coords.longitude);

        var infowindow = new google.maps.InfoWindow({
          map: map,
          position: pos,
          content: 'click here'
        });

        map.setCenter(pos);
      }, function() {
        handleNoGeolocation(true);
      });
    } else {
      // Browser doesn't support Geolocation
      handleNoGeolocation(false);
       }
       }

           function handleNoGeolocation(errorFlag) {
    if (errorFlag) {
      var content = 'Error: The Geolocation service failed.';
    } else {
      var content = 'Error: Your browser doesn\'t support geolocation.';
    }

    var options = {
      map: map,
      position: new google.maps.LatLng(60, 105),
      content: content
    };

    var infowindow = new google.maps.InfoWindow(options);
    map.setCenter(options.position);
      }

      google.maps.event.addDomListener(window, 'load', initialize);
       </script>
    </head>
  <body>
     <div id="map_canvas"></div>
       </body>
  </html>

1 个答案:

答案 0 :(得分:1)

这有帮助吗?

用锚标记替换文字。

使用content : '<a href="https://www.google.com">click here<a>'代替{ - 1}},而不是

content : 'click here'

在您的情况下,您可以将页面链接放在var infowindow = new google.maps.InfoWindow({ map : map, position : pos, content : '<a href="https://www.google.com">click here<a>' });

的位置