Brian
Brian

Reputation: 461

How do I draw rectangle covering more than 180 degrees longitude using google maps drawing library?

The issue can be seen on google's example page https://developers.google.com/maps/documentation/javascript/examples/drawing-tools. If you zoom out to a point where you can see the entire world, and try to draw a rectangle it will snap around to cover the opposite direction when more than 180 degrees of longitude are covered.

I have not found any way to change this behavior in the documentation https://developers.google.com/maps/documentation/javascript/drawing. Is there a way to fix this? If not is there another javascript library I could use to accomplish this?

Upvotes: 1

Views: 403

Answers (1)

Brian
Brian

Reputation: 461

I ended up implementing my own draw method / selection tool because the drawing library does not support this. Clicking the map puts it into 'select' mode which causes the rectangle to draw on mousemove. Clicking a second time completes the selection and leaves the rendered rectangle on the map. The 'magic' happens in the calculateBounds function.

self.map = new google.maps.Map(document.getElementById('map'), {
        center: new google.maps.LatLng(0,0),
        zoom: 1,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    self.rectangle = new google.maps.Rectangle ({
        map: null,
        clickable:  false,
        selectable: false,
        dragable: true,
        fillColor:  "#000000",
        fillOpacity:    0.2,
        strokeColor:    "#000000",
        strokeOpacity:  1.0,
        strokeWeight:   1
    });

    google.maps.event.addListener(self.map, 'click', function(e) {
        self.select = !self.select;

        if (self.select) {
            self.startPoint = e.latLng;
        } else {
            self.drawRectangle(e.latLng);

            self.north(self.rectangle.getBounds().getNorthEast().lat().toFixed(15));
            self.south(self.rectangle.getBounds().getSouthWest().lat().toFixed(15));
            self.east(self.rectangle.getBounds().getNorthEast().lng().toFixed(15));
            self.west(self.rectangle.getBounds().getSouthWest().lng().toFixed(15));
        }
    });

    google.maps.event.addListener(self.map, 'mousemove', function(e) {
        if (self.select) {
            self.drawRectangle(e.latLng);
        }
    });

    self.calculateBounds = function(newEndPoint) {
        var north = self.startPoint.lat() >= newEndPoint.lat() ? self.startPoint.lat() : newEndPoint.lat();
        var south = self.startPoint.lat() <= newEndPoint.lat() ? self.startPoint.lat() : newEndPoint.lat();
        var sw = new google.maps.LatLng(south, self.startPoint.lng());
        var ne = new google.maps.LatLng(north, newEndPoint.lng());

        return new google.maps.LatLngBounds(sw, ne);
    };

    self.drawRectangle = function(newEndPoint) {
        self.rectangle.setBounds(self.calculateBounds(newEndPoint));
        self.rectangle.setMap(self.map);
    };

Upvotes: 1

Related Questions