由于标记对象,Google地图无法加载

时间:2012-08-08 14:38:52

标签: javascript jquery html5 google-maps geolocation

在没有标记对象的情况下正确加载地图,但是当我在此结构上添加Marker对象时它不会加载我的

<!doctype html>
<html>
    <head>
    <script type="text/javascript"
    src="http://maps.google.com/maps/api/js?sensor=false"></script>

    <script src="jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="script.js" type="text/javascript"></script>
    <script type="text/javascript"> 

        $.geolocation.find(function(location) {
            var lat = location.latitude;
            var lng = location.longitude;

            var map = new google.maps.Map($('#mapDiv').get(0), {
                center: new google.maps.LatLng(lat, lng),
                zoom: 13,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });

            var pinColor = "FE7569";
            var pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2" + pinColor,
            new google.maps.Size(21, 34),
            new google.maps.Point(0,0),
            new google.maps.Point(10, 34));
            var pinShadow = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_shadow",
            new google.maps.Size(40, 37),
            new google.maps.Point(0, 0),
            new google.maps.Point(12, 35))

            var marker= new google.maps.Marker({
                position: new google.maps.LatLng(lat, lng),
                map: map,
                icon: pinImage,
                shadow: pinShadow 
            });        
        });
    </script>
</head>
 <body>
    <div id="mapDiv" style="width:700px; height: 500px;"></div>
 </body>
</html>

脚本包含在页面script.js

      (function($){

            $.extend($.support,{
                geolocation:function(){
                    return $.geolocation.support();
                }
            });

            $.geolocation = {        
                find:function(success, error, options){
                    if($.geolocation.support()){
                        options = $.extend({highAccuracy: true, track: false}, options);
                        ($.geolocation.object())[(options.track ? 'watchPosition' : 'getCurrentPosition')](function(location){
                            success(location.coords);
                        }, function(){
                            error();
                        }, {enableHighAccuracy: options.highAccuracy});     
                    }else{
                        error();                
                    }
                },
                object:function(){
                    return navigator.geolocation;
                },
                support:function(){
                    return ($.geolocation.object()) ? true : false;
                }
            };

        })(jQuery);

我需要在谷歌地图中加入标记 另一个问题是此代码仅在Firefox和IE上运行,但它不能在Google Chrome上运行

1 个答案:

答案 0 :(得分:2)

看看这个,我认为这已经解决,你的错误在url参数中,现在它也在Chrome中运行。您还可以根据需要更换FE7569来更改标记的颜色。

例如: 73B5EB enter image description here 33DE61 enter image description here

您可以做的另一件事是在标记中放置一个文本:

http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=You%20are%20here|FE7569

例如: enter image description here


您的代码的实时演示但已更正:

http://jsfiddle.net/oscarj24/sPsxh/

希望这会有所帮助: - )