2016-10-26 9 views
-1

私はこのコードを持っている:Google map Geocoder新しいgoogle.maps.LatLngで値を返す方法は?

私は値原点を返すことができますどのように

function getLatLong(address) { 
 
     var geocoder = new google.maps.Geocoder(); 
 
     var result = ""; 
 
     geocoder.geocode({ 'address': address }, function(results, status) { 
 
      if (status == google.maps.GeocoderStatus.OK) { 
 
       result[lat] = results[0].geometry.location.Pa; 
 
       result[lng] = results[0].geometry.location.Qa; 
 
      } else { 
 
       result = "Unable to find address: " + status; 
 
      } 
 
      storeResult(result); 
 
      }); 
 
     } 
 

 

 

 

 

 
     directionsDisplay = new google.maps.DirectionsRenderer(); 
 
     var directionsService = new google.maps.DirectionsService(); 
 

 
     var request = { 
 
      origin: new google.maps.LatLng(getLatLong()[0],getLatLong()[1]), 
 
      destination: new google.maps.LatLng(59.79530896374892,30.410317182540894), 
 
      travelMode: google.maps.DirectionsTravelMode.DRIVING/
 
     }; 
 

 
     directionsService.route(request, function(response, status) { 
 
      if (status == google.maps.DirectionsStatus.OK) { 
 
       directionsDisplay.setDirections(response); 
 
      } 
 
     }); 
 

 
     map = new google.maps.Map(document.getElementById("map")); 
 
     directionsDisplay.setMap(map);

:新しいgoogle.maps.LatLng()?問題のコールバック、どのようにグローバル変数に値を保存できますか?それを修正してください。私には考えがありません。

+0

'location.Pa'は、' location.Qa'が安定して変数名ではありません、彼らは閉鎖コンパイラによって生成され、およびAPIのリリースごとに変更できますかすることができます。 – geocodezip

答えて

0

ジオコーダは非同期なので、コールバック関数から値を返すことはできません。コールバック関数で/が利用可能なときにそれらを使用する必要があります。

  • 参照:

    function getLatLong(address) { 
        var geocoder = new google.maps.Geocoder(); 
        geocoder.geocode({ 
        'address': address 
        }, function(results, status) { 
        if (status == google.maps.GeocoderStatus.OK) { 
         var request = { 
         origin: results[0].geometry.location, 
         destination: new google.maps.LatLng(59.79530896374892, 30.410317182540894), 
         travelMode: google.maps.DirectionsTravelMode.DRIVING 
         }; 
    
         directionsService.route(request, function(response, status) { 
         if (status == google.maps.DirectionsStatus.OK) { 
          directionsDisplay.setDirections(response); 
         } else { 
          alert("directions failed:" + status) 
         } 
         }); 
        } else { 
         result = "Unable to find address: " + status; 
        } 
        }); 
    } 
    

    proof of concept fiddle

    コードスニペット:

    How do I return the response from an asynchronous call?

一つの選択肢、ジオコーダのコールバック関数の指示のサービス呼び出しを行います

var geocoder; 
 
var map; 
 
var directionsDisplay; 
 
var directionsService; 
 

 
function initialize() { 
 
    map = new google.maps.Map(
 
    document.getElementById("map"), { 
 
     center: new google.maps.LatLng(37.4419, -122.1419), 
 
     zoom: 13, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 
    directionsDisplay = new google.maps.DirectionsRenderer(); 
 
    directionsService = new google.maps.DirectionsService(); 
 
    map = new google.maps.Map(document.getElementById("map")); 
 
    directionsDisplay.setMap(map); 
 
    getLatLong("St. Petersburg, Russia"); 
 

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

 
function getLatLong(address) { 
 
    var geocoder = new google.maps.Geocoder(); 
 
    geocoder.geocode({ 
 
    'address': address 
 
    }, function(results, status) { 
 
    if (status == google.maps.GeocoderStatus.OK) { 
 
     var request = { 
 
     origin: results[0].geometry.location, 
 
     destination: new google.maps.LatLng(59.79530896374892, 30.410317182540894), 
 
     travelMode: google.maps.DirectionsTravelMode.DRIVING 
 
     }; 
 

 
     directionsService.route(request, function(response, status) { 
 
     if (status == google.maps.DirectionsStatus.OK) { 
 
      directionsDisplay.setDirections(response); 
 
     } else { 
 
      alert("directions failed:" + status) 
 
     } 
 
     }); 
 
    } else { 
 
     result = "Unable to find address: " + status; 
 
    } 
 
    }); 
 
}
html, 
 
body, 
 
#map { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<div id="map"></div>

+0

私は2つの住所をどのようにすることができますか?原点:results [0] .geometry.location、 宛先:結果[1] .geometry.location? – BlogSER

関連する問題