How to create Google Latitude like markers?

≯℡__Kan透↙ 提交于 2019-12-04 14:48:52
DMKing

You could do this server-side or since you are using HTML 5 and assuming you have the canvas available you could do this on the client-side. Below is a way to do it client-side using the HTML 5 canvas. Doing it server-side will vary by what language you are using but the technique would be similar.

Download these images and try it out. The images need to reside in the same domain as the page to avoid a security error. They should also be in the same directory unless you update the locations in the HTML.

After the page loads, click the faces and a random marker will be created using the supplied frame image and added to the Google map.

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; utf-8">
        <title>HTML 5 Canvas Image Processing</title>
    </head>
    <body>
        <img onclick="build(this)" id="face1" src="face1.png"/>
        <img onclick="build(this)" id="face2" src="face2.png"/>
        <img onclick="build(this)" id="face3" src="face3.png"/>
        <img id="frame" src="frame.png"/>
        <div id="map_canvas" style="width:640px;height:480px;">
        </div>
        <script type="text/javascript">

            function build(caller){
                var image = getMergedUrl(document.getElementById("frame"), caller);
                var myLatLng = getRndPos(map.getBounds());
                var myMarker = new google.maps.Marker({
                    position: myLatLng,
                    map: map,
                    icon: image
                });

            }

            function getRndPos(bounds) {
                var xDiff = Math.abs(bounds.getNorthEast().lng() - bounds.getSouthWest().lng());
                var yDiff = Math.abs(bounds.getNorthEast().lat() - bounds.getSouthWest().lat());
                var xMin = Math.min(bounds.getNorthEast().lng(),bounds.getSouthWest().lng());
                var yMin = Math.min(bounds.getNorthEast().lat(),bounds.getSouthWest().lat());
                var x = xMin + xDiff * Math.random();
                var y = yMin + yDiff * Math.random();
                var pos = new google.maps.LatLng(y,x);
                return pos;
            }

            function getMergedUrl(frame, pic){
                // Create an empty canvas element
                var canvas = document.createElement("canvas");
                canvas.width = frame.width;
                canvas.height = frame.height;

                // Copy the image contents to the canvas
                var ctx = canvas.getContext("2d");
                ctx.drawImage(frame, 0, 0);
                ctx.drawImage(pic, 4, 4);

                // Get the data-URL formatted image
                var dataURL = canvas.toDataURL("image/png");

                return dataURL;
            }

            function initialize(){
                var myLatlng = new google.maps.LatLng(-34.397, 150.644);
                var myOptions = {
                    zoom: 8,
                    center: myLatlng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                }
                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            }

            function loadScript(){
                var script = document.createElement("script");
                script.type = "text/javascript";
                script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
                document.body.appendChild(script);
            }

            window.onload = loadScript;
        </script>
    </body>
</html>

Thanks to Matthew Crumley for the toDataUrl code from here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!