Javascript 谷歌地图地理定位。地理定位提供地图,但不提供方向
因此,我制作了这段代码,它将普通地址转换为地理位置,并从您的gps位置(phonegap位置、html5位置或其他位置)返回地图和方向。此函数在phonegap或html5位置成功时调用 此代码在浏览器中工作正常,但在以下情况下失败: 错误:TypeError:“未定义”不是中的对象file://bla 第761行的bla 第761行=geocoder.geocode({'address':address},函数(结果,状态){ 所以我检查了我所有的变量,它们并不像在线版本那样是空的,返回div也存在。所以我猜Google的geolocator链接有问题 有趣的是,它显示的是地图,但不是方向。它标记了你需要去的地方,但不是你在哪里。就好像只是方向没有加载或什么的Javascript 谷歌地图地理定位。地理定位提供地图,但不提供方向,javascript,google-maps-api-3,geolocation,Javascript,Google Maps Api 3,Geolocation,因此,我制作了这段代码,它将普通地址转换为地理位置,并从您的gps位置(phonegap位置、html5位置或其他位置)返回地图和方向。此函数在phonegap或html5位置成功时调用 此代码在浏览器中工作正常,但在以下情况下失败: 错误:TypeError:“未定义”不是中的对象file://bla 第761行的bla 第761行=geocoder.geocode({'address':address},函数(结果,状态){ 所以我检查了我所有的变量,它们并不像在线版本那样是空的,返回div
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
function bring_me_back(call_back_map,call_back_directions,address,position) {
var latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); // your location
directionsDisplay = new google.maps.DirectionsRenderer();
var myOptions = {
zoom: 14,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: true
};
var map = new google.maps.Map(document.getElementById(call_back_map),myOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById(call_back_directions));
var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"My location"
});
// find the address
var geocoder = new google.maps.Geocoder (map);
geocoder.geocode ( { 'address': address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var end = results [0].geometry.location;
map.setCenter(results [0].geometry.location);
marker.setPosition(results [0].geometry.location);
var start = latlng; // your gps location
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.WALKING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else alert("Directions was not successful for the following reason: " + status);
});
}
else {
$('#'+call_back).html("Geocode was not successful for the following reason: " + status);
}
});
}
我调用google的方式(在我使用此代码之前,它有足够的时间加载和“初始化”被调用)
}
API的反馈非常有限,因此我无法再多做任何事情。经过大量搜索后,我发现了一个变量:
var directionsService = new google.maps.DirectionsService();
在加载google脚本之前被调用。将其更改为
var directionsService;
并呼吁:
directionsService = new google.maps.DirectionsService();
从函数内部
谢谢您的帮助。您的geocoder对象为空吗?geocoder返回{},似乎是正确的。geocoder.geocode返回函数(a,b){S(Ke,函数(c){c.geocode(a,b)}
directionsService = new google.maps.DirectionsService();