Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Map V3 not centering, displays only part of map

I'm developing a jQTouch-based app for the iPhone and part of it uses the Google Maps API (V3). I want to be able to pass the geolocation coordinates to the map and have it center the location with a marker. What I'm getting now is the map at the proper zoom level but the desired center-point appears in the upper-righthand corner. It's also showing only about a third of the map area (the rest is gray) and it behaves somewhat erratically when you pan or zoom. Here's the code:


var coords = { latitude : "35.510630", longitude : "-79.255374" };
var latlng = new google.maps.LatLng(coords.latitude, coords.longitude);
var myOptions = {
    zoom: 12,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map($("#map_canvas").get(0), myOptions);
var marker = new google.maps.Marker({
    position: latlng, 
    map: map, 
});     

BTW: It looks and behaves the same on other platforms/browsers as well.

Thoughts?

Thanks in advance,

Mark


Added Here's a link that'll show exactly what's happening: Screen shot of iPhone emulator

like image 416
mpemburn Avatar asked Oct 23 '10 01:10

mpemburn


People also ask

How do I fix map orientation?

To rotate on the Google Maps mobile app, place two fingers down on the map and rotate them clockwise or counterclockwise. Your map will be manipulated according to the direction of your multi-touch input.

Why Google map is not showing my exact location?

Android Devices: On the Android smartphone or tablet, open the Settings app. Tap Location. At the top, switch location on. Tap Mode and then select High accuracy.

How do I fix map problems?

First, make sure that you're using the latest version of the Maps app. If you're not, you can update the app by going to the Google Play Store or the App Store. If that doesn't fix the problem, try restarting your computer. If that doesn't work, try reinstalling the Maps app.


2 Answers

I'm guessing you're using AJAX to display the map, or have the map hidden at some point and then display it based on an action?

In this case Google doesn't know the size of the map container and it will draw only part of the map, usually off-centered.

To fix this, resize the map, then re-center on your lat/lng. Something like this:

google.maps.event.trigger(map, 'resize');

// Recenter the map now that it's been redrawn               
var reCenter = new google.maps.LatLng(yourLat, yourLng);
map.setCenter(reCenter);
like image 185
Brett DeWoody Avatar answered Oct 16 '22 14:10

Brett DeWoody


The solution for getting the map renedered correctly is to execute your Maps-Code on pageAnimationEnd-Event of jQTouch.

Example within Page #div:

    <div id="map_canvas" style="width:100%; height:100%; min-height:400px; background:#ccc;"></div>
    <script type="text/javascript"> 

    $('#map').bind('pageAnimationEnd', function() {

        var mapOptions = {
            zoom: 13,
            disableDefaultUI: true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map =  new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

        if(navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                    var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude)

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

                    map.setCenter(pos);
                }, function() {
                    console.log('Device Environment Capable of Geolocation but not Available - Geolocation failed');
                }
            );
        } else {
            console.log('Device Environment Not Capable of Geolocation - Geolocation failed');
        }
    });
    </script>

Though, i find myself having trouble to make the Map fullscreen-height. Any help in this case is appreciated. Note that im using DataZombies bars extension for fixed footer navigation.

like image 34
2ndOfJuly Avatar answered Oct 16 '22 15:10

2ndOfJuly