Google地图:重叠点未显示

时间:2012-04-26 19:40:56

标签: javascript html google-maps google-api

嘿伙计们希望在这里得到一些帮助。

首先我在这里创建了一张工作地图: http://leongaban.com/_stack/googlemaps/firstmap.html 指针显示在现场,但我需要删除Map |卫星|地形按钮。

深入挖掘我在这里找到了一个例子disablingDefaults。但是我无法使用google.map.js文件和API密钥使地图正常工作。所以我只使用了Google's example page中的脚本。

现在我的第二张地图我删除了地图视图选项,但无法显示叠加层:( http://leongaban.com/_stack/googlemaps/

请帮忙!

目标:

  • 使用我的Google地图API密钥
  • 删除地图视图选项按钮
  • 将叠加指针放在该位置。

使用我的Google API 2密钥编写第一张地图代码:

<head>
    <title>Test Google Maps</title>

    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=AIzaSyAXsNu2EwRNqKJn9OmC19WPkEJFM0r6ALk&sensor=true"
        type="text/javascript"></script>

    <script type="text/javascript">

        function initialize() {

            // var myOptions = {
            //     zoom: 16,
            //     center: new google.maps.LatLng(40.750159, -73.976473),
            //     disableDefaultUI: true,
            //     mapTypeId: google.maps.MapTypeId.ROADMAP
            //   }

            // var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

            // var point = new GLatLng(40.750159, -73.976473);
            // map.addOverlay(new GMarker(point));

            var map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(40.750159, -73.976473), 13);
            map.setUIToDefault();

            var myGeographicCoordinates = new GLatLng(40.750159, -73.976473)
            map.addOverlay(new GMarker(myGeographicCoordinates));

            // map.addOverlay(new GMarker(40.750159, -73.976473));


        }

    </script>

</head>

<body onload="initialize()" onunload="GUnload()">
    <div id="map_canvas" style="width: 450px; height: 370px"></div>
</body>

已更新
工作代码!感谢堆叠!!!

<head>
    <title>Test Google Maps</title>

    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>

    <script type="text/javascript">

        function initialize() {

            var myOptions = {
                zoom: 16,
                center: new google.maps.LatLng(40.750159, -73.976473),
                disableDefaultUI: true,
                mapTypeId: google.maps.MapTypeId.ROADMAP
              }

            var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

            var myGeographicCoordinates = new google.maps.LatLng(40.750159, -73.976473);

            var marker = new google.maps.Marker({
                map:        map,
                position:   myGeographicCoordinates,
                title:      "My First Test Marker",
                visible:    true
            }); 
            }

    </script>

</head>

<body onload="initialize()" onunload="GUnload()">
    <div id="map_canvas" style="width: 450px; height: 370px"></div>
</body>

2 个答案:

答案 0 :(得分:1)

定义latLng对象的方法存在问题。

new GLatLng(40.750159, -73.976473);

用于Google Maps Api v2。现在你正在使用最新的api(v3),在新的api中,你应该这样做:

new google.maps.LatLng(40.750159, -73.976473);

编辑:编辑后,我发现标记有一些问题。在API v3中,这是您创建新标记的方式:

var marker = new google.maps.Marker({
    map:        map,
    position:   latLng
});

以下代码设置地图,禁用默认用户界面,设置标记并将其放在地图上;

function initialize() {
 var myOptions = {
  zoom: 16,
  center: new google.maps.LatLng(40.750159, -73.976473),
  disableDefaultUI: true,
  mapTypeId: google.maps.MapTypeId.ROADMAP
 }

 var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
 var myGeographicCoordinates = new GLatLng(40.750159, -73.976473);
 var marker = new google.maps.Marker({
  map:        map,
  position:   myGeographicCoordinates
 });
}

答案 1 :(得分:1)

现在您已转移到v3 API,您想要更改标记创建代码:

map.addOverlay(new GMarker(myGeographicCoordinates));

为:

var marker = new google.maps.Marker({
    map:        map,
    position:   myGeographicCoordinates,
    title:      "My First Test Marker",
    visible:    true
});

如果需要,您还可以在icon中定义传递给标记构造函数的自定义标记MarkerOption,并将值设置为等于图像文件的路径和名称:

var marker = new google.maps.Marker({
    map:        map,
    icon:       "images/my-nice-marker.png",
    position:   myGeographicCoordinates,
    title:      "My First Test Marker",
    visible:    true
});