Javascript 谷歌地图API v3的问题

Javascript 谷歌地图API v3的问题,javascript,jquery,html,google-maps,google-maps-api-3,Javascript,Jquery,Html,Google Maps,Google Maps Api 3,我试图通过Google Maps API实现以下目标: 在地图上显示至\u地址作为标记 获取用户位置 根据gps/用户输入提供的信息生成和显示方向 我已经让最后两个好好工作了。我现在遇到的问题是,如果没有提供位置,则在地图上显示收件人地址作为标记 这是我正在使用的代码。请记住最后两个步骤按预期工作。我知道我可以用latlng来完成这项工作,但这不是我的选择。我需要提供一个地址 var geocoder; var directionDisplay; var directionsService

我试图通过Google Maps API实现以下目标:

  • 在地图上显示
    至\u地址
    作为标记
  • 获取用户位置
  • 根据gps/用户输入提供的信息生成和显示方向
我已经让最后两个好好工作了。我现在遇到的问题是,如果没有提供位置,则在地图上显示
收件人地址作为标记

这是我正在使用的代码。请记住最后两个步骤按预期工作。我知道我可以用latlng来完成这项工作,但这不是我的选择。我需要提供一个地址

var geocoder;
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var to_pos;
var to_address = '11161 84th ave delta bc';

function initialize() {

    directionsDisplay = new google.maps.DirectionsRenderer();


    geocoder = new google.maps.Geocoder();

    geocoder.geocode({
        'address': to_address
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            to_pos = results[0].geometry.location;
        }
    });

    var myOptions = {
        zoom: 7,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: to_pos
    };
    var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('directions'));

    var control = document.getElementById('d_options');

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

            var infowindow = new google.maps.Marker({
                position: pos,
                map: map,
                title: "You"
            });

            map.setCenter(pos);
            $('#from').val(pos);
            $('#d_options').trigger('collapse');
            calcRoute();

        }, function () {
            handleNoGeolocation(true);
        });
    }
}

function calcRoute() {
    var start = document.getElementById('from').value;
    var end = to_address;
    $('#results').show();
    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);
        }
    });
}


google.maps.event.addDomListener(window, 'load', initialize);

地理编码使用异步请求。必须在geocode()的回调中创建标记

geocoder.geocode({
        'address': to_address
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var to_pos = results[0].geometry.location;

            new google.maps.Marker({
                position: new google.maps.LatLng(to_pos.lat(),to_pos.lng()),
                map: map,
                title: "Destination"
            });
        }
    });