0

Error in js function using google map

asked 2015-09-24 15:15:11 +0800

greendragon gravatar image greendragon
25 4

Hi! I want to insert google map in a zul file.

<div class="map-canvas" id="map-canvas">
    <div class="map" id="map"></div>
</div>

In my .css file I have:

.map-canvas {
    background-color: aqua;
    min-height: calc(100vh - 50px);
    padding: 0px;
}

#map {
    position: absolute;
    top: 0px;
    bottom: 0px;
    width: 100%;
}

and the js file contains:

function initialize() {
    var mapOptions = {
            center: new google.maps.LatLng(-33.8688, 151.2195),
            zoom: 13,
            disableDefaultUI: true
    };
    var map = new google.maps.Map(document.getElementById('map'),mapOptions);
    var autocomplete = new google.maps.places.Autocomplete(input);
    autocomplete.bindTo('bounds', map);
    var infowindow = new google.maps.InfoWindow();
    var marker = new google.maps.Marker({
        map: map,
        anchorPoint: new google.maps.Point(0, -29)
    });
    google.maps.event.addListener(autocomplete, 'place_changed', function() {
        infowindow.close();
        marker.setVisible(false);
        var place = autocomplete.getPlace();
        if (!place.geometry) {
            window.alert("Autocomplete's returned place contains no geometry");
            return;
        }

        if (place.geometry.viewport) {
            map.fitBounds(place.geometry.viewport);
        } else {
            map.setCenter(place.geometry.location);
            map.setZoom(17);
        }
        marker.setIcon(({
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(35, 35)
        }));
        marker.setPosition(place.geometry.location);
        marker.setVisible(true);

    });
    google.maps.event.addDomListener(window, 'load', initialize);
}

It doesn't work, the error message is Uncaught TypeError: Cannot read property 'offsetWidth' of null Any ideas?

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2015-10-16 10:52:54 +0800

greendragon gravatar image greendragon
25 4

Hi! I'm losing a lot of time on this problem, and I think the problem is in the following line code:

var map = new google.maps.Map(document.getElementById('map'),mapOptions);

I think the document.getElementById('map') doesn't get correctly the element. Is it possible?

Is there another way to get the element?

Thanks

link publish delete flag offensive edit
0

answered 2015-10-19 08:03:28 +0800

Darksu gravatar image Darksu
1991 1 4

Hello greendragon,

Why don't you use ZK's gmap component as shown below:

http://www.zkoss.org/zkdemo/reporting/google_map

Best Regards,

Darksu

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2015-09-24 15:15:11 +0800

Seen: 36 times

Last updated: Oct 19 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More