Categories
JavaScript Answers

How to get coordinates of marker in Google Maps API and JavaScript?

Sometimes, we want to get coordinates of marker in Google Maps API and JavaScript.

In this article, we’ll look at how to get coordinates of marker in Google Maps API and JavaScript.

How to get coordinates of marker in Google Maps API and JavaScript?

To get coordinates of marker in Google Maps API and JavaScript, we listen for the dragend event.

For instance, we write

const map = new google.maps.Map(document.getElementById("map_canvas"), {
  zoom: 1,
  center: new google.maps.LatLng(35, -82),
  mapTypeId: google.maps.MapTypeId.ROADMAP,
});

const myMarker = new google.maps.Marker({
  position: new google.maps.LatLng(41, -100),
  draggable: true,
});

google.maps.event.addListener(myMarker, "dragend", (evt) => {
  console.log(evt.latLng.lat().toFixed(3), evt.latLng.lng().toFixed(3));
});

map.setCenter(myMarker.position);
myMarker.setMap(map);

to listen for the dragend event of the myMarker marker to get its coordinates after dragging.

We get its latitude with evt.latLng.lat.

And we get its latitude with evt.latLng.lng.

Conclusion

To get coordinates of marker in Google Maps API and JavaScript, we listen for the dragend event.

Categories
JavaScript Answers

How to add custom styles for info window with Google Maps API v3 and JavaScript?

Sometimes, we want to add custom styles for info window with Google Maps API v3 and JavaScript.

In this article, we’ll look at how to add custom styles for info window with Google Maps API v3 and JavaScript.

How to add custom styles for info window with Google Maps API v3 and JavaScript?

To add custom styles for info window with Google Maps API v3 and JavaScript, we set the boxStyle property.

For instance, we write

<div class="infobox-wrapper">
  <div id="infobox1">Infobox</div>
</div>

to add divs for the infobox.

Then we write

const infobox = new InfoBox({
  content: document.getElementById("infobox1"),
  disableAutoPan: false,
  maxWidth: 150,
  pixelOffset: new google.maps.Size(-140, 0),
  zIndex: null,
  boxStyle: {
    background:
      "url('http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/examples/tipbox.gif') no-repeat",
    opacity: 0.75,
    width: "280px",
  },
  closeBoxMargin: "12px 4px 2px 2px",
  closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
  infoBoxClearance: new google.maps.Size(1, 1),
});

to create an infobox from the element with the InfoBox constructor.

We set content to the div.

And we set boxStyle to the custom styles.

We also set the margin for the close box with closeBoxMargin and the max width with maxWidth.

Conclusion

To add custom styles for info window with Google Maps API v3 and JavaScript, we set the boxStyle property.

Categories
JavaScript Answers

How to calculate percentage with JavaScript?

Sometimes, we want to calculate percentage with JavaScript.

In this article, we’ll look at how to calculate percentage with JavaScript.

How to calculate percentage with JavaScript?

To calculate percentage with JavaScript, we use arithmetic operators.

For instance, we write

const percentage = (partialValue, totalValue) => {
  return (100 * partialValue) / totalValue;
};

const totalActivities = 10;
const doneActivities = 2;

const result = percentage(doneActivities, totalActivities);

to define the percentage function that multiplies partialValue by 100 and then divide the product by totalValue to get the percentage.

Conclusion

To calculate percentage with JavaScript, we use arithmetic operators.

Categories
JavaScript Answers

How to check for null inline in JavaScript?

Sometimes, we want to check for null inline in JavaScript.

In this article, we’ll look at how to check for null inline in JavaScript.

How to check for null inline in JavaScript?

To check for null inline in JavaScript, we use the optional chaining operator.

For instance, we write

const example = { a: ["first", { b: 3 }, false] };

console.log(example?.a);
console.log(example?.b);

console.log(example?.a?.[0]);
console.log(example?.a?.[1]?.a);
console.log(example?.a?.[1]?.b);

to use the optional chaining ?. operator to return undefined if any property in the chain is null or undefined.

Conclusion

To check for null inline in JavaScript, we use the optional chaining operator.

Categories
JavaScript Answers

How to close Bootstrap dropdown after link click with HTML?

Sometimes, we want to close Bootstrap dropdown after link click with HTML.

In this article, we’ll look at how to close Bootstrap dropdown after link click with HTML.

How to close Bootstrap dropdown after link click with HTML?

To close Bootstrap dropdown after link click with HTML, we add the dropdown-toggle class.

For instance, we write

<a class="dropdown-toggle"></a>

to add the dropdown-toggle class to the link to close Bootstrap dropdown after link click with HTML.

Conclusion

To close Bootstrap dropdown after link click with HTML, we add the dropdown-toggle class.