2013-05-09 7 views
0

因此,我製作了這段代碼,將普通地址轉換爲地理位置,並從您的GPS位置(手機位置,html5位置或其他位置)返回地圖和路線經緯度)。在phonegap或html5位置成功時調用此函數。Google maps geolocation.geolocate給出了地圖,但沒有指示

此代碼工作正常在瀏覽器中,但無法用: 錯誤:類型錯誤:「未定義」是不是在文件中的對象://血乳酸血乳酸在761

線761 = geocoder.geocode({ '地址':地址},功能(結果,狀態){

所以我已經檢查了我的所有變量,它們不是空的就像在線版本和返回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); 
    } 
}); 
} 

我叫谷歌(它有足夠的時間來加載和「初始化」被調用的方法之前,我使用此代碼)的方式。

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的反饋方式有限,所以我什麼都做不了更多。

+0

是您的geocoder對象null嗎? – Mehmet 2013-05-09 22:18:21

+0

Geocoder返回{},看起來正確。並且geocoder.geocode返回 函數(a,b){S(Ke,function(c){c.geocode(a,b)});} – Matt 2013-05-10 06:02:54

回答

0

搜索的地獄後,我發現了一個變量:

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

之所以叫加載谷歌腳本之前。它改成

var directionsService; 

喊着:

directionsService = new google.maps.DirectionsService(); 

從裏面的功能。

感謝您的幫助。

相關問題