Ocena wątku:
  • 1 głosów - średnia: 1
  • 1
  • 2
  • 3
  • 4
  • 5
Zmienna Google API
#1
Kod PHP:
<script type="text/javascript">

    var 
location1;
    var 
location2;
    
    var 
address1;
    var 
address2;

    var 
latlng;
    var 
geocoder;
    var 
map;
    
    var 
distance;
    var 
cena
    
    
// finds the coordinates for the two locations and calls the showMap() function
    
function initialize()
    {
        
geocoder = new google.maps.Geocoder(); // creating a new geocode object
        
        // getting the two address values
        
address1 document.getElementById("address1").value;
        
address2 document.getElementById("address2").value;
        
        
// finding out the coordinates
        
if (geocoder
        {
            
geocoder.geocode( { 'address'address1}, function(resultsstatus
            {
                if (
status == google.maps.GeocoderStatus.OK
                {
                    
//location of first address (latitude + longitude)
                    
location1 results[0].geometry.location;
                } else 
                {
                    
alert("Geocode was not successful for the following reason: " status);
                }
            });
            
geocoder.geocode( { 'address'address2}, function(resultsstatus
            {
                if (
status == google.maps.GeocoderStatus.OK
                {
                    
//location of second address (latitude + longitude)
                    
location2 results[0].geometry.location;
                    
// calling the showMap() function to create and show the map 
                    
showMap();
                } else 
                {
                    
alert("Geocode was not successful for the following reason: " status);
                }
            });
        }
    }
        
    
// creates and shows the map
    
function showMap()
    {
        
// center of the map (compute the mean value between the two locations)
        
latlng = new google.maps.LatLng((location1.lat()+location2.lat())/2,(location1.lng()+location2.lng())/2);
        
        
// set map options
            // set zoom level
            // set center
            // map type
        
var mapOptions 
        {
            
zoom1,
            
centerlatlng,
            
mapTypeIdgoogle.maps.MapTypeId.HYBRID
        
};
        
        
// create a new map object
            // set the div id where it will be shown
            // set the map options
        
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
        
        
// show route between the points
        
directionsService = new google.maps.DirectionsService();
        
directionsDisplay = new google.maps.DirectionsRenderer(
        {
            
suppressMarkerstrue,
            
suppressInfoWindowstrue
        
});
        
directionsDisplay.setMap(map);
        var 
request = {
            
origin:location1
            
destination:location2,
            
travelModegoogle.maps.DirectionsTravelMode.DRIVING
        
};
        
directionsService.route(request, function(responsestatus
        {
            if (
status == google.maps.DirectionsStatus.OK
            {
                
directionsDisplay.setDirections(response);
                
distance "<h4>Cena kursu:</h4> "+response.routes[0].legs[0].distance.text;
                                
                
distance += "<br/><h4>Planowany czas jazdy:</h4> "+response.routes[0].legs[0].duration.text;
                
                
document.getElementById("distance_road").innerHTML distance ;
                
                
            }
        });
        
     
        
        
// TWORZY MARKERY 2CH LOKALIZACJI
        
var marker1 = new google.maps.Marker({
            
mapmap
            
positionlocation1,
            
title"Początek trasy"
        
});
        var 
marker2 = new google.maps.Marker({
            
mapmap
            
positionlocation2,
            
title"Koniec trasy"
        
});
        
        
// create the text to be shown in the infowindows
        
var text1 '<div id="content">'+
                
'<h1 id="firstHeading">Miejsce startowe</h1>'+
                
'<div id="bodyContent">'+
            
'<p>TaxiStudent.pl</p>'+
                
'<p>Addres: '+address1+'</p>'+
                
'</div>'+
                
'</div>';
                
        var 
text2 '<div id="content">'+
            
'<h1 id="firstHeading">Koniec trasy</h1>'+
            
'<div id="bodyContent">'+
            
'<p>TaxiStudent.pl</p>'+
            
'<p>Addres: '+address2+'</p>'+
            
'</div>'+
            
'</div>';
        
        
// create info boxes for the two markers
        
var infowindow1 = new google.maps.InfoWindow({
            
contenttext1
        
});
        var 
infowindow2 = new google.maps.InfoWindow({
            
contenttext2
        
});

        
// add action events so the info windows will be shown when the marker is clicked
        
google.maps.event.addListener(marker1'click', function() {
            
infowindow1.open(map,marker1);
        });
        
google.maps.event.addListener(marker2'click', function() {
            
infowindow2.open(map,marker1);
        });
        
        
// compute distance between the two points
        
var 6371
        var 
dLat toRad(location2.lat()-location1.lat());
        var 
dLon toRad(location2.lng()-location1.lng()); 
        
        var 
dLat1 toRad(location1.lat());
        var 
dLat2 toRad(location2.lat());
        
        var 
Math.sin(dLat/2) * Math.sin(dLat/2) +
                
Math.cos(dLat1) * Math.cos(dLat1) * 
                
Math.sin(dLon/2) * Math.sin(dLon/2); 
        var 
Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
        var 
c;
        
    
    }
    
    function 
toRad(deg
    {
        return 
deg Math.PI/180;
    }
</
script

Mógłby ktoś pomóc i podać przykład jak pomnożyć wynik ,,distance" - długość trasy ? Próbowałem z dziesiątki razy i nie potrafię tego zrobić.
Odpowiedz
#2
distance to jest tekst, który ma się wyświetlić jako wynik (długość trasy, czas itp.)

Ciężko mi więcej cokolwiek powiedzieć mając tak sam kod, ale zobacz co ma takie coś:
Kod:
response.routes[0].legs[0].distance
Odpowiedz
#3
(27-11-2011, 04:51)Raalsky napisał(a): distance to jest tekst, który ma się wyświetlić jako wynik (długość trasy, czas itp.)

Ciężko mi więcej cokolwiek powiedzieć mając tak sam kod, ale zobacz co ma takie coś:
Kod:
response.routes[0].legs[0].distance


Już sobie poradziłem z problemem. Jeśli ktoś by kiedyś potrzebował to:
trzeba skorzystać z API V3
I NP:
Kod PHP:
response.routes[0].legs[0].distance.value response.routes[0].legs[0].distance.value *2
Odpowiedz


Podobne wątki…
Wątek: Autor Odpowiedzi: Wyświetleń: Ostatni post
  reklamy google liver128 4 3,506 26-08-2021, 06:32
Ostatni post: wozniak
  pomoc z google analitycs blondynka2 1 2,526 09-03-2015, 04:54
Ostatni post: blondynka2
  alias i złe łącze w google Ar2r 0 1,868 13-01-2013, 18:21
Ostatni post: Ar2r
  Pytanie o Google Analytics joker 5 4,852 16-11-2012, 22:53
Ostatni post: PrograStudio
  Google Maps niepoprawnie wyświetlane na Firefoxie G1T 3 3,117 02-08-2012, 17:53
Ostatni post: Ar2r

Skocz do:


Użytkownicy przeglądający ten wątek:
Sponsorzy i przyjaciele
SeoHost.pl