Trevor Daniel
Trevor Daniel

Reputation: 3954

Drawing a "Custom" Circle on a Google Map

I am trying to draw a google map with circles on it... no problem. nice easy example here...

https://developers.google.com/maps/documentation/javascript/examples/circle-simple

But, what I want to do is replace the red circle with a custom image, with concentric circles that the user can move around the map. Like this:

Google Map with Concentric Circles

I need four concentric rings at 220nm, 470nm, 720nm and 970nm

I have looked at the properties of the circle and there doesn't appear to be a way of making the circle an image...

I can see how to produce 4 circles with only a border and no inside colour, which might be a way of solving this, but I don't how to make them all move in unison.

Does anyone know if it's possible to make the circle an image or am I trying to do this the wrong way possibly?

Do I need to use a KML Layer possibly?

Any help would be much appreciated!

UPDATE:

I have now decided to try the "4 circle" method. i.e. adding 4 separate circles to the map, making the last one draggable and put a "center_changed" listener on the last one that tries to change the "center" of all the others...

But moving the 4th circle does not move the first circle...

any thoughts?

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Circles</title>
<style>
  html, body, #map-canvas {
    height: 100%;
    margin: 0px;
    padding: 0px
  }
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
    // This example creates circles on the map, representing
    // populations in the United States.

    // First, create an object containing LatLng and population for each city.
    var citymap = {};
    citymap['firstcircle'] = {
        center: new google.maps.LatLng(51.511214, -0.119824),
        radius: 407440
    };
    citymap['secondcircle'] = {
        center: new google.maps.LatLng(51.511214, -0.119824),
        radius: 2000000
    };
    citymap['thirdcircle'] = {
        center: new google.maps.LatLng(51.511214, -0.119824),
        radius: 3000000
    };
    citymap['fourthcircle'] = {
        center: new google.maps.LatLng(51.511214, -0.119824),
        radius: 1796440
    };

    var cityCircle;

    function initialize() {
        // Create the map.
        var mapOptions = {
            zoom: 4,
            center: new google.maps.LatLng(51.511214, -0.119824),
            mapTypeId: google.maps.MapTypeId.TERRAIN
        };

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

        // Create circle 1
        var firstCircleOptions = {
            strokeColor: '#FF0000',
            strokeOpacity: 0.8,
            strokeWeight: 2,
            map: map,
            center: citymap['firstcircle'].center,
            radius: citymap['firstcircle'].radius,
            draggable: true
        };
        // Add the circle for this city to the map.
        firstCircle = new google.maps.Circle(firstCircleOptions)

        // Create circle 4
        var fourthCircleOptions = {
            strokeColor: '#FF0000',
            strokeOpacity: 0.8,
            strokeWeight: 2,
            map: map,
            center: citymap['fourthcircle'].center,
            radius: citymap['fourthcircle'].radius,
            draggable: true
        };
        // Add the circle for this city to the map.
        fourthCircle = new google.maps.Circle(fourthCircleOptions)

        google.maps.event.addListener(fourthCircle, 'center_changed', function () {
            firstCircle.center == fourthCircle.center;
            //alert("circle moved");
        });

        google.maps.event.addListener(fourthCircle, 'dragend', function () {
            alert(fourthCircle.center);
        });

    }

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

</script>
</head>
<body>
<div id="map-canvas"></div>
</body>

Upvotes: 1

Views: 3491

Answers (1)

Trevor Daniel
Trevor Daniel

Reputation: 3954

Credit to Duncan here. as he correctly pointed out I should use

firstCircle.setCenter(fourthCircle.getCenter());

Works Perfectly!

Upvotes: 3

Related Questions