user2636556
user2636556

Reputation: 1915

get users geolocation using EgMap

I'm currently using the EGmap extension for Yii. and was wondering how do i get the users current geolocation one the map loads?

Here is my code:

Yii::app()->clientScript->registerScript('filterscript',"
        if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      alert(initialLocation)
      //map.setCenter(initialLocation);
    }, function() {
      alert(initialLocation); //alerts the lat lng
    });
  }
  // Browser doesn't support Geolocation
  else {
    alert('fail');
  }
  ",CClientScript::POS_READY);

    Yii::import('ext.EGMap.*');

            $gMap = new EGMap();
            $gMap->zoom = 5;
            $gMap->width = '100%';
            $gMap->height = 200;

            $mapTypeControlOptions = array(
                'position'=> EGMapControlPosition::RIGHT_TOP,
                'style'=>EGMap::MAPTYPECONTROL_STYLE_DEFAULT,
            );

            $gMap->mapTypeId = EGMap::TYPE_HYBRID;

            $gMap->mapTypeControlOptions= $mapTypeControlOptions;

            // Create geocoded lat and lng here
            $lat = $lng =''; 

            // Center the map on geocoded address
            $gMap->setCenter($lat, $lng);

            // Add marker on geocoded address
            $gMap->addMarker(
                new EGMapMarker($lat, $lng)
            );

            $gMap->renderMap();

If not using the extension, got it to work using this. (tutorial from google map api page)

  var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

  // Try W3C Geolocation (Preferred)
  if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  }
  // Browser doesn't support Geolocation
  else {
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }

how do i put the users lat lang marker on the map since it's done from outside the extension? Am i even doing it right?

Upvotes: 4

Views: 756

Answers (1)

user133408
user133408

Reputation:

First set jsName:

$gMap->setJsName('test_map');

Then add afterInit code to renderMap call, including name from previous step:

$script = "
        if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      alert(initialLocation)
      test_map.setCenter(initialLocation); // <-- HERE: put name from setJsName() call
    }, function() {
      alert(initialLocation); //alerts the lat lng
    });
  }
  // Browser doesn't support Geolocation
  else {
    alert('fail');
  }
  ";

Fistst parameter is array of additional init scripts:

$gMap->renderMap(array($script));

DISCLAIMER: Not tested, but should point you in right direction:)

Upvotes: 2

Related Questions