谷歌地图地理编码器如何我可以返回值在新的Google .maps. latlng
Google map Geocoder how I can return values in new 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);
我如何返回值origin: new google.maps.LatLng() ?问题回调,我如何保存值在全局变量?请帮忙修理。我不知道。
地理编码器是异步的,您不能从回调函数返回值,您需要在可用的时间/地点(在回调函数中)使用它们。
- reference:如何从异步调用返回响应?
一个选项,在Geocoder的回调函数中调用方向服务:
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;
}
});
}
概念验证
代码片段:
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>
相关文章:
- 如何在Google柱状图中动态添加行/列
- Google/html5语音识别JavaScript SDK Chrome网络工具包SpeechRecognition
- 将 jquery .position() 转换为 google maps latlng
- API Google将Javascript地址映射到LatLng
- Google Map API use LatLng or Address
- Google Maps APIv3 large polygon new google.maps.LatLng redun
- GWT Google Maps:setCenter:不是LatLng或LatLngLiteral:属性lat:不是数字
- 检查这是否是JavaScript中new google.maps.LatLng的实例
- 将值传递给google.maps.LatLng
- 如何使用变量来定义新的google.maps.LatLng()
- 不能使用google.maps.LatLng对象's late
- 谷歌地图地理编码器如何我可以返回值在新的Google .maps. latlng
- Google Maps JavaScript将变量放入LatLng()中
- 在Google Maps API V3中使用LatLng坐标数组绘制折线
- 如何字符串化google LatLng对象
- 将JSON字符串值分配给MVC 4中的google.maps.LatLng(lat,lon)中的Latitude和Lon
- 在Google API上使用LatLng获取街景图像
- Convert "[52.43242, 4.43242]" to google LatLng
- 谷歌地图API v3-类型错误:表达式的结果'google.maps.LatLng'[defined]不
- TypeError: google.maps.latLng不是构造函数