duanniubeng2265 2013-03-03 17:28
浏览 28
已采纳

我正在尝试使用PHP将标记添加到Javascript(谷歌地图),但它没有按照我的方式使用它

I'm trying to use PHP to add a custom marker using GET. I can get inside the if statement just fine and the map loads, but this doesn't add a marker, I'm not familiar with Javascript I'm just using the documentation to do what I need to do.

Inside autolocategmap.js it just contains the Javascript to initialize the map and use geolocation to find the user and it works great, the only problem is the marker is not appearing on load or refresh or anything, I'm not even sure if I can append this extra bit of script by just including <script>custom marker</script>, any information would be great thanks.

     <?php
    /* Include header and config and set variable*/
    require_once('config.inc.php');
    require_once($rootdir . $dirsubfolder . 'navbar.php');
    $route = $_GET['route'];
?>

<?php
/* User wants to retrieve their route */
if ((isset($route)) && (strcmp($route, "sunauto") == 0)) { 
?>
<script src="js/autolocategmap.js"></script>
<script>
    addMarker('56.742111','-111.481753','Stop 3', 'Arrives at: 6:00am');
</script>
<?php
}
?>

autolocategmap.js:

  /**
  * Basic Map
  */

$(document).ready(function(){

 var map = new GMaps({
    div: '#gmap',
   lat: 56.744901,
   lng: -111.473049,
    zoom: 16,
    zoomControl : true,
    zoomControlOpt: {
        style : 'SMALL',
        position: 'TOP_LEFT'
    },
    panControl : false,
  });


    GMaps.geolocate({
  success: function(position) {
    map.setCenter(position.coords.latitude, position.coords.longitude);
  },
  error: function(error) {
    alert('Geolocation failed: '+error.message);
  },
  not_supported: function() {
    alert("Your browser does not support geolocation");
  }
});

$(window).resize(function () {
   var h = $(window).height(),
   offsetTop = 150; // Calculate the top offset

   $('#gmap').css('height', (h - offsetTop));
   }).resize();
});

function addMarker(lat,lng,title,window){
   map.addMarker({
         lat: lat,
         lng: lng,
         title: title,
         infoWindow: window

      });
}
  • 写回答

1条回答 默认 最新

  • dsiuz86842 2013-03-03 18:14
    关注
    function show_map() {
        var myOptions = {
            zoom: 10,
            center: new google.maps.LatLng(51.477118, -0.000732),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map"), myOptions);
        var checkCoords = new Array();
        checkCoords[0] = addMarker(51.477118, -0.000732, 'Royal Observatory, Greenwich, London', map);
        checkCoords[1] = addMarker(38.92126, -77.066442, 'US Naval Observatory, Washington, DC', map);
        checkCoords[2] = addMarker(48.853499, 2.348090, 'Notre Dame Cathedral, Paris', map);
    }
    
    function addMarker(lat, lng, title, map) {
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat, lng),
            title: title
        });
        marker.setMap(map);
        return (lat + ',' + lng);
    }
    
    show_map();
    

    Ok, use that for your js file. It is straight javascript, no jQuery, but it does what you want. You can then play with it to your hearts content.

    As for multiple markers. There are many ways they can be handled through loading from whatever sources.

    Maybe a good bet would be for the php to create a json feed that the javascript could read in and then loop through.

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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路