Not being able to create a Draggable Marker when getting latitude and longitude by geocoding

76 views Asked by At

So, I need to create a draggable marker by using latitude and logitude from an address. To get the lat and lng from the address, I did this function:

var longit;
var lati;

function searchGeocoding() {
  map.removeObjects(map.getObjects());
  let search;
  search = document.getElementById("myText").value;
  service.geocode(
    {
      q: search,
    },
    (result) => {
      result.items.forEach((item) => {
        map.setCenter(item.position);
        map.setZoom(16);
        lati = item.position.lat;
        longit = item.position.lng;
        addDraggableMarker(map, behavior);
      });
    },
    alert
  );
}

Which works fine. However, when I try to create the draggable marker using the example from Here API Docs, it gives me the following error:

mapsjs-core.js:43 Uncaught InvalidArgumentError: H.map.AbstractMarker#setGeometry (Argument #0 undefined)
    at new D (https://js.api.here.com/v3/3.1/mapsjs-core.js:43:977)
    at Xf (https://js.api.here.com/v3/3.1/mapsjs-core.js:89:407)
    at on.ai.ba (https://js.api.here.com/v3/3.1/mapsjs-core.js:177:548)
    at on.ai [as constructor] (https://js.api.here.com/v3/3.1/mapsjs-core.js:177:309)
    at new on (https://js.api.here.com/v3/3.1/mapsjs-core.js:395:290)
    at addDraggableMarker (http://127.0.0.1:5500/js/test.js:32:16)
    at http://127.0.0.1:5500/js/test.js:21:9
    at Array.forEach (<anonymous>)
    at http://127.0.0.1:5500/js/test.js:14:20
    at e.f (https://js.api.here.com/v3/3.1/mapsjs-core.js:31:102)

There's also a problem with the addDraggableMarker function, I noticed that it doesn't get past the marker.draggable = true; line. You can see the example code here (I am using this exact same code, except that instead of lat:42.35805, lng:-71.0636 is lat: lati and lng: longit.

1

There are 1 answers

0
Shruti Kuber On

I implemented your snippet to find out what the problem is. if you are using oninput() for your text box, it calls the geocoder for every letter you type. Thus the geocoder has no response for those incomplete words. You can test this by putting if(result.items.length > 0). If would recommend using the autosuggest API if you want the result as the user is typing. Or you an use a submit button to call the function only when the user enters the complete string.