我想检测Yandex地图中的地图位置变化(与地图相机变化相同)。Google maps API 通过 OnCameraChangeListener() 提供了这个机会,但我找不到适合 Yandex 的机会。
SO:在Yandex地图中,Google的OnCameraChangeListener的合适功能是什么?
提前谢谢。
我知道
答案太晚了,但也许可以帮助某人:
class GeofenceActivity : ComponentActivity() , CameraListener {
private lateinit var mapViewYandex: com.yandex.mapkit.mapview.MapView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
SetupMapInitializing(this)
mapViewYandex.map.addCameraListener(this)
}
private fun SetupMapInitializing(cx : Context){
try {
MapKitFactory.setApiKey("your-key")
MapKitFactory.initialize(cx)
I18nManagerFactory.setLocale(Locale.getDefault().language)
} catch(e : IOException){
e.printStackTrace()
}
}
override fun onCameraPositionChanged(
p0: Map,
p1: CameraPosition,
p2: CameraUpdateReason,
p3: Boolean
) {
Log.w(
"Yandex",
p1.target.latitude.toString() + " , " +
p1.target.longitude.toString()
)
}
}
您需要在顶部为映射添加一个标题为 boundschange
的事件处理程序
下面是一些简单的示例代码:
<script>
var myMap;
ymaps.ready(init);
function init () {
myMap = new ymaps.Map('map', {
center: [55.76, 37.64], // Moscow
zoom: 10
});
//add an event handler for detecting any change to the boudnaries of the map
myMap.events.add('boundschange', onBoundsChange);
//a callback function to do something with the new boundaries
function onBoundsChange(e){
console.log(e.originalEvent.newBounds);
}
}
</script>
还有最基本的 HTML:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Detecting boundary changes of a Yandex Map</title>
<script src="https://api-maps.yandex.ru/2.1/?lang=en_RU&apikey=<your API-key>" type="text/javascript"></script>
<style>
body, html {
padding: 0;
margin: 0;
width: 100%;
height: 100%;
}
#map {
width: 100%;
height: 90%;
}
</style>
</head>
<body>
<div id="map"></div>
</body>
</html>