Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用EgMap获取用户的地理位置_Php_Google Maps_Yii - Fatal编程技术网

Php 使用EgMap获取用户的地理位置

Php 使用EgMap获取用户的地理位置,php,google-maps,yii,Php,Google Maps,Yii,我目前正在使用。我想知道如何让用户在地图加载时获得当前地理位置 这是我的密码: Yii::app()->clientScript->registerScript('filterscript'," if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { initialLocation = new google.maps.LatL

我目前正在使用。我想知道如何让用户在地图加载时获得当前地理位置

这是我的密码:

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();
如果不使用扩展,请使用该扩展使其工作。()

既然用户lat lang标记是从扩展外部完成的,我如何将其放在地图上?我做得对吗?

第一套jsName:

$gMap->setJsName('test_map');
然后将
afterInit
code添加到
renderMap
调用中,包括上一步的名称:

$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');
  }
  ";

免责声明:未测试,但应为您指明正确的方向:)

如果您已经找到了一种不使用扩展的方法,您可以设法做到这一点,扩展只是一个帮助程序,您可以通过首先为map object设置名称来引用它。
$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');
  }
  ";
$gMap->renderMap(array($script));