GumGum Auspuff: Anfänger - GEOCoding mit Google Maps API

Hallo,

wir haben heute im Informatikunterricht die Aufgabe bekommen mit PHP und der Google Maps API zu einer Adresse eine GEO Coordinate zu erstellen.

Ich habe den Testeinwandfrei hinbekommen. Und probiere mich jetzt gerade durch. Gewählt habe ich die Ausgabe von der Google Maps API via XML.
Leider verstehe ich nicht wie ich auf die "address_component" zugreifen kann.
Nehmen wir folgenden Datensatz:

(  
    [status] => OK  
    [result] => SimpleXMLElement Object  
        (  
            [type] => route  
            [formatted_address] => Ackerstraße, 10115 Berlin, Deutschland  
            [address_component] => Array  
                (  
                    [0] => SimpleXMLElement Object  
                        (  
                            [long_name] => Ackerstraße  
                            [short_name] => Ackerstraße  
                            [type] => route  
                        )  
  
                    [1] => SimpleXMLElement Object  
                        (  
                            [long_name] => Mitte  
                            [short_name] => Mitte  
                            [type] => Array  
                                (  
                                    [0] => sublocality  
                                    [1] => political  
                                )  
  
                        )  
  
                    [2] => SimpleXMLElement Object  
                        (  
                            [long_name] => Berlin  
                            [short_name] => Berlin  
                            [type] => Array  
                                (  
                                    [0] => locality  
                                    [1] => political  
                                )  
  
                        )  
  
                    [3] => SimpleXMLElement Object  
                        (  
                            [long_name] => Berlin  
                            [short_name] => Berlin  
                            [type] => Array  
                                (  
                                    [0] => administrative_area_level_1  
                                    [1] => political  
                                )  
  
                        )  
  
                    [4] => SimpleXMLElement Object  
                        (  
                            [long_name] => Deutschland  
                            [short_name] => DE  
                            [type] => Array  
                                (  
                                    [0] => country  
                                    [1] => political  
                                )  
  
                        )  
  
                    [5] => SimpleXMLElement Object  
                        (  
                            [long_name] => 10115  
                            [short_name] => 10115  
                            [type] => postal_code  
                        )  
  
                )

Hier kann ich mit
$xml->result->formatted_address;
die Adresse holen. Soweit verstanden. Somit müssten wir ja bei  print_r($xml->result->address_component);
alle folgenden im Array vorhanden Inhalte angezeigt werden. Dem ist aber nicht so ich sehe nur

SimpleXMLElement Object  
(  
    [long_name] => Ackerstraße  
    [short_name] => Ackerstraße  
    [type] => route  
)

Mache ich aber einen Count auf das Array also count($xml->result->address_component)

Bekomme ich die Zahl 6 ?

Ich verstehe das nicht! Bitte helft mir.

  1. (

    [status] => OK
        [result] => SimpleXMLElement Object
            (
                [type] => route
                [formatted_address] => Ackerstraße, 10115 Berlin, Deutschland
                [address_component] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [long_name] => Ackerstraße
                                [short_name] => Ackerstraße
                                [type] => route
                            )

    [1] => SimpleXMLElement Object
                            (
                                [long_name] => Mitte
                                [short_name] => Mitte
                                [type] => Array
                                    (
                                        [0] => sublocality
                                        [1] => political
                                    )

    )

    [2] => SimpleXMLElement Object
                            (
                                [long_name] => Berlin
                                [short_name] => Berlin
                                [type] => Array
                                    (
                                        [0] => locality
                                        [1] => political
                                    )

    )

    [3] => SimpleXMLElement Object
                            (
                                [long_name] => Berlin
                                [short_name] => Berlin
                                [type] => Array
                                    (
                                        [0] => administrative_area_level_1
                                        [1] => political
                                    )

    )

    [4] => SimpleXMLElement Object
                            (
                                [long_name] => Deutschland
                                [short_name] => DE
                                [type] => Array
                                    (
                                        [0] => country
                                        [1] => political
                                    )

    )

    [5] => SimpleXMLElement Object
                            (
                                [long_name] => 10115
                                [short_name] => 10115
                                [type] => postal_code
                            )

    )

      
    Wie hast du diese Ausgabe erzeugt und vorallem welchen Teil verschweigst du uns bei der Ausgabe? Das ist weder eine vollständige Ausgabe von @var\_dump noch von @print\_r, obwohl es mehr nach letzterem aussieht.  
      
    [Das Handbuch](http://php.net/manual/en/function.print-r.php) ist hier ziemlich eindeutig und @print\_r sollte genau so funktionieren, wie von dir erwartet. Dein Fehler steckt vermutlich woanders.
    
    1. Ok, fangen wir anders an. Mein Querry an die Google API sieht wie folgend aus:
      http://maps.googleapis.com/maps/api/geocode/xml?address=ackerstr.+mitte,+berlin,+10115,+deutschland&sensor=false&language=de
      Ich nutze nun simplexml um ihn zu öffnen und erhalte folgenden Datensatz:

      SimpleXMLElement Object  
      (  
          [status] => OK  
          [result] => SimpleXMLElement Object  
              (  
                  [type] => route  
                  [formatted_address] => Ackerstraße, 10115 Berlin, Deutschland  
                  [address_component] => Array  
                      (  
                          [0] => SimpleXMLElement Object  
                              (  
                                  [long_name] => Ackerstraße  
                                  [short_name] => Ackerstraße  
                                  [type] => route  
                              )  
        
                          [1] => SimpleXMLElement Object  
                              (  
                                  [long_name] => Mitte  
                                  [short_name] => Mitte  
                                  [type] => Array  
                                      (  
                                          [0] => sublocality  
                                          [1] => political  
                                      )  
        
                              )  
        
                          [2] => SimpleXMLElement Object  
                              (  
                                  [long_name] => Berlin  
                                  [short_name] => Berlin  
                                  [type] => Array  
                                      (  
                                          [0] => locality  
                                          [1] => political  
                                      )  
        
                              )  
        
                          [3] => SimpleXMLElement Object  
                              (  
                                  [long_name] => Berlin  
                                  [short_name] => Berlin  
                                  [type] => Array  
                                      (  
                                          [0] => administrative_area_level_1  
                                          [1] => political  
                                      )  
        
                              )  
        
                          [4] => SimpleXMLElement Object  
                              (  
                                  [long_name] => Deutschland  
                                  [short_name] => DE  
                                  [type] => Array  
                                      (  
                                          [0] => country  
                                          [1] => political  
                                      )  
        
                              )  
        
                          [5] => SimpleXMLElement Object  
                              (  
                                  [long_name] => 10115  
                                  [short_name] => 10115  
                                  [type] => postal_code  
                              )  
        
                      )  
        
                  [geometry] => SimpleXMLElement Object  
                      (  
                          [location] => SimpleXMLElement Object  
                              (  
                                  [lat] => 52.5318480  
                                  [lng] => 13.3961610  
                              )  
        
                          [location_type] => GEOMETRIC_CENTER  
                          [viewport] => SimpleXMLElement Object  
                              (  
                                  [southwest] => SimpleXMLElement Object  
                                      (  
                                          [lat] => 52.5289240  
                                          [lng] => 13.3913010  
                                      )  
        
                                  [northeast] => SimpleXMLElement Object  
                                      (  
                                          [lat] => 52.5350052  
                                          [lng] => 13.3976550  
                                      )  
        
                              )  
        
                          [bounds] => SimpleXMLElement Object  
                              (  
                                  [southwest] => SimpleXMLElement Object  
                                      (  
                                          [lat] => 52.5289240  
                                          [lng] => 13.3913010  
                                      )  
        
                                  [northeast] => SimpleXMLElement Object  
                                      (  
                                          [lat] => 52.5350052  
                                          [lng] => 13.3976550  
                                      )  
        
                              )  
        
                      )  
        
                  [partial_match] => true  
              )  
        
      )
      

      Wie komme ich nun an $xml->result->address_component->1->type->0 ?

      1. Und du hast mir immernoch nicht verraten, wie du diese Ausgabe erzeugst.

        Wie komme ich nun an $xml->result->address_component->1->type->0 ?

        $xml->result->address_component[1]->type[0];

  2. Tach!

    Ich habe den Testeinwandfrei hinbekommen. Und probiere mich jetzt gerade durch. Gewählt habe ich die Ausgabe von der Google Maps API via XML.
    Leider verstehe ich nicht wie ich auf die "address_component" zugreifen kann.

    Allgemein: Du arbeitest hier mit SimpleXML. Das versucht, einen einfach Zugriff auf das komplexe Gebilde XML zu bieten. Allerdings arbeitet es manchmal anders als man es von normalen Objekten und Arrays gewöhnt ist und erwartet, weil es eine Menge PHP-Magie eingebaut hat. Bitte informiere dich im PHP-Handbuch im SimpleXML-Kapitel, wie man damit arbeitet. Da sind einige Beispiele, die die Verwendungsweise beschreiben.

    dedlfix.