Custom CSS for the Google Maps Circle Object

Currently, I am utilizing the Google Maps Javascript API v3 Circle object to display circles on the map. I am interested in customizing the CSS of this circle by incorporating some CSS animations.

Although I am aware that custom overlays can be used for this purpose, I prefer using the Circle class due to its simplicity in setting a radius and automatic scaling of the circle.

However, my challenge lies in trying to edit specific CSS properties of the circle as there doesn't seem to be any documentation available regarding this issue.

I am seeking guidance on how to achieve this customization.

This is the current code snippet I am using for creating the Circle:

var latLng = {
   lat: 0,
   lng: 0
};      
var radius = 200; //200 meters
var fillColor = "#40ad00";
var strokeColor = "#40ad00";

var circle = new google.maps.Circle({
   center: latLng,
   radius: radius,
   strokeColor: strokeColor,
   strokeOpacity: 0.25,
   strokeWeight: 1,
   fillColor: fillColor,
   fillOpacity: 0.1,
   map: map
});

My goal is to have an animated circle displayed on the map.

body {
    background: #000000;
}

div.circle {
    position: absolute;
    top: 10px;
    left: 50px;
    width: 200px;
    height: 200px;
    overflow: hidden;
    border: 2px solid;
    border-radius: 100%;
    box-sizing: border-box;
    animation: spin 5s infinite cubic-bezier(0.075, 0.80, 0.135, 1);
}

@keyframes spin {
    0% {
        opacity: 0;
        -webkit-transform: scale(0.25);
        border-color: #FFFFFF;
        box-shadow: 0px 0px 0px 1px #FFFFFF;
    }
    15% {
        opacity: 1;
    }
    70% {
        opacity: 1;
        border-color: #d100ff;
        box-shadow: 0px 0px 50px 1px #d100ff;
    }
    100% {
        opacity: 0;
        -webkit-transform: scale(1);
        box-shadow: 0px 0px 50px 1px #FFFFFF;
    }
}
<div class="circle"></div>

Answer â„–1

Unfortunately, it's not possible to customize the appearance of the Circle object in the Google Maps API.

A workaround is to create a custom OverlayView that replicates the functionality of the Circle object. The trick is to utilize the Circle API to calculate the bounds and then apply those bounds to the custom OverlayView.

Below is the complete code snippet with a custom style applied using the circle class:

CustomCircle = function(center, radius, map) {
  // Calculate bounds using Circle API
  this.bounds_ = new google.maps.Circle({
    center: center,
    radius: radius
  }).getBounds();
  this.map_ = map;
  this.div_ = null;
  this.setMap(map);
};
CustomCircle.prototype = new google.maps.OverlayView();
CustomCircle.prototype.onAdd = function() {
  var div = document.createElement('div');
  div.style.position = 'absolute';

  var circle = document.createElement('div');
  circle.className = 'circle'; // Class for custom styling
  circle.style.width = '100%';
  circle.style.height = '100%';
  circle.style.position = 'absolute';
  div.appendChild(circle);

  this.div_ = div;
  var panes = this.getPanes();
  panes.overlayLayer.appendChild(div);
};
CustomCircle.prototype.draw = function() {
  var overlayProjection = this.getProjection();
  var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
  var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());
  var div = this.div_;
  div.style.left = sw.x + 'px';
  div.style.top = ne.y + 'px';
  div.style.width = (ne.x - sw.x) + 'px';
  div.style.height = (sw.y - ne.y) + 'px';
};
CustomCircle.prototype.onRemove = function() {
  this.div_.parentNode.removeChild(this.div_);
  this.div_ = null;
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The script for choosing pages is malfunctioning

I'm having trouble with a script on my website. It works on the index page, but not on other pages. <div id="pages"></div>   <script>      a = location.href;      b = a.split('-');      c = b.length;    ...

Animate the appearance of list items sequentially using CSS animations

Is there a simpler way to achieve the effect of having list items slide in one after the other? The CSS method provided here seems quite intricate. #nav ul.is-visible{visibility:visible;opacity:1;-webkit-transform:scale(1);-ms-transform:scale(1);transfo ...

Setting up an SSL certificate for an Express application: A step-by-step guide

I am currently trying to set up my Express server in order to pass the SSL certificate and transition from http to https. After going through the Express documentation, I still haven't been able to find a suitable solution. While some suggestions lik ...

Is there a way to update a PHP include file without needing to refresh the page?

I've been learning as I go, so some of my questions may seem basic or beyond my current knowledge level. However, I find that peer explanations, examples, and advice are the most effective way for me to learn. Thank you in advance for your help. Usin ...

Is my data secure in Node.js RAM?

I have developed a web application that enables users to create personal pages using Express and Node.JS. Each page is represented as an object, allowing for the creation of new ones with the syntax: new privatePage(name, pswd). As a result, all passwords ...

Searching for a JavaScript tool that offers syntax highlighting capabilities

I have come across some excellent RTE HTML text editors such as jsredaktor, TinyMCE, and FCK Editor, but I am in search of an HTML/JavaScript component that functions similarly to a TEXTAREA with built-in code syntax highlighting. ...

Learn the process of updating a nested document within an array in MongoDB

I have a data structure as shown below: { "name":"xxxxxx", "list":[ { "listname":"XXXXX1", "card":[ { "title":"xxxxxx", "descip":"xxxxxxx ...

Ways to identify the visible elements on a webpage using JavaScript

I'm working on a nextjs app , I am looking to dynamically update the active button in the navbar based on which section is currently visible on the page. The child elements of the page are structured like this: <div id="section1" > < ...

Can you explain the functioning of knockout container less syntax? (does it have any drawbacks?)

There are numerous instances and examples of using knockout ContainerLess syntax, although I find it challenging to locate proper documentation from their site. Initially, my question was "is it evil?" but upon realizing my lack of understanding on how it ...

Mastering the art of curating the perfect image for your Facebook timeline

Controlling the Like image presented on Facebook timeline can be done using the header element. In my single-page app, I have multiple like buttons, each should be connected to a different image. What is the best way to associate a specific image with e ...

Exploring jQuery.each: A guide to navigating JSON objects

As a beginner in working with JSON, I am struggling to iterate over a JSON response received via AJAX. My objective is to extract and loop through checkbox values retrieved from a database table such as 2,3,7,9,3. However, I am currently facing difficultie ...

I have been dynamically inserting div elements into the DOM using JavaScript, using the appendChild method to add them and later removing them with removeChild. Strangely, when trying to append a div

There seems to be an issue in the code snippet below. The function create() works fine the first time, but encounters a problem on the second run at the line boxWrapper.appendChild(box);. It appears that the removeChild method is causing some disruption in ...

Using a promise inside an Angular custom filter

I am attempting to implement a filter that will provide either a success or error response from the ret() function. The current code is returning {}, which I believe is its promise. .filter('postcode', ['$cordovaSQLite', '$q' ...

How come it functions with $scope but fails with `this`?

I am trying to figure out why my code only works with scripts 1 and 3, but not with script 2. I need this functionality for my project because I can't use $scope. Thank you!! <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19 ...

Unexpected loading glitches occurring in vue-material components

I recently delved into the world of Vue.js and VueMaterial, which has been a refreshing and new experience for me since I've mostly focused on Native Android development in the past. Currently, I'm facing an unusual issue that seems to stem from ...

The Foundation 6 Zurb Template is not compatible for offline use

After successfully installing Foundation 6 Zurb Template via the cli, I encountered no issues. I then added the missing babel install and everything worked fine online. However, BrowserSync does not seem to work offline. Upon initiating watch, I receive a ...

Concern raised about the challenge of removing an element from an array and its potential

When attempting to remove an element from an array without altering the state, I typically use the following code snippet: const tempArray = [ ...originalArray ]; tempArray.splice(index, 1); setOriginalArray(tempArray); After some experimentation, I deci ...

What is causing this issue? Error: [$compile:nonassign] The expression 'undefined' cannot be assigned! AngularJS 1.5

I'm currently working on developing a component to post "Tweets" on my personal website. However, I am facing an issue where I cannot input any text into the textarea. Below is an image showcasing the error: Please review my code in the file "editor ...

Steps to insert an image object into a table

Note: The image in the database is named "profileImage." I want to create a dynamic image object similar to other objects when I insert this code. { label: "Image", name: "profileImage", } It will display the image endpoint as 3704545668418.PNG and ...

Retrieving data from radio buttons using React Hook Form

As I delve into learning React and Next.js, working with form submissions has been a breeze thanks to react-hook-form. However, I've hit a roadblock when it comes to handling radio buttons in my application. Specifically, there's a step where use ...