JavaScript 地理定位和谷歌地图
我希望有人能帮助我找到一个简单的答案,但我就是无法解决。
我正在尝试捕获地理位置纬度和经度并将其放入谷歌地图 api
到目前为止,我已经
var myOptions = {
zoom:7,
trips:1,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map"), myOptions);
directionsDisplay.setMap(map);
var request = {
origin: 'newyork'
destination: 'deleware',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
可以很好地满足我的需求。但是我希望能够使用 lat long 将 origin
更改为用户 以下脚本来自 google.maps.api。
他们的代码是:
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
var placeMarker = new google.maps.Marker({
position: initialLocation,
map: map,
});
map.setCenter(initialLocation);
}, function() {
handleNoGeolocation(browserSupportFlag);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation();
}
function handleNoGeolocation() {
initialLocation = newyork;
map.setCenter(initialLocation);
}
我想取出
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
并将其分配给两个变量,myLat
和 myLong
。 然后我希望能够将我的原始脚本从 更改
var request = {
origin: 'newyork'
destination: 'deleware',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
为
var request = {
origin: myLat,myLong
destination: 'deleware',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
这有意义吗..?
我目前有一头猪,因为我不是 JS 开发人员,我认为这应该是一个简单的编码,但我正在输掉这场战斗。..
有什么想法..?
为道格拉斯的及时回复干杯。
我从之前提出的问题中找到了这个示例网站,它提供了一些线索。站点是 http://npdoty.name/location/
他的示例如下:
navigator.geolocation.getCurrentPosition(function(position){
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var marker = new GMarker(new GLatLng(lat, lon));
var jsMap = new GMap2(document.getElementById("jsMap"));
jsMap.addOverlay(marker);
},function(error){
//use error.code to determine what went wrong
});
我可以突破这个例子并做类似的事情:
var request = {
navigator.geolocation.getCurrentPosition(function(position){
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var myOrigin = new Gorigin(new GLatLng(lat, lon));
origin: myOrigin,
destination: 'deleware',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
非常感谢
I'm hoping that somebody can help me out on what I feel is an easy answer but I just can't get it to work out.
I'm trying to trap the geolocation lat and long and place it into the google maps api
So far I have
var myOptions = {
zoom:7,
trips:1,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map"), myOptions);
directionsDisplay.setMap(map);
var request = {
origin: 'newyork'
destination: 'deleware',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
This works fine for what I want. However I want to be able to change origin
to the users lat long using
the following script from google.maps.api.
their code is:
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
var placeMarker = new google.maps.Marker({
position: initialLocation,
map: map,
});
map.setCenter(initialLocation);
}, function() {
handleNoGeolocation(browserSupportFlag);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation();
}
function handleNoGeolocation() {
initialLocation = newyork;
map.setCenter(initialLocation);
}
I want to pull out the
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
and allocate it to two 2 variables, myLat
and myLong
.
I then want to be able to change my orignal script from
var request = {
origin: 'newyork'
destination: 'deleware',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
to
var request = {
origin: myLat,myLong
destination: 'deleware',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
Does this make sense..?
I'm currently having a pig with it and as I'm not a JS developer it's what I think should be a simple bit of coding that I'm losing the battle with..
Any thoughts..?
cheers douglas for the prompt reply.
I have found this example website from a previous question asked which sheds a bit of light. site is http://npdoty.name/location/
His example goes as follows:
navigator.geolocation.getCurrentPosition(function(position){
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var marker = new GMarker(new GLatLng(lat, lon));
var jsMap = new GMap2(document.getElementById("jsMap"));
jsMap.addOverlay(marker);
},function(error){
//use error.code to determine what went wrong
});
Can I break out of this example and do something similar to:
var request = {
navigator.geolocation.getCurrentPosition(function(position){
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var myOrigin = new Gorigin(new GLatLng(lat, lon));
origin: myOrigin,
destination: 'deleware',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能导致问题的原因之一是您尝试传递 myLat 和 myLong 变量的方式。
传递给 origin 的值需要是 LatLng 对象。
即
最大的问题是,为了传递值,您需要将其格式化为这样:
注意每行后面的逗号。这会破坏您的示例,因为您将其放置在原始值中,从而混淆了属性。
我会继续关注它,但希望这会有所帮助。
One of the things that may be causing a problem is the way you're trying to pass the myLat and myLong variables.
The value passed to origin needs to be a LatLng object.
i.e.
The biggest problem being that in order to pass the values, you need it to be formatted as such:
Note the commas after each line. This breaks in your example because of where you're placed yours in the origin value, thus confusing the property.
I'll keep looking at it, but hopefully this helps.