duanguzhong5776 2014-11-21 01:02
浏览 80
已采纳

Google地图自定义制作工具不会出现在我的服务器上,但适用于localhost

I'm trying to create a custom map where I'm going to set a few markers with a custom icon. The icon appear normally on my localhost but when I uploaad all the files to my server the custom icon doesn't appear! The map show the default GMap marker proving that my database connection is ok. Here follow my code:

index.php:

<!DOCTYPE html>
<html lang="en">
<head>
<title>My map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
  html, body, #map-canvas {
    height: 100%;
    margin: 0px;
    padding: 0px
  }
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div id="map-canvas"></div>
<script type="text/javascript">
        var map;
        var allMarkers = [];
        var customIcons = {
            0: {
                icon: location.origin + '/mymap/images/mm_20_green.png'
            },
            1: {
                icon: location.origin + '/mymap/images/mm_20_red.png'
            }
        };
        function setMarkers(data) {
            console.log('setMarkers');
            console.log(data);
            var markerJson = JSON.parse(data);

            var pos;
            var marker;
            var icon;

            for (var i = 0; i < allMarkers.length; i++) {
                allMarkers[i].setMap(null);
            };

            allMarkers = [];

            console.log(markerJson.length);
            for (var i = 0; i < markerJson.length; i++) {
                icon = customIcons[markerJson[i].stat] || {};                   
                pos = new google.maps.LatLng(markerJson[i].lat, markerJson[i].lng);
                marker = new google.maps.Marker({
                    position: pos,
                    map: map,
                    title: 'Title',
                    icon: icon.icon
                });

                allMarkers.push(marker);
                console.log(allMarkers);
            }
        }

        function fetchMarkers() {
            console.log('fetchMarkers');
            $.ajax({
                type: "GET",
                url: "jsonResponse.php",           
                success: function(data) {
                    setMarkers(data);
                    setTimeout(function(){
                        fetchMarkers();
                    }, 3000);
                }
            });
        }

        function initialize() {
            console.log('initialize');
            // Get center
            var mapOptions = {
                zoom: 14,
                //mapTypeId: google.maps.MapTypeId.HYBRID,
                panControl: false,
                streetViewControl: false,
                mapTypeControl: false
            };
            // Load google map
            map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

            if(navigator.geolocation) {
              browserSupportFlag = true;
              navigator.geolocation.getCurrentPosition(function(position) {
                initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
                map.setCenter(initialLocation);
              }, function() {
                handleNoGeolocation(browserSupportFlag);
              });
            }

            fetchMarkers();
        }

        function loadScript() {                
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&' + 'callback=initialize';
            document.body.appendChild(script);
        }
        window.onload = loadScript;
    </script>
</body>
</html>

jsonResponse.php:

<?php

require("config.php");

// Start XML file, create parent node

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// Opens a connection to a MySQL server

$connection=mysql_connect (DB_SERVER, DB_USER, DB_PASSWORD);
if (!$connection) {  die('Not connected : ' . mysql_error());}

// Set the active MySQL database

$db_selected = mysql_select_db(DB_DATABASE, $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
}

// Select all the rows in the markers table

$query = "SELECT * FROM sensors WHERE 1";
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

$markersArray = array();

while ($row = @mysql_fetch_assoc($result)){
  array_push($markersArray, $row);
}

echo json_encode ($markersArray);

I'm pretty sure the icon image is in the right place. I just copy and paste to the server, so the files structure is exactly the same.

Anyone have any ideia about what should I do? I'm starting to think it is about server configuration...

  • 写回答

1条回答 默认 最新

  • dongzuan4491 2014-11-21 01:45
    关注

    You're probably having a problem with this line:

    icon = customIcons[markerJson[i].stat] || {};

    markerJson[i] is an item that you get back from the server, and stat should be 0 or 1 according to your code.
    If it's not, you will get an empty object, which means that you won't be able to get the icon property of the empty object.

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

报告相同问题?

悬赏问题

  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题