Encountering an issue when using both the Google Maps API and Google URL Shortener API within the same program

Recently, I developed a program that involves passing data to an iframe through a URL. However, due to the limitation of Internet Explorer supporting only 2083 characters in a URL, I decided to use the Google URL Shorten API to shorten the URL before sending it as an argument. The plan was to later retrieve the long URL using the shortened URL and extract the necessary data.

The challenge I encountered was calling two APIs within the same program.

Below is a snippet of the code where I fetch the long URL from Google and then initialize the map. According to my logic, we need to first extract the data from the long URL before we can pinpoint the markers on the map. Therefore, initializing the map before calling the Google shorten URL API is not feasible.

Check out the code below -

<html>
<head>
</head> 
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script>
function makeURLShortenApiCall(){
var shortUrl="";
gapi.client.setApiKey('AIzaSyDQo3yvYTgw7pdfGTRz6zanq41rhymA-N8');
gapi.client.load('urlshortener', 'v1',function(){

var shortUrl="http://goo.gl/AWGcY4";

    var request = gapi.client.urlshortener.url.get({
      'shortUrl': shortUrl,
    'projection':'FULL'
    });
    request.execute(function(response) 
    {

        if(response.longUrl!= null)
        {
            console.log(response.longUrl);
            initialize();
        }
        else
        {
            alert("error: "+response.error);
        }

    });

});
}
</script>
<script src="https://apis.google.com/js/client.js?onload=makeURLShortenApiCall"></script>
<script>
function initialize() {
  var mapProp = {
    center:new google.maps.LatLng(51.508742,-0.120850),
    zoom:5,
    mapTypeId:google.maps.MapTypeId.ROADMAP
  };
  var map=new google.maps.Map(document.getElementById("Map"),mapProp);
}
</script>
<body>
<div id="googleMap" style="width:500px;height:380px;" class="absolute"></div>
</body>
</html>

Upon running the program, an error message popped up saying -

Uncaught TypeError: Cannot read property 'offsetWidth' of null

I'm currently stuck with this issue. Any suggestions on how to troubleshoot and solve this problem?

Answer №1

I actually identified the issue.

The problem arose because

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

The Map id did not correspond to the div id named googleMap

<div id="googleMap" style="width:500px;height:380px;" class="absolute"></div>

Nevertheless, I appreciate your assistance.

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

Instructions on removing an HTML element from a div that has the attribute contentEditable

Here is an example of HTML code: <div id="editable" contentEditable="true"> <span contentEditable="false">Text to remove</span> </div> I want to be able to delete the entire span element (along with its text) with just one bac ...

User events in the fullCalendar

I'm feeling stuck. I currently have a basic fullCalendar setup for the User model in Rails. I want to add 2 buttons, allowing the User to separate the calendar into two views. One view would display only their own events, while the other would show al ...

Encountering issues with normalizing the HttpRequests <class, str> type when utilizing urllib.request

I am currently using urllib.request to implement the GET method. The response type I'm getting is HttpRequests <class , str> Issue: I am encountering an error that prevents me from normalizing the response with pandas.json_normalize TypeErro ...

The issue with Bootstrap 5 navbar dropdown links not functioning on screens smaller than 576px

I'm completely new to this. I managed to set up a functional navbar with a dropdown feature that consists of 3 links. When expanded, each dropdown link displays a card upon clicking, revealing contact information. Everything works perfectly fine...exc ...

Display the data count of a filtered list in Vue 2

As a newcomer to Vue, I am facing what seems like a simple and common task but the solution is escaping me. I need to filter some data and then display the count in a component. This is the HTML structure: <main class="container-fluid"> <div ...

Using the special character ":" for routing in Angular2

My website URLs follow this pattern: abc.org/books/sort:created/direction:desc abc.org/books/sort:mostviewed/direction:desc abc.com/books/schools/sort:created/direction:desc abc.com/books/schools/sort:mostviewed/direction:desc I am having an issue where ...

How to disable click event binding in Angular2 after it has been clicked once

Within my application, there is a button that has a click event attached to it: <button class="btn btn-default" (click)="doSomething()"> I am wondering if there is a way to remove the (click) event from the button within the doSomething method so t ...

Trigger a jQuery event when an element is moved to a new line

Is there a way to trigger a jQuery event when an element moves to a new line in the view? I want to hide navigation items and run additional JavaScript code when this happens. Any ideas on how to achieve this? Here's how my navigation appears in a wi ...

Is there a way to retrieve the textFrame where the cursor is currently located?

While inputting text into a frame, I am looking to execute a script. I want this script to target the specific text frame where my cursor is located. How can I reference this particular textFrame using Javascript? ...

Creating an event within a class that is generated dynamically when hovering

I'm trying to create a hover effect where an image moves around the page in a pattern (top left corner -> top right corner -> bottom right corner -> bottom left corner -> then back up to the top left corner). To achieve this, I am adding ...

Inspection Techniques for Javascript Objects

Is there a way to display an object's properties and methods in an alert box? When I try alerting an object, it only shows the nodename: alert(document); I'm looking for a way to see all the details of the object in the alert message. Is there ...

Modify the PrimeFaces dialog to be modal using client-side code

One of my primefaces dialogs is set up like this: <p:dialog widgetVar="dlg" width="320" height="220" modal="false" closable="false" showHeader="false" resizable="false" position="right,top"> I am looking to make this dialog modal if a certain eleme ...

Using either AngularJS's simple .then method or the $q service to handle asynchronous requests

I'm trying to understand the distinction between AngularJS $q service and simply using .then() after an asynchronous request. For example, using .then(): function InboxService($http) { this.getEmails = function getEmails() { return $http.get(& ...

Benefits of utilizing minified AngularJS versions (Exploring the advantages of angular.min.js over angular.js, along with the inclusion of angular.min.js.map)

After introducing angular.min.js into my project, I encountered a problem. http://localhost:8000/AngularProject/angular.min.js.map 404 (Not Found) angular.min.js.map:1 Upon further investigation, I discovered that including angular.min.js.map resolve ...

How can I use jQuery UI to slide a div, while also smoothly moving the adjacent div to take its place?

Wishing you an amazing New Year! I am looking to create a smooth sliding effect for a div when a button is clicked. I want the adjacent div to slide alongside it seamlessly, without any clunky motions or delays. Currently, the adjacent div only moves afte ...

Is there a way to remove the "next" button from the last page in a table?

I'm currently working on implementing pagination for my table. So far, I have successfully added both the "next" and "previous" buttons, with the "previous" button only appearing after the first page - which is correct. However, I am facing an issue w ...

How can we simplify this React component to reduce its verbosity?

After creating a test project to delve into react, react-router and react-redux, I revisited the Settings.jsx file. Now, I am pondering on how to streamline it and reduce potential errors. import React, { Component } from "react"; import { connect } from ...

Centering the scrollIntoView feature on mobile devices is presenting challenges with NextJS applications

Description While navigating on mobile browsers, I'm facing a challenge with keeping an element centered as I scroll due to the browser window minimizing. I've experimented with different solutions such as utilizing the react-scroll library and ...

Running a script within an Ajax-rendered form using remote functionality in a Ruby on Rails

After clicking the following button, my form is rendered: = link_to 'New Note', new_note_path, :class => "btn btn-primary new-note-button", :type => "button", :id => "new-link", remote: true The form is rendered using the script below ...

A method for assigning a single event listener to multiple events in a React component

I find myself in a situation where I have two events, onClick and onSelect, both of which share the same event handler. I am wondering what the most efficient way to handle this scenario would be - should I create a common method and then call the event ...