繁体   English   中英

如何在javascript(谷歌地图)中插入链接

[英]How to insert a link inside javascript (google maps)

我想在 JavaScript 中插入一个链接。

我想要这个链接infoWindow.setContent('<a href="add-sesizare.php?lat=">Adaugă sesizare pentru locația ta actuală </a>.'); 将用户发送到add-sesizare.php?lat=&lng=&userid=

<?php include 'header.php'; 
session_start();


 if (isset($_SESSION['user'])) {
    $email = $_SESSION['user'];
    $query = "SELECT * FROM useri WHERE email='$email' LIMIT 1";
    $result = mysqli_query($con,$query);
    $user = mysqli_fetch_assoc($result);
    $userid = $user['id'];
} else { header('Location: login.php'); }
?>
<div class="head-first">
           <div class="d-flex justify-content-between">
                    <a class="navbar-brand" href="#">
                    <i class="fas fa-users"></i> MyCity Curtici
                    </a>
                    <span style="font-size:30px;cursor:pointer;padding: 0 0 0 15px;" onclick="openNav()">&#9776;</span>
            </div> 
</div>
    <div id="map"></div>
    <script>
      // Note: This example requires that you consent to location sharing when
      // prompted by your browser. If you see the error "The Geolocation service
      // failed.", it means you probably did not give permission for the browser to
      // locate you.
      var map, infoWindow;
      var user_id = "<?php echo $userid ?>";
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: 46.35, lng: 21.3},
          zoom: 15
        });
        infoWindow = new google.maps.InfoWindow;

        // Try HTML5 geolocation.
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var pos = {
              lat: position.coords.latitude,
              lng: position.coords.longitude
            };
            infoWindow.setPosition(pos);
            infoWindow.setContent('<a href="add-sesizare.php?lat=">Adaugă sesizare pentru locația ta actuală </a>.');
            infoWindow.open(map);
            map.setCenter(pos);
          }, function() {
            handleLocationError(true, infoWindow, map.getCenter());
          });
        } else {
          // Browser doesn't support Geolocation
          handleLocationError(false, infoWindow, map.getCenter());
        }
        google.maps.event.addListener(map, 'click', function(event) {
        window.location='add-sesizare.php?lat='+event.latLng.lat()+'&long='+event.latLng.lng()+'&userid='+user_id;
        });
      }

      function handleLocationError(browserHasGeolocation, infoWindow, pos) {
        infoWindow.setPosition(pos);
        infoWindow.setContent(browserHasGeolocation ?
                              'Error: The Geolocation service failed.' :
                              'Error: Your browser doesn\'t support geolocation.');
        infoWindow.open(map);
      }
    </script>

    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDd7JYEWDAJVdVkIzZOQumCHYbS2xsIvtM&callback=initMap"
    async defer></script>

<?php include 'footer.php' ?>

您的' vs. "有问题,并且您没有尝试添加地理定位服务返回的 lat/lng 本地值(我认为这是需要的)。

infoWindow.setContent('<a href="add-sesizare.php?lat=' + pos.lat + '&long=' + pos.lng + '&userid=' + user_id +'">Adaugă sesizare pentru locația ta actuală </a>.');

(使用'表示 javascript 字符串, "表示 HTML 中的引用。)

概念证明小提琴

代码片段:

 // Note: This example requires that you consent to location sharing when // prompted by your browser. If you see the error "The Geolocation service // failed.", it means you probably did not give permission for the browser to // locate you. var map, infoWindow; var user_id = "$userid"; function initMap() { map = new google.maps.Map(document.getElementById('map'), { center: { lat: 46.35, lng: 21.3 }, zoom: 15 }); infoWindow = new google.maps.InfoWindow; // Try HTML5 geolocation. if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { var pos = { lat: position.coords.latitude, lng: position.coords.longitude }; infoWindow.setPosition(pos); infoWindow.setContent('<a href="add-sesizare.php?lat=' + pos.lat + '&long=' + pos.lng + '&userid=' + user_id + '">Adaugă sesizare pentru locația ta actuală </a>.'); infoWindow.open(map); map.setCenter(pos); }, function() { handleLocationError(true, infoWindow, map.getCenter()); }); } else { // Browser doesn't support Geolocation handleLocationError(false, infoWindow, map.getCenter()); } google.maps.event.addListener(map, 'click', function(event) { window.location = 'add-sesizare.php?lat=' + event.latLng.lat() + '&long=' + event.latLng.lng() + '&userid=' + user_id; }); } function handleLocationError(browserHasGeolocation, infoWindow, pos) { infoWindow.setPosition(pos); infoWindow.setContent(browserHasGeolocation ? 'Error: The Geolocation service failed.' : 'Error: Your browser doesn\\'t support geolocation.'); infoWindow.open(map); }
 /* Always set the map height explicitly to define the size of the div * element that contains the map. */ #map { height: 100%; } /* Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; }
 <div id="map"></div> <!-- Replace the value of the key parameter with your own API key. --> <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap"></script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM