I am using Google location (longitude/latitude api) to search location based on user input (longitude/latitude,country,city,location_id) so for that first I am loading Magento collection and iterating collection data and based on data i am sending request to google Api. But it is taking too time to get result (around 15-20 sec).
$mapviewCollection = Mage::getModel("location/store")->getCollection();
if (isset($country_id) && $country_id !== 'NULL') {
$mapviewCollection->addFieldToFilter('website_id', $country_id);
}
if (isset($city_id) && $city_id !== 'NULL') {
$mapviewCollection->addFieldToFilter('city_id', $city_id);
}
if (isset($location_id) && $location_id !== 'NULL') {
$mapviewCollection->addFieldToFilter('location_id', $location_id);
}
if (isset($brand_id) && $brand_id !== 'NULL') {
$mapviewCollection->addFieldToFilter('id', $brand_id);
}
/* mapview of store within selected Kilometers */
if (isset($area_km) && $area_km !== 'NULL') {
$mapviewCollection->addFieldToSelect(array('id','store_name','latitude','longitude' ));
$mapviews = $mapviewCollection->getData();
if ($mapviews) {
foreach ($mapviews as $mapview) {
if ($mapview['latitude'] != null && $mapview['longitude'] != null) {
$source = $latitude . ',' . $longitude;
$destination = $mapview['latitude'] . ',' . $mapview['longitude'];
$apikey = Mage::getStoreConfig('testapi/testapi_group/google_mapapi_key');
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" . $source . "&destinations=" . $destination . "&mode=driving&language=en-US&key=" . $apikey;
$string = file_get_contents($url);
$json_a = json_decode($string, TRUE);
$distance = $json_a['rows'][0]['elements'][0]['distance']['value']; // distance in meters km = m / 1000.0
$distance = (float) ($distance / 1000);
if ($distance <= $area_km) {
$mapviewarr['test_id'] = $mapview['id'];
$mapviewarr['test_name'] = Mage::helper('mobileapi')->translate($mapview['store_name'], $locale);
$mapviewarr['latitude'] = $mapview['latitude'];
$mapviewarr['longitude'] = $mapview['longitude'];
$data[] = $mapviewarr;
}
}
}
I have tried by writing simple mysql query also and found no change. Seems the time it is taking because of foreach loop. Please help me how i can decrease the response time for time. Please give your suggestion ASAP.