how to add google map api key in EGamp in yii

966 views Asked by At

I have a google map api key .. AdfadXXXXXXXXXXX....

I have installed EGamp extension . how can i set Api key in below code for localhost and production.

I tried using $gMap->setAPIKey('local.mylocal.com','MYAPYKEY'); function of EGmap but not working ..

Still in console it displaying that "Google Maps API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys".

Below is my code.

Yii::import('vendor.2amigos.egmap.*');
            $gMap = new EGMap();
            $gMap->zoom = 16;
            $gMap->mapTypeControl = false;
            $gMap->scrollwheel = false;

            $gMap->setContainerStyle('width', '100%');
            $gMap->setContainerStyle('height', '250px');
            $gMap->setCenter($defaultVenue->lat, $defaultVenue->lng);

            // Create GMapInfoWindows
            $infoWindow = new EGMapInfoWindow('<div>' . nl2br($defaultVenue->full_address) . '</div>');

            // Create marker
            $marker = new EGMapMarker($defaultVenue->lat, $defaultVenue->lng, ['title' => $defaultVenue->full_address]);
            $marker->addHtmlInfoWindow($infoWindow);
            $gMap->addMarker($marker);

Can i use same key for local host and production ?

1

There are 1 answers

0
srakl On BEST ANSWER

Was using this extension a couple of months ago. Not sure if i was missing something when reading the documentation, but what i did was hacked it a bit to get it to work. In your EGMap.php change the code to this ...

    public function registerMapScript($afterInit=array(), $language = null, $region = null, $position = CClientScript::POS_LOAD)
    {
            // TODO: include support in the future
            //$params = 'sensor=false';
            $params = '';

            $key = $this->getAPIKey();

            if ($language !== null)
                $params .= '&language=' . $language;
            if ($region !== null)
                $params .= '&region=' . $region;

            //hack!!
            if ($key !== null)
                $params .= '&key='. $key;
            //end of hack


            CGoogleApi::init();
            CGoogleApi::register('maps', '3', array('other_params' => $params));

            ....
            ....
            ....
            ....
   }

In your view, don't forget to add $gMap->setAPIKey(DOMAIN, APIKEY) before your $gMap->renderMap();

Hope that works