Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

free form drawing on a google maps

I am using Google Maps API V3 to do some free form drawing of a polygon in Google Maps, as opposed to the standard point-per-click polygon that comes with the standard library. Everything works great.

Problem: Polygons generates lot of editable points.

How can I simplify the polygons and create editable point when its needed?

here my code:

var latlng = new google.maps.LatLng(46.779231, 6.659431);

        var options = {
            center: latlng,
            zoom: 19,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            draggable:false
        };


        var map = new google.maps.Map(document.getElementById("map"), options);

        var markers = [];

        var isDrawing = false;
        var overlay = new google.maps.OverlayView();
        overlay.draw = function () {};
        overlay.setMap(map);
        var polyLine;
        var parcelleHeig = Array();
        google.maps.event.addListener(map, 'mousedown', function () {
                isDrawing=true;
                polyLine = new google.maps.Polyline({
                    map: map
                });
                $("#map").mousemove(function (e) {
                    if(isDrawing == true)
                    {
                        var pageX = e.pageX;
                        var pageY = e.pageY;
                        var point = new google.maps.Point(parseInt(pageX), parseInt(pageY));

                        var latLng = overlay.getProjection().fromDivPixelToLatLng(point);

                        polyLine.getPath().push(latLng);

                        latLng = String(latLng);
                        latLng=latLng.replace("(","");
                        latLng=latLng.replace(")","");

                        var array_lng =  latLng.split(',');
                        parcelleHeig.push(new google.maps.LatLng(array_lng[0],array_lng[1])) ;
                    }
                });
        });
        google.maps.event.addListener(map, 'mouseup', function () {

            isDrawing=false;
            //console.log(parcelleHeig);

            var polygoneParcelleHeig = new google.maps.Polygon({
                paths: parcelleHeig,
                strokeColor: "#0FF000",
                strokeOpacity: 0.8,
                strokeWeight: 2,
                fillColor: "#0FF000",
                fillOpacity: 0.35,
                editable:true,
                geodesic: false

            });

            parcelleHeig=Array();
            polygoneParcelleHeig.setMap(map);
            polyLine.setMap(null);
        });

http://jsfiddle.net/kevdiho/yKHs4/

like image 771
madiknight Avatar asked Oct 18 '13 08:10

madiknight


People also ask

Is there a drawing tool on Google Maps?

Add line or shape.Select a layer and click where to start drawing. A layer can have 2,000 lines, shapes or places. Click each corner or bend of your line or shape. To move the map, click and hold the mouse.

Can you draw free Google Maps?

Scribble Maps is an interesting mash-up that turns Google Maps into an online whiteboard. It adds common drawing and painting tools such as pencils, lines and colored brushes to the Google Maps interfaces so you can easily sketch freehand drawings, insert text, or even add custom shapes anywhere on the map.

Can I draw on Google Maps and save it?

More with Google My Maps Hand-draw points, lines and polygons on your map with the drawing tools. Click the hand icon to stop drawing. You can also save driving, bicycling or walking directions to the map.

How do I draw on Google Maps app?

Select the Draw a Line tool. Select Add Driving Route. Click on your starting point to begin drawing the route, and then double-click on your ending point to stop drawing. My Maps will create your route.


1 Answers

Here is what I am using : http://jsfiddle.net/uF62D/1/

You can change the value of the variable douglasPeuckerThreshold to change the level of simplification on the algorithm.

Note: This is a version I created from the code found at this URL https://gist.github.com/adammiller/826148 (updated to get the same "visual" level of simplification at different zoom levels)

like image 85
user2622729 Avatar answered Oct 11 '22 21:10

user2622729