Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9108535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:49:51+00:00 2026-06-17T02:49:51+00:00

I want to build web app with the Leaflet API. First my user is

  • 0

I want to build web app with the Leaflet API. First my user is geolocated with IP then if he accepts I try to update his position with HTML5 geolocation (accuracy is better).

My problem is that the marker position is not updated on the map and the code bellow fails with no error. I have try hundred of different syntax and methods from leaflet documentation to update marker position (ie. setLatLng) but I did not succeed. I would like to understand what’s wrong with my code.


My problem is solved by doing like this :

    var lat = (e.latlng.lat);
    var lng = (e.latlng.lng);
    var newLatLng = new L.LatLng(lat, lng);
    marker.setLatLng(newLatLng); 

Old code was:

//initial IP based geolocation

var lat = google.loader.ClientLocation.latitude;
var lng = google.loader.ClientLocation.longitude;

//place marker on the map

var marker = L.marker([lat,lng]).addTo(map);

//start HTML5 geolocation 

map.locate({setView: true, maxZoom: 16});

function onLocationFound(e) {

    var marker = L.marker([e.latlng.lat,e.latlng.lng]).update(marker);
    alert ('New latitude is ' + e.latlng.lat)
}

map.on('locationfound', onLocationFound);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:49:53+00:00Added an answer on June 17, 2026 at 2:49 am

    The code inside your question is a little bit confusing, it’s hard to say what the issue is when you only post snippets.

    As it is, this code:

        var lat = (e.latlng.lat);
        var lng = (e.latlng.lng);
        var newLatLng = new L.LatLng(lat, lng);
        marker.setLatLng(newLatLng); 
    

    ..should work as expected inside onLocationFound().

    You can simplify it:

    marker.setLatLng(e.latlng);
    

    However, I guess the problem is a scope-issue, some of your variables (e.g. marker) is not accessible inside onLocationFound.

    Here an example how to achieve it:

    function init(){
        var map             = L.map('map', {center: [51.505, -0.09], zoom: 13}),
            marker          = L.marker(map.getCenter()).addTo(map),
            glcl            = google.loader.ClientLocation,
            onLocationfound = function(e){
              marker.setLatLng(e.latlng);
              map.setView(marker.getLatLng(),map.getZoom()); 
              alert('Marker has been set to position :'+marker.getLatLng().toString());
            };
    
        L.tileLayer('http://{s}.tile.cloudmade.com/[yourCloudmadeKey]/997/256/{z}/{x}/{y}.png').addTo(map);
    
        map.on('locationfound', onLocationfound);
    
        if(glcl){//when google.loader.ClientLocation contains result
           onLocationfound({latlng:[glcl.latitude,glcl.longitude]});
        }else{alert('google.loader.ClientLocation fails');}
    
        map.locate();
    } 
    

    Demo: http://jsfiddle.net/doktormolle/6ftGz/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to build a Java web app and deploy it on EC2 .
I want to build a REST web service on app engine. Currently i have
I want to build a web app for myself that will allow me to
I want to build an entire web app using only Javascript and MYSQL .
i want to build an information support system which is an web app. and
I am (trying) to build a web app, first one :) I need to
I am developing web forms app in C# Framework 3.5 I want to build
I'm trying to build a Google Maps web app. I store flats and want
I am planning to build web app in spring mvc , hibernate. i want
i want to build a web service for iphone app that get the msg

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.