【问题标题】:custom google map marker - javascript & php自定义谷歌地图标记 - javascript & php
【发布时间】:2011-03-15 15:09:45
【问题描述】:

这是我的代码 - 我尝试使用 google maps api 和其他教程,但我似乎无法正确获取代码。它要么显示默认图标,要么根本不显示图标。

这是未更改的代码 -

                        <script type="text/javascript">
                    //<![CDATA[

                    <?php if ($er_imglayout == 4 && $row->image2) { ?>
                        function domap()
                    <?php } else { ?>
                        function ezrealtymap()
                    <?php } ?>
                    {
                        // Check to see if this browser can run the Google API
                        if (GBrowserIsCompatible()) {

                            gmarkers = [];
                            htmls = [];
                            to_htmls = [];
                            from_htmls = [];
                            i=0;


                            // Display the map, with some controls and set the initial location 
                            var map = new GMap2(document.getElementById("map"));
                            map.addControl(new GLargeMapControl());
                            map.addControl(new GMapTypeControl());
                            map.setCenter(new GLatLng( <?php echo $row->declat;?>,<?php echo $row->declong;?>), <?php echo $er_mapres;?>);
                            map.setMapType(<?php echo $er_mapview;?>);

                            // Set up three markers with info windows 

                            var point = new GLatLng( <?php echo $row->declat;?>,<?php echo $row->declong;?>);
                            var marker = createMarker(point,"<?php echo stripslashes($row->adline);?>","<?php echo stripslashes($row->adline);?>")
                            map.addOverlay(marker);

                        } else {

                            // display a warning if the browser was not compatible

                            alert("<?php echo _EZREALTY_MAPPING_INCOMPATIBLE;?>");
                        }
                    }

                    // This Javascript is based on code provided by the
                    // Blackpool Community Church Javascript Team
                    // http://www.commchurch.freeserve.co.uk/   
                    // http://www.econym.demon.co.uk/googlemaps/

                    //]]>

                    // A function to create the marker and set up the event window

                    function createMarker(point,name,html) {
                        var marker = new GMarker(point);

                        // The info window version with the "to here" form open

                        to_htmls[i] = html + '<br /><?php echo _EZREALTY_MAP_DIRECTIONS;?>: <strong><?php echo _EZREALTY_MAP_TOHERE;?></strong> - <a href="javascript:fromhere(' + i + ')"><?php echo _EZREALTY_MAP_FROMHERE;?></a>' +
                        '<br /><?php echo _EZREALTY_MAP_START;?>:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
                        '<input type="text" size="40" maxlength="40" name="saddr" id="saddr" value="" /><br />' +
                        '<input value="<?php echo _EZREALTY_MAP_DIRECTIONS;?>" type="submit">' +
                        '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
                        // "(" + name + ")" + 
                        '"/>';

                        // The info window version with the "to here" form open

                        from_htmls[i] = html + '<br /><?php echo _EZREALTY_MAP_DIRECTIONS;?>: <a href="javascript:tohere(' + i + ')"><?php echo _EZREALTY_MAP_TOHERE;?></a> - <strong><?php echo _EZREALTY_MAP_FROMHERE;?></strong>' +
                        '<br /><?php echo _EZREALTY_MAP_END;?>:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
                        '<input type="text" size="40" maxlength="40" name="daddr" id="daddr" value="" /><br />' +
                        '<input value="<?php echo _EZREALTY_MAP_DIRECTIONS;?>" type="submit">' +
                        '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
                        // "(" + name + ")" + 
                        '"/>';

                        // The inactive version of the direction info

                        html = html + '<br /><?php echo _EZREALTY_MAP_DIRECTIONS;?>: <a href="javascript:tohere('+i+')"><?php echo _EZREALTY_MAP_TOHERE;?></a> - <a href="javascript:fromhere('+i+')"><?php echo _EZREALTY_MAP_FROMHERE;?></a>';

                        GEvent.addListener(marker, "click", function() {
                            marker.openInfoWindowHtml(html);
                        });
                        gmarkers[i] = marker;
                        htmls[i] = html;
                        i++;
                        return marker;
                    }

                    // functions that open the directions forms
                    function tohere(i) {
                        gmarkers[i].openInfoWindowHtml(to_htmls[i]);
                    }
                    function fromhere(i) {
                        gmarkers[i].openInfoWindowHtml(from_htmls[i]);
                    }

                    </script>

【问题讨论】:

  • 看起来您丢失了所有代码。
  • 您的帖子中似乎缺少 createMarker 函数的最后一部分。
  • 哎呀,我已经添加了整个脚本 - 谢谢!

标签: javascript api google-maps-markers google-maps


【解决方案1】:

嗯,对不起 - 现在我明白了。我猜你错过了标记生成过程的一部分。这就是我创建自定义标记并将它们添加到地图的方式。

var niceIcon = new GIcon();
niceIcon.image = '/images/Maps/icon/image.png';
niceIcon.shadow = '/images/Maps/icon/shadow.png';
niceIcon.iconSize = new GSize(34, 38);
niceIcon.shadowSize = new GSize(53, 38);
niceIcon.iconAnchor = new GPoint(0, 38);
niceIcon.infoWindowAnchor = new GPoint(17, 0);
niceIcon.printImage = '/images/Maps/icon/printImage.gif';
niceIcon.mozPrintImage = '/images/Maps/icon/mozPrintImage.gif';
niceIcon.printShadow = '/images/Maps/icon/printShadow.gif';
niceIcon.transparent = '/images/Maps/icon/transparent.png';
niceIcon.imageMap = [27, 0, 27, 1, 27, 2, 27, 3, 27, 4, 27, 5, 27, 6, 27, 7, 27, 8, 27, 9, 27, 10, 27, 11, 27, 12, 27, 13, 27, 14, 27, 15, 27, 16, 27, 17, 27, 18, 27, 19, 27, 20, 33, 21, 33, 22, 33, 23, 33, 24, 32, 25, 32, 26, 31, 27, 30, 28, 31, 29, 30, 30, 30, 31, 17, 32, 19, 33, 18, 34, 15, 35, 15, 36, 14, 37, 13, 37, 13, 36, 12, 35, 11, 34, 11, 33, 10, 32, 3, 31, 3, 30, 3, 29, 3, 28, 0, 27, 0, 26, 0, 25, 0, 24, 0, 23, 0, 22, 0, 21, 0, 20, 0, 19, 0, 18, 0, 17, 0, 16, 0, 15, 0, 14, 0, 13, 0, 12, 0, 11, 0, 10, 0, 9, 0, 8, 0, 7, 0, 6, 0, 5, 0, 4, 0, 3, 0, 2, 0, 1, 0, 0];

var markerOptions = { icon: niceIcon };

var point = new GLatLng(lat, lon);
var marker = new GMarker(point, markerOptions);
map.addOverlay(marker);

【讨论】:

  • 我可以控制的那部分 - 但是与我上面的代码集成是我遇到问题的地方
  • 我添加了与上面相同的内容,但我遇到的问题是以下代码: var point = new GLatLng( declat;?>,declong;?>); var marker = createMarker(point,"adline);?>","adline);?>") 如何使用自定义图标代码中的 var 点和 var 标记?
  • 在 createMarker(...) 你有: var marker = new GMarker(point); - 在那里你应该添加第二个参数与 markerOptions 你设置关于图标的所有信息(图标:niceIcon 在我的代码)。
  • 我似乎仍然无法让它工作。我对 api 不够熟悉,无法组合所有这些变量。我在某个地方遇到了冲突,因为地图要么不带标记显示,要么完全消失。任何额外的帮助将不胜感激。谢谢!
猜你喜欢
  • 2013-08-21
  • 1970-01-01
  • 2015-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多