setCenter on loaded KML

212 views
Skip to first unread message

Niklasro

unread,
Sep 25, 2010, 4:40:17 AM9/25/10
to Google Maps JavaScript API v3
Hello
My setCenter is not working. Can you recommend what I should do? Link
to map's in code here. Thanks

var saopaulo = new google.maps.LatLng(-23.33, -46.38);
var browserSupportFlag = new Boolean();
var map;
var infowindow = new google.maps.InfoWindow();

function initialize() {
var myOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var ctaLayer = new google.maps.KmlLayer('http://www.montao.com.br/
output.kml');
ctaLayer.setMap(map);
map.setCenter(saopaulo)

Rossko

unread,
Sep 25, 2010, 4:49:01 AM9/25/10
to Google Maps JavaScript API v3
> My setCenter is not working. Can you recommend what I should do?

You could just let KmlLayer do its own thing
http://code.google.com/apis/maps/documentation/javascript/overlays.html#KMLLayers
"By default, when displaying a KmlLayer, the map is zoomed and
positioned to show the entirety of the layer's contents"

map.setCenter(saopaulo)
won't work, see the docs
http://code.google.com/apis/maps/documentation/javascript/reference.html#Map
setCenter expects to be given a LatLng object, not a KmlLayer object

Chad Killingsworth

unread,
Sep 25, 2010, 8:28:46 AM9/25/10
to Google Maps JavaScript API v3
As Rossko mentioned, KmlLayers by default will zoom and center the map
to show all of the contained data. If this is not the behavior you
want, then you need to use the perserveViewport option to prevent
this. Then you can use setCenter and setZoom to methods on the map to
show the area you intend.

I'm not sure what Rossko was referring to on your map.setCenter call -
it looks right to me.

Chad Killingsworth

Niklasro

unread,
Sep 25, 2010, 2:36:55 PM9/25/10
to Google Maps JavaScript API v3
It worked perfectly just adding preserveViewport. Thank you Rossko and
Chad. I share data layer between actions in Brazilia and India and
don't want to show data from India on the Brazil view hence trying to
partition to 2 views, India and Brazil.
Niklas

Niklasro

unread,
Sep 25, 2010, 3:27:08 PM9/25/10
to Google Maps JavaScript API v3


On Sep 25, 8:49 am, Rossko <ros...@culzean.clara.co.uk> wrote:
> > My setCenter is not working. Can you recommend what I should do?
>
> You could just let KmlLayer do its own thing
>    http://code.google.com/apis/maps/documentation/javascript/overlays.ht...
> "By default, when displaying a KmlLayer, the map is zoomed and
> positioned to show the entirety of the layer's contents"

That works for the view that displays all articles. In this case only
the articles posted to the same domain should display and they're all
near same geograpical center.

>
>    map.setCenter(saopaulo)
> won't work,  see the docs
>    http://code.google.com/apis/maps/documentation/javascript/reference.h...
> setCenter expects to be given a LatLng object, not a KmlLayer object

I reworked the javascript to support the wanted effect real glad it
was easy.

Thanks a lot!
Reply all
Reply to author
Forward
0 new messages