AngularJS: Retrieving marker coordinates post map drag action

My CSS displays a marker on the map and allows users to drag a map like Uber. Now, I need help getting the current location of the marker. I have included style.css, DeliverCtrl.js, and HTML code. Can anyone assist me with this?

style.css

map{
    display: block;
    width: 100%;
    height: 50%;
    overflow:visible;
}
map:after {
    width: 22px;
    height: 40px;
    display: block;
    content: ' ';
    position: absolute;
    top: 50%; left: 50%;
    margin: -40px 0 0 -11px;
    background: url('https://maps.gstatic.com/mapfiles/api-3/images/spotlight-poi_hdpi.png');
    background-size: 22px 40px; /* Since I used the HiDPI marker version this compensates for the 2x size */
    pointer-events: none; /* This disables clicks on the marker. Not fully supported by all major browsers, though */
}

DeliverCtrl.js

angular.module('Home').controller('DeliverCtrl',["$scope","$state", function($scope, $state, $ionicModal, MessageService, $stateParams) {

    function initialize() {
        var position = new google.maps.LatLng($state.params.lat,$state.params.lng);

        $scope.mapOptions = {
        mapTypeControl: true,
        zoom: 15,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: position
    };

};

$scope.createMarker = function(latLng)
{

    var map = $scope.map;

    var marker = new google.maps.Marker({
        position: latLng,
        map: map,
        title: 'Current Location'
    });
};

$scope.mapCreated = function(map) {
    $scope.map = map;
};

initialize();
google.maps.event.addDomListener(window, 'load', initialize); 
}]);

I have generated the map using the following HTML

<map on-create="mapCreated(map)" mapoptions="mapOptions"></map>

Answer №1

Based on the provided CSS code, it appears that you are attempting to display a marker image on top of a map while displaying the map underneath the marker.

In this situation, you likely want to retrieve the center of the map once the user has finished dragging the map.

To obtain the center of the map, you can utilize the map.getCenter() function.

To determine when the map has finished being dragged, you can employ some of the User Interface Events available in Google Maps API (as shown in the first example). For your scenario, using the idle event is recommended over events like center_changed, as it is triggered less frequently. However, you have the flexibility to choose from the list of available events.

Your code snippet should resemble the following:

$scope.mapCreated = function(map) {
    $scope.map = map;
    $scope.current_center = null;
    google.maps.event.addListener($scope.map, 'idle', function(){
       $scope.current_center = $scope.map.getCenter();
       console.log($scope.current_center.toString()); //This will display the latitude and longitude of the map's center after dragging/zooming is completed
    });
};

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

AngularJS: Utilizing Multiple HTTP Requests with the $http Service

Here is the code snippet I am working with: var updateScreen = function() { $http.get("http://localhost:5000").then(function(response){ $scope.numSerie = response.data.refDetail.Num_Serie; $http.get("data/tempsgammes ...

Tips for switching the login button to a logout button using React

Hey there! I'm currently working on my first single page app using react.js, and I'm encountering some issues with syntax. My main goal is to switch the login button to a logout button once the user has signed in. Can anyone help me figure out wh ...

Angular: identifying inconsistencies between previous and current values with range slider and numeric input spinner

Attempting to resolve a discrepancy between oldVal and newVal in a 'range slider' and 'number spinner input' that are both linked. Discovered a bug where the difference between oldVal and newVal is much larger than expected, considering ...

I'm noticing a significant slowdown on my webpage due to a high volume of href URLs in my HTML script. What steps can I take to resolve this issue

As a newcomer to scripting, I spent countless hours researching to find a solution but couldn't crack it. I implemented a jquery image search script that functioned smoothly with all components embedded in a single HTML code. However, things took a t ...

How about this: "Is the text in the columns of the <table> body aligned to the

In my HTML5 table, I have 3 header columns with a colspan of 2 each, making it a total of 6 columns wide. Below each header are 2 body columns corresponding to that header. While the individual columns are aligned left, I am now looking to center align the ...

Delay the page briefly before redirecting

I want to implement a feature on my WordPress website where after successfully submitting a form, the visitor is shown a message like 'form submitted successfully' for a few seconds before being redirected back to the page they were on. The redir ...

Having trouble accessing an AngularJS $scope variable because it is coming up as undefined

Currently, I am developing an Electron application using AngularJS for the frontend. Since node.js operates at the OS level while Angular runs at the client level, communication between the two is achieved through IPC (Inter-Process Communication) implemen ...

The visibility of the Bootstrap 4 collapse button seems to be missing

I am currently working on optimizing the responsiveness of my navigation bar. However, I encountered an issue where the button that collapses the nav bar is not visible when I resize my browser for testing purposes. Even after attempting to set a dark back ...

Searching for a specific object within an array using React

const information = [ { id: "card1", img: require("./image/prosperity.jpg"), title: "Increasing Prosperity Through Positive Thinking", tags: ["Art", "Design"], desc: " ...

Display the returned view following an AJAX request

Currently, I am trying to search for a specific date in the database using ajax to trigger the function. However, I am encountering a 404 error when I should be receiving the desired outcome, and the ajax call is entering an error mode. Here is the snippet ...

What are the steps to incorporate page navigation within AngularJS?

I am working with two HTML pages named main.html and detail.html. I want to open the detail page for more information when clicking a link button in the main page. Main.html: <body ng-app="MyApp" ng-controller="mainController"> <div data-role ...

What steps can I take to ensure my website maintains a consistent appearance across various internet browsers?

I'm fairly new to web development; my knowledge is limited to CSS and HTML, just enough to make a website look good. I would appreciate it if you could explain things in simple terms. I've been researching how to ensure that a website looks the ...

The battle between percentages and pixels in fluid design

I am looking to create a flexible layout and would like to achieve the following: width:100%-200px; This means having a div that contains content, referred to as div id="content", with a fixed margin on each side. I have attempted to place the div id="co ...

How to efficiently manage multiple objects as a unified entity in Three.js

Hello, I am new to the world of three.js and I am facing a challenge. I want to combine multiple objects into one single entity, essentially creating a bas-relief effect. Specifically, I need to render the following elements: A box geometry serving as th ...

What is the best way to allocate the remaining space to one div while setting the first div to 20vmin?

I am facing a challenge with two div elements in my design. One of the divs has been set to have a height of 20vmin, but I am struggling to make the other div take up the remaining height. Initially, I experimented with using 80vmin for the second div, ho ...

JavaScript Array failing to transfer to PHP using AJAX

I've encountered a recurring issue with my code and despite searching for solutions, I can't seem to find one that works for me. The problem lies in trying to delete a specific row from a table based on whether the user selects a checkbox associa ...

Correct the initial error in the column by retroactively applying the source map

Whenever our Selenium tests run on our web application, we always have access to the console logs in case there is a javascript crash. The typical error message usually appears like this: 09:27:09.540 [main] ERROR gui.rule.ScreenshotRule - Chrome console: ...

Beginning your journey with Mock server and Grunt

Although I have gone through the documentation available at , I am still struggling to get the mockserver up and running despite spending hours on following the instructions provided in the guide. Could someone please outline the precise steps or identify ...

Button Triggering Javascript

Looking for a handy solution that allows users to either accept or reject a website's cookie policy. I came across an interesting library called cookies-EU-Banner (found at ) which seems to be quite popular. It recognizes when the user clicks on Reje ...

Using NodeJS to generate a multipart/mixed response

I am faced with a particular situation: creating a multipart/mixed response in NodeJS with full control over the communication on both ends to avoid interoperability issues. A JSON file containing a list of nodes describing each ZIP file, for example: [{ ...