Javascript 将谷歌地图设置为当前位置
我的html页面中有一个谷歌地图,地图上面有两个输入字段,当你点击地图时,它会返回这些字段中的lat和LAG,我想在地图中设置我的当前位置,无论谁打开地图,它都会确定他当前的位置并显示在地图中,我尝试了地理位置,但我的代码无法使用,这是我的密码:Javascript 将谷歌地图设置为当前位置,javascript,google-maps,geolocation,location,Javascript,Google Maps,Geolocation,Location,我的html页面中有一个谷歌地图,地图上面有两个输入字段,当你点击地图时,它会返回这些字段中的lat和LAG,我想在地图中设置我的当前位置,无论谁打开地图,它都会确定他当前的位置并显示在地图中,我尝试了地理位置,但我的代码无法使用,这是我的密码: google.maps.event.addListener(映射,'click',函数(事件){ 警报(事件警报); }); #地图{ 宽度:100%; 高度:250px; } 纬度 经度 函数myMap(){ 变量映射选项={ 中心:新googl
google.maps.event.addListener(映射,'click',函数(事件){
警报(事件警报);
});代码>
#地图{
宽度:100%;
高度:250px;
}
纬度
经度
函数myMap(){
变量映射选项={
中心:新google.maps.LatLng(51.5,-0.12),
缩放:10,
mapTypeId:google.maps.mapTypeId.HYBRID
}
var map=new google.maps.map(document.getElementById(“map”)、mapOptions);
google.maps.event.addListener(映射,'click',函数(事件){
var mylatng=event.latLng;
var lat=mylatng.lat();
var lng=myLatLng.lng();
document.getElementById(“lat”).value=lat;
document.getElementById(“lang”).value=lng;
});
}
您可以使用navigator.geolocation.getCurrentPosition()
这是我差点就把它删掉了。我知道这并不完全是您想要的,但它可能会帮助您操作当前代码
下面是一个带有一个输入字段的工作示例,其中long/lat可以用逗号分隔
<html>
<head>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script language="javascript" type="text/javascript">
var map;
var geocoder;
function InitializeMap() {
var latlng = new google.maps.LatLng(39.949430, -75.171984);
var myOptions =
{
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
};
map = new google.maps.Map(document.getElementById("map"), myOptions);
}
function FindLocaiton() {
geocoder = new google.maps.Geocoder();
InitializeMap();
var address = document.getElementById("addressinput").value;
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
function Button1_onclick() {
FindLocaiton();
}
window.onload = InitializeMap;
</script>
</head>
<body>
<h2>Gecoding Demo JavaScript: </h2>
<table>
<tr>
<td>
<input id="addressinput" type="text" style="width: 447px" />
</td>
<td>
<input id="Button1" type="button" value="Find" onclick="return Button1_onclick()" /></td>
</tr>
<tr>
<td colspan ="2">
<div id ="map" style="height: 253px" >
</div>
</td>
</tr>
</table>
</body>
</html>
var映射;
var地理编码器;
函数初始化映射(){
var latlng=新的google.maps.latlng(39.949430,-75.171984);
变异性肌肽=
{
缩放:8,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.ROADMAP,
disableDefaultUI:true
};
map=新的google.maps.map(document.getElementById(“map”),myOptions);
}
函数FindLocaiton(){
geocoder=新的google.maps.geocoder();
初始化映射();
var address=document.getElementById(“addressinput”).value;
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
map.setCenter(结果[0].geometry.location);
var marker=new google.maps.marker({
地图:地图,
位置:结果[0]。几何体。位置
});
}
否则{
警报(“地理编码因以下原因未成功:“+状态”);
}
});
}
功能按钮1_onclick(){
FindLocaiton();
}
window.onload=初始化映射;
Gecoding演示JavaScript:
代码开放示例
您在地理定位方面做了哪些尝试?你说的“你的代码不起作用”是什么意思?请提供一份说明问题的报告。