JavaScript 地理定位和谷歌地图

发布于 2024-08-31 15:53:52 字数 2872 浏览 2 评论 0原文

我希望有人能帮助我找到一个简单的答案,但我就是无法解决。

我正在尝试捕获地理位置纬度和经度并将其放入谷歌地图 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);

并将其分配给两个变量,myLatmyLong。 然后我希望能够将我的原始脚本从 更改

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间☆小暴躁 2024-09-07 15:53:52

可能导致问题的原因之一是您尝试传递 myLat 和 myLong 变量的方式。

传递给 origin 的值需要是 LatLng 对象。

var myLat = "12.345";
var myLng = "54.321";
var myLatLng = new google.maps.LatLng(myLat, myLng);

var request = {
    origing: myLatLng,
    desitination...
    etc.
}

最大的问题是,为了传递值,您需要将其格式化为这样:

var request = {
    something: value,
    somethingElse: anotherValue,
    evenMore: bigValue
}

注意每行后面的逗号。这会破坏您的示例,因为您将其放置在原始值中,从而混淆了属性。

我会继续关注它,但希望这会有所帮助。

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.

var myLat = "12.345";
var myLng = "54.321";
var myLatLng = new google.maps.LatLng(myLat, myLng);

var request = {
    origing: myLatLng,
    desitination...
    etc.
}

The biggest problem being that in order to pass the values, you need it to be formatted as such:

var request = {
    something: value,
    somethingElse: anotherValue,
    evenMore: bigValue
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文