marquito | Bonjour, comme j'ai toujours été bien aidé sur ce forum je reviens vous voir sur un sujet concernant les cartes de Google.
J'ai un script qui affiche la carte quand je renseigne la ville, le code postal et l'adresse jusqu'à là c'est tout bon Si je ne renseigne pas l'adresse rien ne s'affiche Mais moi j'aimerai pouvoir montrer la la carte même si l'adresse n'ai pas renseignée !
Vous voyez le truc il arrive que certain utilisateur n'on pas d'adresse par exemple les entreprise dans les zones.
Je vous montre le code et je vous remercie pour votre aide
Code :
- <?php
- class GoogleMap
- {
- private $apiKey;
- private $points = array();
- private $tags;
- private $depth;
- private $xmlArray;
- private $zoomLevel;
- private $showControl = true;
- private $controlType = "small";
- private $showMapType = true;
- private $responseCode;
-
- public function setApiKey($apiKey)
- {
- $this->apiKey = $apiKey;
- }
-
- public function setZoomLevel($zoomLevel)
- {
- $this->zoomLevel = $zoomLevel;
- }
-
- public function addGeoPoint($lat, $lng, $description = "" )
- {
- $point = array("lat" => $lat,
- "lng" => $lng,
- "address" => "",
- "description" => $description);
-
- array_push($this->points, $point);
- }
-
- public function addAddress($address, $description = "" )
- {
- $url = "http://maps.google.com/maps/geo?&output=xml&key=".$this->apiKey."&q=".urlencode($address);
- $buff = @file_get_contents($url);
- $results = $this->xml2array(utf8_encode($buff));
-
- $this->responseCode = (!empty($results['kml']['Response']['Status']['code'])) ? $results['kml']['Response']['Status']['code'] : 0;
-
- if(!empty($results['kml']['Response']['Placemark']['Point']['coordinates']))
- {
- $coords = explode(",", $results['kml']['Response']['Placemark']['Point']['coordinates']);
-
- $point = array("lat" => $coords[1],
- "lng" => $coords[0],
- "address" => $address,
- "description" => $description);
-
- array_push($this->points, $point);
- return array("lat" => $coords[1], "lng" => $coords[0]);
- }
-
- return false;
- }
-
- public function validPointsExists()
- {
- return (!empty($this->points));
- }
-
- public function isServiceProblem()
- {
- return ($this->responseCode != 200);
- }
-
- public function getScriptCode()
- {
- return "\n<script src=\"http://maps.google.com/maps?file=api&v=2&key=".$this->apiKey."\" type=\"text/javascript\"></script>\n";
- }
-
- public function getMapCode()
- {
- $html = "<script type=\"text/javascript\">\n
- function showGoogleMap()
- {
- //<![CDATA[
- if (GBrowserIsCompatible())
- {
- var map = new GMap(document.getElementById(\"map\" ));
- map.centerAndZoom(new GLatLng(".$this->points[0]['lat'].",".$this->points[0]['lng']." ), ".$this->zoomLevel." );
- }
- var icon = new GIcon();
- icon.image = \"http://labs.google.com/ridefinder/images/mm_20_red.png\";
- icon.shadow = \"http://labs.google.com/ridefinder/images/mm_20_shadow.png\";
- icon.iconSize = new GSize(12, 20);
- icon.shadowSize = new GSize(22, 20);
- icon.iconAnchor = new GPoint(6, 20);
- icon.infoWindowAnchor = new GPoint(5, 1);
- ";
-
- if($this->showControl)
- {
- switch($this->controlType)
- {
- case "small": $html .= "map.addControl(new GSmallMapControl());\n"; break;
- case "large": $html .= "map.addControl(new GLargeMapControl());\n"; break;
- }
- }
-
- if($this->showMapType)
- {
- $html .= "map.addControl(new GMapTypeControl());\n";
- }
-
- $i = 0;
-
- foreach($this->points as $point)
- {
- $i++;
-
- $html .= "
- var point".$i." = new GLatLng(".$point['lat'].",".$point['lng']." );
- var marker".$i." = new GMarker(point".$i." );
- map.addOverlay(marker".$i." );
- ";
-
- $infoText = ($point['description']) ? $point['description'] : htmlspecialchars($point['address']);
-
- if($infoText)
- {
- $html .= "GEvent.addListener(marker".$i.", \"click\", function(){
- marker".$i.".openInfoWindowHtml(\"".$infoText."\" );
- });
- ";
- }
- }
-
- $html .= "//]]>
- }
- \$(window).addEvent(\"load\", showGoogleMap);
- </script>
- ";
-
- return $html;
- }
-
- public function getUserSideMapCode($address, $description = "" )
- {
- $point = array(
- "address" => $address,
- "description" => $description);
-
- array_push($this->points, $point);
-
- $html = "<script type=\"text/javascript\">\n
- function showGoogleMap()
- {
- var map = new GMap(document.getElementById(\"map\" ));
- map.enableContinuousZoom();
- map.enableInfoWindow();
- map.addControl(new GSmallMapControl());
- map.addControl(new GMapTypeControl());
- //map.addControl(new GOverviewMapControl());
-
- var geo = new GClientGeocoder();
- ";
-
- $i = 0;
-
- foreach($this->points as $point)
- {
- $i++;
-
- $html .= "geo.getLocations('".addslashes($point['address'])."', function(result)
- {
- if(result.Status.code != G_GEO_SUCCESS)
- {
- alert(result.Status.code);
- return;
- }
-
- map.clearOverlays();
-
- var p = result.Placemark[0].Point.coordinates;
- var lng = p[1];
- var lat = p[0];
-
- var latLng = new GLatLng(parseFloat(lng), parseFloat(lat));
- map.centerAndZoom(latLng, ".($this->zoomLevel)." );
- var marker".$i." = new GMarker(latLng);
- map.addOverlay(marker".$i." );
- ";
-
- $infoText = ($point['description']) ? $point['description'] : htmlspecialchars($point['address']);
-
- if($infoText)
- {
- $html .= "GEvent.addListener(marker".$i.", \"click\", function(){
- marker".$i.".openInfoWindowHtml(\"".$infoText."\" );
- });
- ";
- }
-
- $html .= "
- });";
- }
-
- $html .= "
- }
- \$(window).addEvent(\"load\", showGoogleMap);
- </script>
- ";
-
- return $html;
- }
-
- public function xml2array($xml)
- {
- $this->depth = -1;
- $xmlParser = xml_parser_create();
- xml_set_object($xmlParser, $this);
- xml_set_element_handler($xmlParser, "startElement", "endElement" );
- xml_set_character_data_handler($xmlParser, "characterData" );
- xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, 0);
- xml_parse($xmlParser, $xml, true);
- xml_parser_free($xmlParser);
-
- return $this->xmlArray[0];
- }
-
- public function startElement($parser, $name, $attribs)
- {
- $this->tags[] = $name;
- $this->depth++;
- }
-
- public function characterData($parser, $data)
- {
- $key = end($this->tags);
- $this->xmlArray[$this->depth][$key] = $data;
- }
-
- public function endElement($parser, $name)
- {
- $tag = array_pop($this->tags);
- $childDepth = $this->depth + 1;
- if(!empty($this->xmlArray[$childDepth]))
- {
- $this->xmlArray[$this->depth][$tag] = $this->xmlArray[$childDepth];
- unset($this->xmlArray[$childDepth]);
- }
-
- $this->depth--;
- }
-
- }
- ?>
|
|