0

I'm trying to create an angular app to drop markers on a map. When you click the map, a function generates the marker and opens a form and then another function logs the position. You enter a description into the form and then when you submit it, the form updates a json database. The problem I'm having is that I can't get the position information to update.

HTML:

  <div class="jumbotron" id="map container" style="position: relative">
    <h1>Map in Angular</h1>

    <!--Map image-->
    <div (click)="logCursorPosition(e)" (click)="getCursorPositionX(e)" (click)="getCursorPositionY(e)" ></div>
    <img id="map" src="../assets/img/summonersRift.jpg" width="960" height="540" >
  </div>

<!--form appears on marker placement-->
<div class="form-popup" id="myForm">
  <form class="form-container" #mapMarker="ngForm" (ngSubmit)="markerFormSubmit(mapMarker.value)">
    <input type="text"   class="form-control" placeholder="Enter Description" name="description" ngModel>
    <button type="submit" class="btn btn-sm">Submit</button>
    <button type="button" class="btn cancel btn-sm" (click)="closeForm()">Close</button>
    <button type="button" class="btn delete btn-sm">Delete</button>
  </form>
</div>

ts file:

import { Component, OnInit, AfterViewInit} from '@angular/core';
import { DndDatabaseService } from './dnd-database.service';
import { HttpClient } from '@angular/common/http';
import { FormsModule }   from '@angular/forms';
import { markerData } from './markerData';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = 'map-app';

  constructor(private dndDatabaseService: DndDatabaseService,
              private httpClient: HttpClient,
              private http: HttpClient){};

  iconDescription:Object={};
  xPosition:Object={};
  yPosition:Object={};







  ngOnInit(){
      const iconImage = new Image();
      iconImage.src = "../assets/img/marker.jpg"

      //subscribes to the JSON file
      this.httpClient.get<markerData[]>('http://localhost:3000/mapMarker')
      .subscribe(posts => {
          posts.forEach(post=>{
                setMarker(post)})})      

      //Generate the markers from the json data
      function setMarker(post){
        console.log("function started", post.id)
        var x = post.xPos;
        var y = post.yPos;
        var img = document.createElement("img");
        img.src = "../assets/img/marker.jpg";
        img.width = 20;
        img.height= 20;
        img.style.position="absolute";
        img.style.left= (x-20)+'px';
        img.style.top=(y-40)+'px';
        document.getElementById('map container').appendChild(img);
      }

      //Listen for map events
      var map = document.getElementById('map')
      map.addEventListener("click", this.logCursorPosition);
      map.addEventListener("click", this.getCursorPositionX);
      map.addEventListener("click", this.getCursorPositionY);



  }

  getCursorPositionX(e){
    var x = (e.clientX)-20;
    //console.log("x:", x)
    this.xPosition = {"x": x};
    return this.xPosition
  }

  getCursorPositionY(e){
    var y = (e.clientY)-40;
    //console.log("y:", y)
    this.yPosition = {"y": y};
    return this.yPosition
  }

  logCursorPosition(e){
      //Get Cursor Location
      var x = e.clientX;
      var y = e.clientY;
      console.log("X Position "+ x + " Y Position" +y);
      //Place Icon
      var img = document.createElement("img");
      img.src = "../assets/img/marker.jpg";
      img.width = 20;
      img.height= 20;
      img.style.position="absolute";
      img.style.left= (x-20)+'px';
      img.style.top=(y-40)+'px';
      document.getElementById('map container').appendChild(img);
      img.id="currentMarker"
      //Open Form
      document.getElementById("myForm").style.display = "block"; //Opens the form
      document.getElementById('myForm').style.top = y +'px'; // sets the form y coordinate
      document.getElementById('myForm').style.left = x +'px'; // sets the form x coordinate
  }

//function that opens and closes the form for the icons
closeForm() {
  document.getElementById("myForm").style.display = "none";
}


  markerFormSubmit(marker){
      this.iconDescription ={
        "description": marker.description,
        "xPos":this.xPosition,
        "yPos":this.yPosition,
      }
    this.http.post("http://localhost:3000/mapMarker", this.iconDescription).subscribe((po:Response) => {console.log("po",po)})
    document.getElementById("myForm").style.display = "none";
    alert("Successfully Added")
  }
}

Db File

{
  ],
  "mapMarker": [
    {
      "description": "Example",
      "xPos": 450,
      "yPos": 450,
      "id": 1
    },
    {
      "description": "middle",
      "id": 2
    }
  ]
}
3
  • can you be slightly more specific as to where I should do this? In the getCursorPosition functions for the this.Position value? Commented Feb 27, 2020 at 17:45
  • I don't think using arrow function will solve your issue, sorry about that, If it possible can you create stackblitz? Commented Feb 27, 2020 at 19:28
  • I updated the stackblitz so now it actually works, except for the marker images stackblitz.com/edit/angular-z8nxkk Commented Feb 27, 2020 at 20:53

1 Answer 1

1

You can save items in localStorage. For example, you can set xPosition to x here:

getCursorPositionX(e){
    var x = (e.clientX)-20;
    localStorage.setItem("xPosition", x)
}

Then you can call it again when you need it here:

markerFormSubmit(marker){
  this.iconDescription ={
    "description": marker.description,
    "xPos": localStorage.getItem("xPosition"),
    "yPos": localStorage.getItem("yPosition"),
  }
this.http.post("http://localhost:3000/mapMarker", 
this.iconDescription).subscribe((po:Response) => 
    {console.log("po",po)})
document.getElementById("myForm").style.display = "none";
alert("Successfully Added")

}

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

This is not the solution I went with, however I recently learned about localStorage for another part of my app and this would work so I'm marking it as correct. Thank you for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.