我正在尝试将Google Maps API V2转换为V3,但我无法让它工作,我现在已经尝试了3天而且它无法正常工作: - (
如果有人可以告诉我如何正确地做到这一点,我会 非常 感谢...
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 :(得分:2)
任何以“G”开头的东西都是v2类。您需要替换:
与他们的v3等价物。它们不同,请参阅文档以获取详细信息。