Ankur
Ankur

Reputation: 161

How to set the center on a Google Map?

In Google Maps when I set a center through

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>

Upvotes: 0

Views: 1201

Answers (4)

Joseph
Joseph

Reputation: 25523

I just ran some sample code in the Google Playground and it doesn't add a marker when just using 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

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. I would take a look at the vehicle data you're bringing back and see if there's something there.

Upvotes: 0

Chris B
Chris B

Reputation: 15844

It looks like your plotVehicles() function is adding the marker. Check the data which that function is pulling from.

Upvotes: 0

levik
levik

Reputation: 117609

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.

Pasting your entire application into a stackoverflow question is probably not the correct debugging technique :)

Upvotes: 1

SLaks
SLaks

Reputation: 888177

The setCenter method does not add a marker.

You're probably calling some other method as well which is adding a marker; please post all of the code.

Upvotes: 1

Related Questions