2013-02-15 126 views
1

我想谷歌地圖API V2轉換成V3,但我不能讓它開始工作,我現在已經嘗試了3天,它不工作:-(從V2升級谷歌,地圖API V3

如果有人能告訴我如何正確地做到這一點,我會非常感激......

V2代碼

<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA4vJ90IKcF0qHxC2Cg9NjchT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQ-rtrvY-Stu0L_q-OypfOv3WnUfg" 
type="text/javascript"></script> 

<script type="text/javascript"> 

var geocoder, location1, location2, gDir, getCityName, getcITIESName, getDeliveryCityName, PickState, DeliveryState, SteState, SteStateB2, theAddress1, memeaddress1, memeaddress2; 

function initialize() { 

geocoder = new GClientGeocoder(); 
gDir = new GDirections(); 
GEvent.addListener(gDir, "load", function() { 
var drivingDistanceMiles = (gDir.getDistance().meters/1609.344).toFixed(0);//for 0 decial place 
var drivingDistanceKilometers = gDir.getDistance().meters/1000; 
//theAddress1 = location1.address 
getCityName = location1.address.split(",") 
memeaddress1 = getCityName[0] + ',' + getCityName[1]; 

getcITIESName = location2.address.split(","); 
memeaddress2 = getcITIESName[0] + ',' + getcITIESName[1].substr(0,3); 
SteState = document.getElementById('selFromState').options[document.getElementById('selFromState').options.selectedIndex].value; 
SteStateB2 = document.getElementById('selToState').options[document.getElementById('selToState').options.selectedIndex].value; 
if (document.getElementById("inputString").value + ', ' + SteState == memeaddress1) 
{ 
} 
else 
{ 
    doItagain=confirm('Cannot find the Pickup City!\n\nDid you mean: ' + location1.address + ' ?' + '\n\nPress OK to Change\nCancel to keep the City and State you entered!\n\n\n'); 
    if(doItagain){ 
getCityName[1] = (getCityName[1]).replace(/^\s*|\s*$/g,''); 
var showonlythefirst2ltrspickup = getCityName[1].substr(0,2) 
document.getElementById("inputString").value = getCityName[0] 
document.getElementById("selFromState").value = showonlythefirst2ltrspickup; 
showLocation() 
    } 

    else 

// Just end and exit function 
window.alert('Please Note you must enter the miles manually!') 
document.getElementById("txtMiles").value = "0"; 
//} 
} 
if (document.getElementById("inputString2").value + ', ' + SteStateB2 == memeaddress2) 
{ 
document.getElementById("txtMiles").value = drivingDistanceMiles; 
checkemptyB2(); 
} 
else 
{ 
    doIt=confirm('Cannot find the Delivery City!\n\nDid you mean: ' + location2.address + ' ?' + '\n\nPress OK to Change\nCancel to keep the City and State you entered!\n\n\n'); 
    if(doIt){ 
getcITIESName[1] = (getcITIESName[1]).replace(/^\s*|\s*$/g,''); 
var showonlythefirst2ltrs = getcITIESName[1].substr(0,2) 
document.getElementById("inputString2").value = getcITIESName[0] 
document.getElementById("selToState").value = showonlythefirst2ltrs; 

showLocation() 
    } 

    else 

// Just end and exit function 
window.alert('Please Note you must enter the miles manually!') 
document.getElementById("txtMiles").value = "0"; 
} 
}); 
} 

function showLocation() { 
checkemptyB2(); 
//geocoder = new GClientGeocoder(); 
//gDir = new GDirections(); 
//alert("here is the pickup address:" + document.getElementById("inputString2").value + ',' + document.getElementById("selToState").value) 
geocoder.getLocations(document.getElementById("inputString").value + ',' + document.getElementById("selFromState").value, function (response) { 
if (!response || response.Status.code != 200) 
{ 
alert("Sorry, we were unable to geocode the pickup address"); 
} 
else 
{ 
location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address}; 
geocoder.getLocations(document.getElementById("inputString2").value + ',' + document.getElementById("selToState").value, function (response) { 
if (!response || response.Status.code != 200) 
{ 
alert("Sorry, we were unable to geocode the delivery address"); 
} 
else 
{ 
location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: 

response.Placemark[0].address}; 
gDir.load('from: ' + location1.address + ' to: ' + location2.address); 
} 
}); 
} 
}); 
} 
</script> 
<script type="text/javascript"> 

function changedelivery() { 
    doIt=confirm('do you wish to proceed?'); 
    if(doIt){ 
document.getElementById("inputString2").value = getcITIESName[0] 
document.getElementById("selToState").value = SteStateB2 
    } 
    else{ 
// Just end and exit function 
    } 
} 
</script> 

這裏是合德,我試圖:

<script type="text/javascript"> 

var geocoder,location1,location2,gDir,getCityName,getcITIESName,getDeliveryCityName,PickState,DeliveryState,SteState,SteStateB2,theAddress1,memeaddress1,memeaddress2; 

function initialize() { 

geocoder = new google.maps.Geocoder(); 
gDir = new google.maps.DirectionsService(); 
google.maps.Event.addListener(gDir, "load", function() { 
var drivingDistanceMiles = (gDir.getDistance().meters/1609.344).toFixed(0);//for 0 decial place 
var drivingDistanceKilometers = gDir.getDistance().meters/1000; 

getCityName = location1.address.split(",") 
memeaddress1 = getCityName[0] + ',' + getCityName[1]; 

getcITIESName = location2.address.split(","); 
memeaddress2 = getcITIESName[0] + ',' + getcITIESName[1].substr(0,3); 

SteState = document.getElementById('selFromState').options[document.getElementById('selFromState').options.selectedIndex].value; 
SteStateB2 = document.getElementById('selToState').options[document.getElementById('selToState').options.selectedIndex].value; 


if (document.getElementById("inputString").value + ', ' + SteState == memeaddress1) 
{ 
} 
else 
{ 
    doItagain=confirm('Cannot find the Pickup City!\n\nDid you mean: ' + location1.address + ' ?' + '\n\nPress OK to Change\nCancel to keep the City and State you entered!\n\n\n'); 

if(doItagain){ 
getCityName[1] = (getCityName[1]).replace(/^\s*|\s*$/g,''); 
var showonlythefirst2ltrspickup = getCityName[1].substr(0,2) 
document.getElementById("inputString").value = getCityName[0] 
document.getElementById("selFromState").value = showonlythefirst2ltrspickup; 
showLocation() 
    } 

else 

// Just end and exit function 
window.alert('Please Note you must enter the miles manually!') 
document.getElementById("txtMiles").value = "0"; 
//} 
} 

if (document.getElementById("inputString2").value + ', ' + SteStateB2 == memeaddress2) 
{ 
document.getElementById("txtMiles").value = drivingDistanceMiles; 
checkemptyB2(); 
} 

else 

{ 
    doIt=confirm('Cannot find the Delivery City!\n\nDid you mean: ' + location2.address + ' ?' + '\n\nPress OK to Change\nCancel to keep the City and State you entered!\n\n\n'); 

if(doIt){ 
getcITIESName[1] = (getcITIESName[1]).replace(/^\s*|\s*$/g,''); 
var showonlythefirst2ltrs = getcITIESName[1].substr(0,2) 
document.getElementById("inputString2").value = getcITIESName[0] 
document.getElementById("selToState").value = showonlythefirst2ltrs; 

showLocation() 
    } 

else 

// Just end and exit function 
window.alert('Please Note you must enter the miles manually!') 
document.getElementById("txtMiles").value = "0"; 
} 
}); 
} 

function showLocation() { 
checkemptyB2(); 
geocoder.geocode(document.getElementById("inputString").value + ',' + document.getElementById("selFromState").value, function (response) { 


if (!response || response.Status.code != 200) 
{ 
alert("Sorry, we were unable to geocode the pickup address"); 
} 

else 

{ 
location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address}; 
geocoder.geocode(document.getElementById("inputString2").value + ',' + document.getElementById("selToState").value, function (response) { 
if (!response || response.Status.code != 200) 
{ 
alert("Sorry, we were unable to geocode the delivery address"); 
} 

else 

{ 
location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: 

response.Placemark[0].address}; 
gDir.load('from: ' + location1.address + ' to: ' + location2.address); 
} 
}); 
} 
}); 
} 
</script> 
<script type="text/javascript"> 

function changedelivery() { 
    doIt=confirm('do you wish to proceed?'); 

if(doIt){ 
document.getElementById("inputString2").value = getcITIESName[0] 
document.getElementById("selToState").value = SteStateB2 
} 

else 

{ 
// Just end and exit function 
} 
} 
</script> 

再次感謝任何幫助!

+0

三天後你到底得到了哪些代碼?你爲什麼不把它發佈給我們 – 2013-02-15 17:49:24

+0

@AnthonyHatzopoulos我用它作爲指南http://gabrielduque.wordpress.com/2011/01/13/upgrading-from-google-maps-api-v2-to-v3/ – compcobalt 2013-02-15 18:04:12

+0

@AnthonyHatzopoulos我會更新這個問題,我將在V2工作代碼下面添加代碼與V3非工作代碼。謝謝 – compcobalt 2013-02-15 18:05:25

回答

2

任何以「G」開頭的是v2類。您需要替換:

與他們的v3等效。它們不同,請參閱文檔以獲取詳細信息。

+0

我也做了這些改變,但我仍然在線geocoder.getLocations(document.getElementById(「inputString」)。value +','+ document.getElementById(「selFromState」)。value,function(response ){//錯誤是:geocoder.getLocations不是函數 來源,我更新了迄今爲止我嘗試過的代碼,如果你可以請看看它,看看我做錯了請謝謝。 – compcobalt 2013-02-18 15:24:14

+0

但是你沒有閱讀文檔v3 Geocoder沒有getLocations方法 – geocodezip 2013-02-18 16:22:33

+0

說:在v3中,它使用geocoder.geocode()而不是GClientGeocoder.getLocation()我也改變了它(見上面的代碼),但它仍然給我一個錯誤,我想我對這個問題很滿意,我會相信你的答案,並且更好地理解這個問題,非常感謝你的幫助, – compcobalt 2013-02-18 17:30:09