本文介绍了如何在javascript中获取当前的lat和lang?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< input type =buttondata-toggle =modaldata-target =#myModalonclick =getGeoLocation(); class =form-button form-button-submit btn btn-theme btn-theme-darkvalue =获取位置/>
function getGeoLocation(){
var map;
var latitude = 13.0547712; //我需要用当前位置替换它lat
var longitude = 80.2578432; //我需要用当前位置替换它lang
var myLatlng = new google.maps.LatLng(纬度,经度);
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:点击查看信息!
});
marker.setMap(map);
//标记ENDS
google.maps.event.addListener(地图,点击,函数(e){
// lat和lng在e对象$中可用b $ b 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);
}
我的尝试:
< pre> navigator.geolocation.getCurrentPosition(function(position){
var geolocate = new google.maps.LatLng(position.coords.latitude ,position.coords.longitude);
console.log(position.coords.latitude);
});
getCurrentPosition
没有给我lat和lang
解决方案
<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
解决方案
这篇关于如何在javascript中获取当前的lat和lang?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!