用Flex玩转Google Map 之三-自动显示你所在的城市
自动显示你所在的城市
摘自原文:Writing the Hello World of the Maps API for Flash 片断(翻译能力有限,仅供个人学习参考,看得懂就好-_-||)
Google Map API for Flash 之纬度和经度(Latitudes and Longitudes)
现在我们有一个map 了,我们需要有一种方法来定位地图位置。Google Maps API for Flash的LatLng 对像提供了一个机制。构造一个LatLng对像,传递 { latitude, longitude }作为它的参数,是一种习惯的绘图法:
var myGeographicCoordinates:LatLng = new LatLng(myLatitude, myLongitude);
注意:转换一个地址为一个地理点的程序是geocoding,我们会在Google Maps API for Flash Services部分详细介绍的
它可以简单地指向一个地理点,它也可以定义一个地理点对像。例如,一个map显示一个完全世界为当前窗口,被称为 视点。这个视点可以用矩形点来定义。LatLngBounds对像提供这个功能,使用两个LatLng对像(表现为southwest 和 northeast)定义一个矩形区域。
LatLng对像有许多的API。com.google.maps.overlays.Marker对像用一个LatLng对像作为参数,例如,根据在你给的地理点上,放置一个标签覆盖在地图上。
下面例子使用getLatLngBounds()方法返回当前视点,然后在这些范围内,随机放置10个标签在地图上:
private function onMapReady(event:MapEvent):void {
setCenter(new LatLng(37.4419, -122.1419), 13, MapType.NORMAL_MAP_TYPE);
// 随机放置10个标签在地图上
var bounds:LatLngBounds = getLatLngBounds();
var southWest:LatLng = bounds.getSouthWest();
var northEast:LatLng = bounds.getNorthEast();
var lngSpan:Number = northEast.lng() – southWest.lng();
var latSpan:Number = northEast.lat() – southWest.lat();
for (var i:int = 0; i < 10; i++) {
var newLat:Number = southWest.lat() + (latSpan * Math.random());
var newLng:Number = southWest.lng() + (lngSpan * Math.random());
var latlng:LatLng = new LatLng(newLat, newLng);
addOverlay(new Marker(latlng));
}
}
}
查看例子(MapMarkers.html)
查看代码(MapMarkers.as)
注意:更多关于Marker对像的信息请看Overlays章节
Google已发放出as版的Map API了,记得要申请一个API key
地址:http://code.google.com/apis/maps/documentation/flash/intro.html
Flash/Flex中如何使用的例子:http://code.google.com/p/gmaps-samples-flash/
Google Maps API Blog:http://googlemapsapi.blogspot.com/