そこで、通常の住所を地理位置情報に変換し、GPS 位置 (phonegap 位置、html5 位置、または緯度経度) から地図と道順を返すこのコードを作成しました。この関数は、phonegap または html5 の場所の成功時に呼び出されます。
このコードはブラウザーでは正常に動作しますが、エラー: TypeError: 'undefined' is not an object in file://bla bla at line 761 で失敗します
761 行目 = geocoder.geocode ( { 'address': address }, function(results, status) {
だから私はすべての変数をチェックしましたが、オンライン版と同じように空ではなく、戻り値の div が存在します。そのため、Google へのジオロケータ リンクに何か問題があると推測しています。
面白いことに、地図は表示されますが、道順は表示されません。そして、それはあなたが行く必要がある場所を示しますが、あなたがいる場所ではありません. 道順だけが読み込まれないかのようです。
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を呼び出す方法(このコードを使用する前に、ロードして「初期化」が呼び出されるのに十分な時間があります)。
function loadGoogleScript() {
if(!(navigator.network.connection.type == Connection.NONE)) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://maps.google.com/maps/api/js?v=3.6&sensor=false&callback=initialize';
document.body.appendChild(script);
return true;
}
else {
return false;
}
}
API からのフィードバックは非常に限られているため、それ以上のことはできません。