2

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.

2
  • 2
    How many stores are there?
    – Smartie
    Sep 22, 2015 at 10:13
  • 1
    @smartie In database i have 657 stores. But while fetching 3-4 will come as a result. Actually the issue is not while fetching from DB,the issue is with foreach loop because for each found record i am sending request to google Api.
    – Hina
    Sep 22, 2015 at 10:33

1 Answer 1

1

You have kind of answered your own question in your comment.

Relying on a third party service is always going to be a bottle neck and further more, you have very little control over it.

Although you might only have 3 or 4 results at the end, this is being deduced from the result of the google api (mostly). If i was in this situation i would be looking to reduce the number of results i need to look up using the api (in your mapviewCollection result).

Basically i would try and use the google api as little as possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.