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