-1) $itemId = $data[$itemIdColIndex]; //Get the latitude if we found the column $latitude = ""; if($latitudeColIndex > -1) $latitude = $data[$latitudeColIndex]; if(!is_numeric($latitude)) $latitude == ""; //Get the longitude if we found the column $longitude = ""; if($longitudeColIndex > -1) $longitude = $data[$longitudeColIndex]; if(!is_numeric($longitude)) $longitude == ""; //Get the title if we found the column $title = ""; if($titleColIndex > -1) $title = $data[$titleColIndex]; //Get the subTitle if we found the column $subTitle = ""; if($subTitleColIndex > -1) $subTitle = $data[$subTitleColIndex]; //Default values for distance, label.. $distanceFromDevice = -1; $distanceLabel = ""; $showThisLocation = FALSE; //If we did not get the device info... show all the locations with no distance label if(!is_numeric($deviceLatitude) || !is_numeric($deviceLongitude)){ $showThisLocation = TRUE; }else{ //Calculate the distance between the device and the location. //Only do this if both locations have valid lat/lon pairs (numeric). if(is_numeric($deviceLatitude) && is_numeric($deviceLongitude)){ if(is_numeric($latitude) && is_numeric($longitude)){ //Call distance function $distanceFromDevice = getDistance($deviceLatitude, $deviceLongitude, $latitude, $longitude); //are we showing this location? if($distanceFromDevice < $maximumDistanceRadius || $maximumDistanceRadius == -1){ $showThisLocation = TRUE; //Round the distance..if it's more than 10 miles, ignore decimal. if($distanceFromDevice > 10){ $distanceFromDevice = ceil($distanceFromDevice); }else{ $distanceFromDevice = round($distanceFromDevice, 1); } //Wrap distance in parens. so it looks good appended to the title. $distanceLabel = " (" . $distanceFromDevice . " mi.)"; }//if we are within our the circle. }//is numeric }//is numeric } //if we didn't get a distance, clear the label so empty parens. don't show. if($distanceFromDevice == -1){ $distanceLabel = ""; } //Add the JSON location object to the output if we are within the circle. if($showThisLocation){ //Create an array from this locations data.. //the child item $thisItem = array(); //build a BT_screen_webView object to load when the location is tapped $loadScreen = "{"; $loadScreen .= "\"itemNickname\":\"Details-" . $itemId. "\", "; $loadScreen .= "\"itemId\":\"" . $itemId . "\", "; $loadScreen .= "\"itemType\":\"BT_screen_webView\", "; $loadScreen .= "\"dataURL\":\"http://www.yourdomain.com/locations_details.php?itemId=" . $itemId . "\", "; $loadScreen .= "\"navBarTitleText\":\"" . trim($title). "\", "; $loadScreen .= "\"showBrowserBarBack\":\"1\","; $loadScreen .= "\"showBrowserBarRefresh\":\"1\","; $loadScreen .= "\"showBrowserBarLaunchInNativeApp\":\"1\""; $loadScreen .= "}"; //map locations if(strtoupper($outputType) == "MAPLOCATIONS"){ $thisItem["itemId"] = $itemId; $thisItem["itemType"] = "BT_mapLocation"; $thisItem["latitude"] = $latitude; $thisItem["longitude"] = $longitude; $thisItem["distanceFromDevice"] = $distanceFromDevice; $thisItem["title"] = trim($title) . $distanceLabel; $thisItem["subTitle"] = trim($subTitle); $thisItem["transitionType"] = ""; $thisItem["soundEffectFileName"] = ""; $thisItem["loadScreenWithItemId"] = ""; $thisItem["loadScreenWithNickname"] = ""; $thisItem["loadScreenObject"] = $loadScreen; } //menu items.. if(strtoupper($outputType) == "MENUITEMS"){ $thisItem["itemId"] = $itemId; $thisItem["itemType"] = "BT_menuItem"; $thisItem["titleText"] = trim($title); $thisItem["descriptionText"] = trim($subTitle) . $distanceLabel; $thisItem["distanceFromDevice"] = $distanceFromDevice; $thisItem["iconName"] = ""; $thisItem["iconURL"] = ""; $thisItem["transitionType"] = ""; $thisItem["soundEffectFileName"] = ""; $thisItem["rowAccessoryType"] = ""; $thisItem["loadScreenWithItemId"] = ""; $thisItem["loadScreenWithNickname"] = ""; $thisItem["loadScreenObject"] = $loadScreen; } //add to array $childItems[] = $thisItem; }//Not showing this location }//Not the first row } //Close the file handle fclose($handle); }else{ //show an error, we could not open the file. echo "Failed to open file: " . $filePath; exit(); } //Sort results by distance, assign values to new array $sortedArray = array(); if(count($childItems) > 0){ $tmp = array(); foreach($childItems as &$ma){ $tmp[] = &$ma["distanceFromDevice"]; } array_multisort($tmp, $childItems); $cnt = 0; foreach($childItems as &$ma){ //rebuild new array with sort $tmpItem = array(); //map-locations if(strtoupper($outputType) == "MAPLOCATIONS"){ $tmpItem["itemId"] = $childItems[$cnt]["itemId"]; $tmpItem["itemType"] = $childItems[$cnt]["itemType"]; $tmpItem["latitude"] = $childItems[$cnt]["latitude"]; $tmpItem["longitude"] = $childItems[$cnt]["longitude"]; $tmpItem["distanceFromDevice"] = $childItems[$cnt]["distanceFromDevice"]; $tmpItem["title"] = $childItems[$cnt]["title"]; $tmpItem["transitionType"] = $childItems[$cnt]["transitionType"]; $tmpItem["soundEffectFileName"] = $childItems[$cnt]["soundEffectFileName"]; $tmpItem["loadScreenWithItemId"] = $childItems[$cnt]["loadScreenWithItemId"]; $tmpItem["loadScreenWithNickname"] = $childItems[$cnt]["loadScreenWithNickname"]; $tmpItem["loadScreenObject"] = $childItems[$cnt]["loadScreenObject"]; } //menu-items if(strtoupper($outputType) == "MENUITEMS"){ $tmpItem["itemId"] = $childItems[$cnt]["itemId"]; $tmpItem["itemType"] = $childItems[$cnt]["itemType"]; $tmpItem["titleText"] = $childItems[$cnt]["titleText"]; $tmpItem["descriptionText"] = $childItems[$cnt]["descriptionText"]; $tmpItem["distanceFromDevice"] = $childItems[$cnt]["distanceFromDevice"]; $tmpItem["iconName"] = $childItems[$cnt]["iconName"]; $tmpItem["iconURL"] = $childItems[$cnt]["iconURL"]; $tmpItem["transitionType"] = $childItems[$cnt]["transitionType"]; $tmpItem["soundEffectFileName"] = $childItems[$cnt]["soundEffectFileName"]; $tmpItem["rowAccessoryType"] = $childItems[$cnt]["rowAccessoryType"]; $tmpItem["loadScreenWithItemId"] = $childItems[$cnt]["loadScreenWithItemId"]; $tmpItem["loadScreenWithNickname"] = $childItems[$cnt]["loadScreenWithNickname"]; $tmpItem["loadScreenObject"] = $childItems[$cnt]["loadScreenObject"]; } //add to array, increment count $sortedArray[] = $tmpItem; $cnt++; } } //Prepare to write the JSON output to the screen. header("Content-type: application/json"); //Loop through the childItems array and format the JSON echo "{\"childItems\":["; //for each item in the sorted array $parentCnt = 0; for($i = 0; $i < count($sortedArray); $i++){ $parentCnt++; //JSON format depends on outputType (see data above) $childItem = $sortedArray[$i]; $childCnt = 0; echo "\n{"; foreach ($childItem as $key => $val) { $childCnt++; //JSON (no quotes around value if this is the loadScreenObject if($key == "loadScreenObject"){ echo "\"" . $key . "\":" . $val; }else{ echo "\"" . $key . "\":\"" . $val . "\""; } //Don't print comma after last item.. if($childCnt < count($childItem)){ echo ","; } } echo "}"; //Don't print comma after last item.. if($parentCnt < count($sortedArray)){ echo ","; } }//end for //End the JSON output echo "\n]}"; //Done exit(); ?>