简体   繁体   English

如何在Google地图上设置中心?

[英]How to set the center on a Google Map?

In Google Maps when I set a center through 在Google地图中,当我通过

map.setCenter(new GLatLng(28.6589, 77.2104), 13);

It not only sets the centre of the map, but adds a marker on that place which I don't want it to be. 它不仅设置了地图的中心,而且在我不希望出现的地方添加了一个标记。

Update 更新

I feel no harm to share my code, at least it may help others also. 共享我的代码对我没有害处,至少它对其他人也有帮助。 Here's the entire script I wrote 这是我写的整个脚本

<script type="text/javascript">
    var map;
    function initialize() {
      if (GBrowserIsCompatible()) {


      map= new GMap2(document.getElementById("map_canvas"));


        map.setCenter(new GLatLng(28.6589, 77.2104), 13);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
       map.clearOverlays();
        //alert(PlottingMarkersDetails);
      }
      setInterval("plotVehicles()",1000);
    }
    function plotVehicles()
    {

     try
        {
            map.clearOverlays();
            var bounds = new GLatLngBounds();
            if(document.getElementById("<%=hf_SelectedVehiclePlottingDetails.ClientID %>"))
            {
            var PlottingMarkersDetails=document.getElementById("<%=hf_SelectedVehiclePlottingDetails.ClientID %>").value;

            var arrVehicleDetails=PlottingMarkersDetails.split("@");
            alert(arrVehicleDetails.length);
            for(var i=0; i<arrVehicleDetails.length;i++)
            {
                var currentVehicleInfo=arrVehicleDetails[i].split(",");

                var point = new GLatLng(parseFloat(currentVehicleInfo[1]),parseFloat(currentVehicleInfo[2]));
                var marker = createMarker(point,currentVehicleInfo[0] + "<br/>" + currentVehicleInfo[4] + "<br/>" + currentVehicleInfo[3] );
                //bounds.extend(point);
                map.addOverlay(marker);

            }
            //map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));

            }
            }

        catch(err)
        {
        }

    }
    function createMarker(point,html) {
    var blueIcon = new GIcon(G_DEFAULT_ICON);
    blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";    
    markerOptions = { icon:blueIcon };            
        var marker = new GMarker(point,markerOptions);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        return marker;
      }
    </script>

The setCenter method does not add a marker. setCenter方法不添加标记。

You're probably calling some other method as well which is adding a marker; 您可能还会调用添加标记的其他方法。 please post all of the code. 请发布所有代码。

I suggest you debug your code. 我建议您调试代码。 I only see one place where you're calling map.addOverlay() - add a breakpoint here and see if you ever call it, and if so, look at the call stack. 我只看到一个地方在调用map.addOverlay() -在此处添加一个断点,看看是否曾经调用过它,如果是,请查看调用堆栈。

Pasting your entire application into a stackoverflow question is probably not the correct debugging technique :) 将整个应用程序粘贴到stackoverflow问题中可能不是正确的调试技术:)

I just ran some sample code in the Google Playground and it doesn't add a marker when just using map.setCenter() 我只是在Google Playground中运行了一些示例代码,仅使用map.setCenter()时并没有添加标记

I ran the following: 我执行以下操作:

function initialize() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
  }
}

UPDATE UPDATE

From looking at the new code you just put up, the marker that you're concerned about must be getting added in your plotVehicles functionality. 通过查看您刚刚编写的新代码,您必须将关注的标记添加到plotVehicles功能中。 I would take a look at the vehicle data you're bringing back and see if there's something there. 我将查看您要带回的车辆数据,看看那里是否有东西。

It looks like your plotVehicles() function is adding the marker. 看起来您的plotVehicles()函数正在添加标记。 Check the data which that function is pulling from. 检查要从中提取功能的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM