Click here to Skip to main content
15,867,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
<input type="button" data-toggle="modal" data-target="#myModal" onclick="getGeoLocation();" class="form-button form-button-submit btn btn-theme btn-theme-dark" value="Get Location" />

function getGeoLocation(){
    var map;
var latitude = 13.0547712; // I need to replace this with current location lat
var longitude = 80.2578432;// I need to replace this with current location lang
      var myLatlng = new google.maps.LatLng(latitude,longitude);
      var myOptions = {
      zoom: 17,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.SATELLITE
    };
    map = new google.maps.Map(document.getElementById('map_div'),
        myOptions);

    // marker STARTS    
    var marker = new google.maps.Marker({
    position: myLatlng, 
    title:"Click to view info!"
});  
    marker.setMap(map);
    // marker ENDS
google.maps.event.addListener(map, "click", function (e) {

    //lat and lng is available in e object
    var latLng = e.latLng;
    var lat = e.latLng.lat();
    var lang = e.latLng.lng();
$('#latitude').val(lat);
$('#longitude').val(lang);
});
  // google.maps.event.addDomListener(window, 'load', initialize);
  }


What I have tried:

<pre>navigator.geolocation.getCurrentPosition(function(position) {

                    var geolocate = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                    console.log(position.coords.latitude);

                });


getCurrentPosition
is not giving me the lat and lang
Posted
Updated 10-Apr-19 20:58pm

1 solution

1) Create one javascript function which will return current position in Latitude and Longitude.
2) Call this function on click of button
3) After getting position you can call the function getGeoLocation with position



JavaScript
var map, infoWindow;
      
        infoWindow = new google.maps.InfoWindow;

        // Try HTML5 geolocation.
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
          
          var Lat = position.coords.latitude,
          var Long = position.coords.longitude
           getGeoLocation(Lat,Long)

          }, function() {
             //error
          });
        } else {

          // Browser doesn't support Geolocation
         
        }
      }


function getGeoLocation(_lat,_long){
    var map;
var latitude = _lat;   
var longitude = _long; 
// do your remaining code
 
Share this answer
 
v2
Comments
Janardhanam Julapalli 11-Apr-19 3:42am    
I have tried something like you have mentioned above. Now I have used the code which you have given.

function getCurrentLatLang(){
var map, infoWindow;

infoWindow = new google.maps.InfoWindow;

// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {

var Lat = position.coords.latitude;
var Long = position.coords.longitude;
getGeoLocation(Lat,Long)

}, function() {
console.log('error');//this line being executed
});
} else {

// Browser doesn't support Geolocation

}
}

function getGeoLocation(latitude,longitude){
var map;
// var latitude = 13.0547712;
// var longitude = 80.2578432;
var myLatlng = new google.maps.LatLng(latitude,longitude);
var myOptions = {
zoom: 17,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.SATELLITE
};


map = new google.maps.Map(document.getElementById('map_div'),
myOptions);

// marker STARTS
var marker = new google.maps.Marker({
position: myLatlng,
title:"Click to view info!"
});
marker.setMap(map);
// marker ENDS
google.maps.event.addListener(map, "click", function (e) {

//lat and lng is available in e object
var latLng = e.latLng;
var lat = e.latLng.lat();
var lang = e.latLng.lng();
$('#latitude').val(lat);
$('#longitude').val(lang);
});
// google.maps.event.addDomListener(window, 'load', initialize);
}
Janardhanam Julapalli 11-Apr-19 3:47am    
if (navigator.geolocation) {
console.log('in');//this is printing
navigator.geolocation.getCurrentPosition(function(position) {
console.log('in in');//this is not printing

This is the actual problem. Earlier I read that getCurrentPosition() will not work with http. Now I have https but still not working.
Nirav Prabtani 11-Apr-19 4:27am    
Make sure you allowed current location for browser, It seems you denied the same.

If not, then can you please see the console by inspecting an element ?
Janardhanam Julapalli 11-Apr-19 5:14am    
Its working in chrome. Thank You so much.
Janardhanam Julapalli 11-Apr-19 5:05am    
I have given permission. Also nothing in console apart from the log messages I gave. Browser related problem? Using safari now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900