gpt4 book ai didi

php - 使用 EgMap 获取用户地理定位

转载 作者:可可西里 更新时间:2023-10-31 23:30:49 24 4
gpt4 key购买 nike

我目前正在使用 the EGmap extension for Yii .并且想知道如何在 map 加载时获取用户当前的地理位置?

这是我的代码:

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();

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

我如何将用户的经纬度标记放在 map 上,因为它是从扩展程序外部完成的?我做得对吗?

最佳答案

首先设置jsName:

$gMap->setJsName('test_map');

然后将 afterInit 代码添加到 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');
}
";

第一个参数是附加初始化脚本的数组:

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

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

关于php - 使用 EgMap 获取用户地理定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26129040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com