Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 商店定位器-从邮政编码到所选商店的方向_Javascript_Google Maps_Google Maps Api 3_Geolocation - Fatal编程技术网

Javascript 商店定位器-从邮政编码到所选商店的方向

Javascript 商店定位器-从邮政编码到所选商店的方向,javascript,google-maps,google-maps-api-3,geolocation,Javascript,Google Maps,Google Maps Api 3,Geolocation,我有一个JavaScript邮政编码搜索,根据输入内容搜索英国的3家壁橱商店。目前,它发现3家商店都很好 首先,我想在输入中输入的邮政编码处放置一个标记 第二,当这三个结果出现时,它们被标记在地图上。我想有一个链接称为方向,一旦点击,将显示从开始到所选商店的方向 我已经尝试了以下代码,但它不起作用…但是它确实从输入和方向链接中获取邮政编码数据,并在控制台中显示它们。我是否需要将它们转换为long和lat才能工作 function calcRoute() { var start = doc

我有一个JavaScript邮政编码搜索,根据输入内容搜索英国的3家壁橱商店。目前,它发现3家商店都很好

首先,我想在输入中输入的邮政编码处放置一个标记

第二,当这三个结果出现时,它们被标记在地图上。我想有一个链接称为方向,一旦点击,将显示从开始到所选商店的方向

我已经尝试了以下代码,但它不起作用…但是它确实从输入和方向链接中获取邮政编码数据,并在控制台中显示它们。我是否需要将它们转换为long和lat才能工作

function calcRoute() {
    var start = document.getElementById('address').value;
    var end = document.getElementById('get-directions').name;
    //console.log(start, end)
    var request = {
        origin:start,
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });
  }
我有这个代码作为我的开始标记,但这似乎也不起作用

function initialize() {
    var start_marker = new google.maps.LatLng(document.getElementById('address').value);
    directionsDisplay = new google.maps.DirectionsRenderer();
    var mapOptions = {
      zoom:7,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: start_marker
    }
    marker = new google.maps.Marker({
          map:map,
          draggable:false,
          animation: google.maps.Animation.DROP,
          position: start_marker,
        });
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
    directionsDisplay.setMap(map);
  }
这部分从邮政编码中获取long/lat数据

this.geocode = function(address, callbackFunction) {
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      var result = {};
      result.latitude = results[0].geometry.location.lat();
      result.longitude = results[0].geometry.location.lng();
      callbackFunction(result);
      //console.log(result);
        //console.log("Geocoding " + geometry.location + " OK");
        addMarker(map, results[0].geometry.location);
    } else {
      alert("Geocode was not successful for the following reason: " + status);
      callbackFunction(null);
    }
  });
addMarker的功能如下:

function addMarker(map, location) {

console.log("Setting marker for (location: " + location + ")");
marker = new google.maps.Marker({
map : map,
animation: google.maps.Animation.DROP,
position : location
});
}

任何帮助都将不胜感激

的构造函数,而不是字符串:

var start_marker = new google.maps.LatLng(document.getElementById('address').value);

如果你只有一个地址,如果你想在地图上显示一个标记,你需要使用。啊,太好了,谢谢。但由于某些原因,标记不会添加。我有地理位置数据,但它没有;我一点也不想把它加到地图上。这个代码正确吗?marker=new google.maps.marker({map:map,animation:google.maps.animation.DROP,position:location});该代码是否正确取决于“位置”是什么。两件事:1。评论中张贴的代码很难阅读,您应该编辑您的问题,将其包括在内,并以合理的方式设置格式。2.地理编码器是异步的,如果“位置”来自地理编码器,您是否在回调函数中使用它?是的位置来自地理编码器,是的,我在回调函数中使用它。当我输入console.log(location)时,它显示54.9861334,-1.53794489999568,这是我在输入中输入的邮政编码上正确的long/lat。是的,我现在就去