There was a problem finding the correct address indicated by the marker

I am working on an Android app using PhoneGap, and I need to display a marker on a Google map at a specific latitude and longitude. When the marker is clicked, I want to show an info window displaying the address associated with that location. However, the address being displayed is incorrect in my current code. What do I need to change to fix this issue?

   var query = window.location.search.substring(1);
   var parms = query.split('&');
   var pos = parms[0].indexOf('=');
        if (pos > 0) {
            currentlat = parms[0].substring(pos + 1);               
                pos = parms[1].indexOf('=');
                currentlong = parms[1].substring(pos + 1);
        var orgplace = new google.maps.LatLng(currentlat, currentlong);

  service.getDistanceMatrix(
     {
         origins: [orgplace],
         destinations: datatblAcc,
         travelMode: google.maps.TravelMode.DRIVING,
         unitSystem: google.maps.UnitSystem.METRIC,
         avoidHighways: false,
         avoidTolls: false
     }, callback);

 function callback(response, status) {

        var tblhtml = document.getElementById('listaddress');
        if (status != google.maps.DistanceMatrixStatus.OK) {
        document.getElementById('view-loadingpoi').style.display = "none";

        } else {
        setTimeout(function(){
          var origins = response.originAddresses;
            var destinations = response.destinationAddresses;
            var outputDiv = document.getElementById('outputDiv');
            outputDiv.innerHTML = '';
            deleteOverlays();

            var data = '';
            var localplace = new google.maps.LatLng(currentlat, currentlong);
            if ((currentlat == 0))
            {
                localplace = new google.maps.LatLng(-65.994173, 78.127421);

                    currentlat = "-65.994173";
                    currentlong = "78.127421";
            }

            maploc = new google.maps.Map(document.getElementById("mapdisp"), {
                center: localplace,
                zoom: 14,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                disableDefaultUI: true
            });               

            var image = 'images/greenpointer.png';
            var pointcur = new google.maps.LatLng(currentlat, currentlong);
            var marker = new google.maps.Marker({
                position: pointcur,
                icon: image,
                map: maploc
            });

            google.maps.event.addListener(marker, "click", function (event) {
                if (infowindow) infowindow.close();

                var evelatitu = currentlong.split('.')[1];

                if (((currentlat.replace(/^\s+|\s+$/g, '').indexOf(event.latLng.lat())) !== -1) && ((currentlong.replace(/^\s+|\s+$/g, '').indexOf(event.latLng.lng().toFixed(evelatitu.length))) !== -1)) {
                    var txthtml = "<div class='mappopuploc'>My Location<br/>" + response.originAddresses[0] + "</div>";
                    infowindow = new google.maps.InfoWindow({

                        content: txthtml
                    });


                    infowindow.open(maploc, marker);
                }
            });                    

Answer №1

Utilize the marker title in the marker click event listener to obtain the precise address

function responseHandler(res, s) {

    var tblhtml = document.getElementById('listaddress');
    if (s != google.maps.DistanceMatrixStatus.OK) {
        document.getElementById('view-loadingpoi').style.display = "none";

    } else {
        setTimeout(function(){
            var origins = res.originAddresses;
            var destinations = res.destinationAddresses;
            var outputDiv = document.getElementById('outputDiv');
            outputDiv.innerHTML = '';
            deleteOverlays();

            var data = '';
            var localplace = new google.maps.LatLng(currentlat, currentlong);
            if ((currentlat == 0))
            {
                localplace = new google.maps.LatLng(-65.994173, 78.127421);

                currentlat = "-65.994173";
                currentlong = "78.127421";
            }

            maploc = new google.maps.Map(document.getElementById("mapdisp"), {
                center: localplace,
                zoom: 14,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                disableDefaultUI: true
            }); 

            var image = 'images/greenpointer.png';
            var pointcur = new google.maps.LatLng(currentlat, currentlong);
            var marker = new google.maps.Marker({
                position: pointcur,
                icon: image,
                map: maploc,
                title: res.originAddresses[0]
            });

            google.maps.event.addListener(marker, "click", function (event) {
                if (infowindow) infowindow.close();

                var evelatitu = currentlong.split('.')[1];

                if (((currentlat.replace(/^\s+|\s+$/g, '').indexOf(event.latLng.lat())) !== -1) && ((currentlong.replace(/^\s+|\s+$/g, '').indexOf(event.latLng.lng().toFixed(evelatitu.length))) !== -1)) {
                    var txthtml = "<div class='mappopuploc'>My Location<br/>" + this.title + "</div>";
                    infowindow = new google.maps.InfoWindow({

                        content: txthtml
                    });

                    infowindow.open(maploc, marker);
                }
            });    
        

}

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

Creating arrays in JavaScript with or without the use of jQuery

In my JavaScript script, I am defining this array: var status=[ { "name":"name1", "level":0 }, { "name":"name2", "level":0 }, { "name":"name3", "level":0 }, { "name":" ...

Is it better to append content in JQuery rather than replacing it with .innerHTML?

Here is a function designed to retrieve older wallposts from a user, 16 at a time, and add each chunk of 16 to the end of the current list in the div called "sw1". The function works well, except when there is a wallpost containing a video or embedded obj ...

Using Cheerio with a Node.js bot

I am currently utilizing Cheerio to extract information from web pages in my .js files. However, I would like these files to automatically restart every 1 day to check for any new data. Instead of using setTimeout, which may not be efficient for managing ...

Retrieve the input field's value with Selenium, verify its accuracy, and proceed to log a message to the console

Hey there! I'm facing a challenge while working with Selenium Webdriver, specifically Chrome Webdriver and writing tests in JavaScript. The problem is in a section of the code where I can't seem to grab the value typed into an input field using t ...

Embed a stackoverflow.com iframe within a jsbin

While exploring code using jsbin, I mistakenly linked the iframe to . When my work in loads, it triggers an alert that redirects back to . I am unable to figure out how to modify my jsbin code. Is there a solution or should I start from scratch? ...

Can Vuex mapActions be utilized within a module that is exported?

Is it possible to utilize Vuex mapActions from an external module imported into a component? I am working on standardizing a set of functions in a vue.js web application. My goal is to import these functions into each component and pass necessary values f ...

Having trouble with JSONP cross-domain AJAX requests?

Despite reviewing numerous cross-domain ajax questions, I am still struggling to pinpoint the issue with my JSONP request. My goal is simple - to retrieve the contents of an external page cross domain using JSONP. However, Firefox continues to display the ...

Maximizing the efficiency of this JavaScript code

Is there a way to optimize this code for better efficiency? I'm trying to enhance my coding skills and would appreciate some feedback on this particular section: $(function(){ $('#buscar').focus(function(){ if(($(this).val() === ...

An object that appears to be empty at first glance, but contains values that are undefined

I am facing an issue with my object that I am populating with information. The logs show the object as empty, but when I inspect it in Chrome, it appears to have the correct details filled in. Here is a snapshot of what the logs display: closed: closed o ...

What is the best way to add an element conditionally within a specific Vue Component scope?

I've been working on creating a Component for titles that are editable when double-clicked. The Component takes the specific h-tag and title as props, generating a regular h-tag that transforms into an input field upon double click. It's function ...

Tips for obtaining the identifier of a div element while employing the bind() function in jQuery

Imagine having the following div. <div id="456" class="xyz">Lorem Ipsum</div> If I want to execute a function when this specific div is hovered over, I can achieve it like this: $(".xyz").bind({ mouseenter : AnotherFunction(id) }); Prio ...

storing audio files locally with Vue.js

Looking for a way to store a sound locally for my Battleship game rather than referencing it on the internet. Here's the code that triggers the sound: @click.prevent="fireSound('http://soundbible.com/grab.php?id=1794&type=mp3')" I atte ...

AngularJS login form with JSON data

I am currently learning Angular and focusing on the login form implementation. The specific model I am working with can be found in this PLNKR. There are two challenges that I am struggling to resolve. Issue 1: I'm trying to figure out how to tur ...

Making a list of members from an array of objects

Let's say I have a collection of items structured like this: var itemList = [ { date: '22/9/2016', status: 1, id: '11111' }, { date: '23/9/2016', status: 1, id: '22222' }, { date: '24/9/2016&ap ...

Challenge encountered with asynchronous angular queries

Dealing with asynchronous calls in Angular can be tricky. One common issue is getting an array as undefined due to the asynchronous nature of the calls. How can this be solved? private fetchData(id){ var array = []; this.httpClient.get('someUrl ...

What is the best way to assign a JavaScript return value to a PHP variable?

Is it possible to capture the return value of a JavaScript function that checks if text boxes are empty and assign it to a PHP variable? I have successfully created a JavaScript function that returns false if any of the text boxes are empty, but now I ne ...

Is there a way to dynamically import a JSON file within an ECMAScript module?

Currently, I am attempting the following in my code: let filePath = '../../data/my-file.json' import inputArray from filePath assert { type: 'json' } The outcome of this operation is as follows: file:///.../script.mjs:5 import inputArr ...

Calculate averages of an array and show them when the page loads using Vue

In my coding project, there is an array named products that follows this specific structure: { "_id": "150", "name": "Milk", "description": "Skimmed", "price": "10", "ratings": [ ...

Ways to reset the input field of Material UI

I'm encountering an issue with clearing a form that I created using Material UI. Despite searching for solutions on Stackoverflow, none of them seem to work for me. Using setState did not achieve the desired result of clearing the form. I am looking ...

Introducing an alternative solution for handling tasks linked to the completion of CSS3 animations

In order to incorporate smooth CSS3 animations into my website, I have implemented the animate.css library created by Dan Eden. One particular scenario involves a loading screen that features a fade-out animation. It is crucial for this animation to comple ...