如何設置區域與谷歌地圖SDK的iOS? 我想設置縮放標記的位置和半徑。如何設置區域與谷歌地圖SDK的iOS?
回答
我通過框架的頭文件進行搜索,只找到可用於下面的代碼,可能是一個開始的接口。這裏的問題是,我無法在其他頭文件中找到任何GMSCoordinateBounds的導入,所以我找不到在GMSMapView中顯示此區域的方法。
GMSVisibleRegion region;
region.farLeft = CLLocationCoordinate2DMake(farLeftLatitude, farLeftlongitude);
region.farRight = CLLocationCoordinate2DMake(farRightlatitude, farRightlongitude);
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithRegion:region];
UPDATE:
下原來的答案是過時的的SDK 1.2版本 - 您現在可以使用GMSCameraUpdate
類的fitBounds:
方法:
https://developers.google.com/maps/documentation/ios/reference/interface_g_m_s_camera_update
原始回答:
MapKit中的MKMapPoint
類型定義了地圖的2D投影。儘管投影的實際值是不透明的,但它們相當於縮放級別爲20的像素。這可用於將經度/緯度值轉換爲像素,從而將比例轉換爲縮放比例,從而縮放比例。
首先定義兩個位置,指定要顯示的區域的邊界。這可能是邊界框的對角,或只有兩個位置,例如:
CLLocationCoordinate2D location1 =
CLLocationCoordinate2DMake(-33.8683, 151.2086); // Sydney
CLLocationCoordinate2D location2 =
CLLocationCoordinate2DMake(-31.9554, 115.8585); // Perth
如果要包括兩個以上的點,你可以計算出他們自己的邊界。這也可以使用GMSCoordinateBounds
完成,例如:
GMSCoordinateBounds* bounds =
[[GMSCoordinateBounds alloc]
initWithCoordinate: CLLocationCoordinate2DMake(-33.8683, 151.2086) // Sydney
andCoordinate: CLLocationCoordinate2DMake(-31.9554, 115.8585)]; // Perth
bounds = [bounds including:
CLLocationCoordinate2DMake(-12.4667, 130.8333)]; // Darwin
CLLocationCoordinate2D location1 = bounds.southWest;
CLLocationCoordinate2D location2 = bounds.northEast;
接下來,你需要獲得在點地圖視圖的大小。你可以使用這個:
float mapViewWidth = _mapView.frame.size.width;
float mapViewHeight = _mapView.frame.size.height;
但是,這隻會在你已經創建地圖視圖時才起作用。另外,如果您使用getting started guide中的示例代碼,則該幀將設置爲CGRectZero
,因爲稍後將設置實際大小以填充屏幕。在這種情況下,如果你正在創建一個全屏幕的地圖,那麼你可能想是這樣的:
float mapViewWidth = [UIScreen mainScreen].applicationFrame.size.width;
float mapViewHeight = [UIScreen mainScreen].applicationFrame.size.height;
否則,使用它你創建你的地圖視圖大小。
現在你算算相機位置所需的信息:
MKMapPoint point1 = MKMapPointForCoordinate(location1);
MKMapPoint point2 = MKMapPointForCoordinate(location2);
MKMapPoint centrePoint = MKMapPointMake(
(point1.x + point2.x)/2,
(point1.y + point2.y)/2);
CLLocationCoordinate2D centreLocation = MKCoordinateForMapPoint(centrePoint);
double mapScaleWidth = mapViewWidth/fabs(point2.x - point1.x);
double mapScaleHeight = mapViewHeight/fabs(point2.y - point1.y);
double mapScale = MIN(mapScaleWidth, mapScaleHeight);
double zoomLevel = 20 + log2(mapScale);
GMSCameraPosition *camera = [GMSCameraPosition
cameraWithLatitude: centreLocation.latitude
longitude: centreLocation.longitude
zoom: zoomLevel];
然後,您可以用這臺相機初始化地圖視圖,或設置地圖視圖這臺相機。
爲使該代碼編譯,你需要將MapKit框架添加到您的項目,然後同時導入:
#import <MapKit/MapKit.h>
請注意,此代碼不處理環繞,如果你的座標跨越越過日期線。例如,如果您嘗試在東京和夏威夷使用此代碼,而不是顯示太平洋地區,它將嘗試顯示幾乎整個世界。在肖像模式下,不可能縮小到足以在左側看到夏威夷以及在右側看到東京,因此地圖最終以非洲爲中心,而兩個位置都不可見。如果你願意的話,你可以修改上面的代碼來處理日期行處理。
嗨撒克遜。只要閱讀這個答案。難道正確的zoomLevel是「double zoomLevel = 21.0f + log2(mapScale);」。我剛剛在GoogleMaps SDK中查看了最大可能的縮放級別,它似乎是21.0f;順便說一下,最小縮放級別是2.0f; – 2013-03-06 09:06:27
Hi @ xen100,其中20是在那裏,因爲'MKMapPoint'相當於縮放級別爲20的像素。如果你的邊界非常小,那麼'mapScale'最終會大於1.0,所以'zoomLevel'會最終結束大於20.如果縮放級別大於SDK的限制,那麼它會將縮放級別限制爲可支持的級別。 – 2013-03-06 10:03:34
謝謝。這幫了我很多。如果用位置座標與標記進行邊界限制,我建議您稍微減小視圖的寬度和高度,以免標記最終位於視圖的邊緣。 – 2013-03-25 14:39:48
我目前正在使用這種方法。
self.markers是一個字典,其中標記存儲在一個位置ID中,self.currentLocation是一個CLLocation2D,self.mapView是一個GMSMapView。
這裏的數學是檢查是否匹配寬度或高度的大小,然後根據x1/pow(2,zoom1)= x2/pow(2,縮放2)」,從而放大2 = LOG 2(X2 * POW(2 self.mapView.camera.zoom)/ X1)。
- (void)fitMarkers
{
if (2 > self.markers.count)
{
[self.mapView animateToCameraPosition:[GMSCameraPosition cameraWithTarget:self.currentLocation.coordinate zoom:kZoom]];
return;
}
NSArray* markers = self.markers.allValues;
GMSCoordinateBounds* markerBounds = [[GMSCoordinateBounds alloc] initWithCoordinate:((id<GMSMarker>)markers[0]).position andCoordinate:((id<GMSMarker>)markers[1]).position];
for (id<GMSMarker> marker in markers)
{
markerBounds = [markerBounds including:marker.position];
}
// get marker bounds in points
CGPoint markerBoundsTopLeft = [self.mapView.projection pointForCoordinate:CLLocationCoordinate2DMake(markerBounds.northEast.latitude, markerBounds.southWest.longitude)];
CGPoint markerBoundsBottomRight = [self.mapView.projection pointForCoordinate:CLLocationCoordinate2DMake(markerBounds.southWest.latitude, markerBounds.northEast.longitude)];
// get user location in points
CGPoint currentLocation = [self.mapView.projection pointForCoordinate:self.currentLocation.coordinate];
CGPoint markerBoundsCurrentLocationMaxDelta = CGPointMake(MAX(fabs(currentLocation.x - markerBoundsTopLeft.x), fabs(currentLocation.x - markerBoundsBottomRight.x)), MAX(fabs(currentLocation.y - markerBoundsTopLeft.y), fabs(currentLocation.y - markerBoundsBottomRight.y)));
// the marker bounds centered on self.currentLocation
CGSize centeredMarkerBoundsSize = CGSizeMake(2.0 * markerBoundsCurrentLocationMaxDelta.x, 2.0 * markerBoundsCurrentLocationMaxDelta.y);
// inset the view bounds to fit markers
CGSize insetViewBoundsSize = CGSizeMake(self.mapView.bounds.size.width - kMarkerSize/2.0 - kMarkerMargin, self.mapView.bounds.size.height - kMarkerSize/2.0 - kMarkerSize);
CGFloat x1;
CGFloat x2;
// decide which axis to calculate the zoom level with by comparing the width/height ratios
if (centeredMarkerBoundsSize.width/centeredMarkerBoundsSize.height > insetViewBoundsSize.width/insetViewBoundsSize.height)
{
x1 = centeredMarkerBoundsSize.width;
x2 = insetViewBoundsSize.width;
}
else
{
x1 = centeredMarkerBoundsSize.height;
x2 = insetViewBoundsSize.height;
}
CGFloat zoom = log2(x2 * pow(2, self.mapView.camera.zoom)/x1);
GMSCameraPosition* camera = [GMSCameraPosition cameraWithTarget:self.currentLocation.coordinate zoom:zoom];
[self.mapView animateToCameraPosition:camera];
}
這或多或少是我提出的解決方案。 – 2014-01-07 16:11:50
Saxun德魯斯的答案是真的很好。但除此之外,如果你想從任何位置計算半徑,你可以做到這一點與下面的代碼:
CLLocationCoordinate2D center = CLLocationCoordinate2DMake([currentLocationLat doubleValue],[currentLocationLong doubleValue]);
float radius = 25*1000; //radius in meters (25km)
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, radius*2, radius*2);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(region.center.latitude - region.span.latitudeDelta/2, region.center.longitude - region.span.longitudeDelta/2);
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(region.center.latitude + region.span.latitudeDelta/2, region.center.longitude + region.span.longitudeDelta/2);
GMSCoordinateBounds* bounds = [[GMSCoordinateBounds alloc]
initWithCoordinate: northEast
andCoordinate: southWest];
謝謝! 它確實有效! 最好! – LLIAJLbHOu 2015-05-25 12:02:49
你的答案做我想要的,但你可以添加更多的解釋給你的答案? – 2016-02-14 13:05:13
UPDATE
所有問題都已在最新版本的Google地圖(1.5)中修復。 [mapView_ animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds]];
可以noow來代替代碼的下方
原來的答案
[GMSCameraUpdate fitBounds]
標準方法不會在我的版本的SDK(1.2.0)中給出準確的結果。我正在使用下面的代碼而不是它。公式取自Mercator Projection。根據Google Documentation,世界經緯度限制在85度。
#import <stdlib.h>
-(void) animateBoundsNorth:(CGFloat)north West:(CGFloat)west South:(CGFloat)south East:(CGFloat)east Padding:(int)padding {
CGFloat northRad = north * M_PI/180.0;
CGFloat northProj = logf(tanf(M_PI_4 + northRad/2.0));
CGFloat southRad = south * M_PI/180.0;
CGFloat southProj = logf(tanf(M_PI_4 + southRad/2.0));
CGFloat topRad = 85.0 * M_PI/180.0;
CGFloat topProj = logf(tanf(M_PI_4 + topRad/2.0));
CGFloat zoomLat = log2f((mapView_.bounds.size.height - padding * 2) * 2 * topProj /(northProj - southProj)) - 8;
CGFloat zoomLon = log2f((mapView_.bounds.size.width - padding * 2) * 360/(east - west)) - 8;
GMSCameraUpdate *update = [GMSCameraUpdate setTarget:CLLocationCoordinate2DMake((north+south)/2.0, (west+east)/2.0) zoom:MIN(zoomLat, zoomLon)];
[mapView_ animateWithCameraUpdate:update];
}
我有一個問題。我怎樣才能得到「北」,「西」,「南」,「東」和「填充」? – 2013-12-25 07:04:32
這取決於你想要顯示的區域 – 2013-12-26 16:32:51
實際上不是,問題還沒有解決。我正在使用Google Maps v1.7.2,當我嘗試使用GMSCameraUpdate fitBounds方法時,它不起作用。我最終看到地圖放大,看到整個北美。我能夠正常工作的唯一方法是使用上面的animateWithBoundsNorth:方法。 – Marc 2014-05-01 20:57:10
正如2014年6月的,this answer是遍歷標記的給定陣列,然後相應地設置邊界的最簡單方式。
按照GoogleMaps的新版本iOS sdk 1.9.2, 我們可以使用Camera的位置,縮放級別,viewingAngle進行設置。
GMSCameraPosition* camera = [GMSCameraPosition cameraWithLatitude:28.6100
longitude:77.2300
zoom:14.0
bearing:0
viewingAngle:0.00];
self.mapView = [GMSMapView mapWithFrame:CGRectMake(0, 45, self.view.frame.size.width, self.view.frame.size.height - 45) camera:camera];
mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
mapView.delegate = self;
mapView.myLocationEnabled = YES;
mapView.mapType = kGMSTypeTerrain;
mapView.settings.compassButton = YES;
mapView.settings.myLocationButton = YES;
[self.mapView setMinZoom:10 maxZoom:18];
GMSMarker* marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(28.6100, 77.2300);
marker.title = @"New Delhi";
marker.snippet = @"Capital Of India";
marker.map = self.mapView;
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.icon = [GMSMarker markerImageWithColor:[UIColor grayColor]];
[self.view addSubview:self.mapView];
有關進一步的參考see this PDF document。
你還可以設置最小和最大縮放級別根據自己的需要:
[self.mapView setMinZoom:10 maxZoom:30];
希望這解決了這個問題。
如果你有「極左」和谷歌地圖的「接近權」的角落,你可以使用下面的代碼
var region:GMSVisibleRegion = GMSVisibleRegion()
region.nearLeft = CLLocationCoordinate2DMake(nearleflat, nearleftlong)
region.farRight = CLLocationCoordinate2DMake(fareastlat,fareastlong)
let bounds = GMSCoordinateBounds(coordinate: region.nearLeft,coordinate: region.farRight)
let camera = googleMapView.cameraForBounds(bounds, insets:UIEdgeInsetsZero)
googleMapView.camera = camera;
This鏈接也可能會顯示在SWIFT數據的緯度和經度有助於相關的事情。
這可以幫助我迅速。但我得到的兩點幾乎超出了屏幕,我怎麼能得到更多的中心兩點:nearLeft和farRight ?.謝謝你的幫助! – Dasoga 2016-01-28 00:17:58
我嘗試了很多使用其他技術 – 2016-09-06 12:51:04
//我發現這個方法對我來說
func setMapZoomToRadius(lat:Double, lng:Double, var mile:Double)
{
let center = CLLocationCoordinate2DMake(lat, lng)
let radius: Double = (mile) * 621.371
let region = MKCoordinateRegionMakeWithDistance(center, radius * 2.0, radius * 2.0)
let northEast = CLLocationCoordinate2DMake(region.center.latitude - region.span.latitudeDelta, region.center.longitude - region.span.longitudeDelta)
let southWest = CLLocationCoordinate2DMake(region.center.latitude + region.span.latitudeDelta, region.center.longitude + region.span.longitudeDelta)
print("\(region.center.longitude) \(region.span.longitudeDelta)")
let bounds = GMSCoordinateBounds(coordinate: southWest, coordinate: northEast)
let camera = googleMapView.cameraForBounds(bounds, insets:UIEdgeInsetsZero)
googleMapView.camera = camera;
}
- 1. 如何實施iOS谷歌地圖SDK
- 2. 如何使用谷歌地圖sdk獲取CGPoint的iOS位置?
- 3. iOS谷歌地圖SDK:設置maxZoom水平問題
- 4. 谷歌地圖上的繪圖區域
- 5. 設置谷歌地圖邊界與角谷歌地圖
- 6. 如何設置谷歌地圖Angular2 - 谷歌 - 地圖到2d
- 7. 谷歌地圖區域/鄰域選擇
- 8. 設置谷歌地圖顯示標記區域
- 9. iOS版谷歌地圖SDK術語
- 10. 谷歌地圖iOS SDK崩潰 - GMSBackgroundAppException
- 11. 谷歌地圖GMSMaker ICONVIEW - iOS版SDK
- 12. 如何在谷歌地圖中設置中心地圖視圖位置sdk for iOS
- 13. 谷歌地圖自動選擇區域
- 14. 谷歌地圖API:覆蓋區域
- 15. 在分區域劃分谷歌地圖
- 16. 谷歌地圖SDK iOS版:重新定位谷歌標誌
- 17. 谷歌街景使用谷歌地圖sdk爲ios
- 18. 谷歌地圖SDK的IOS 8不更新位置
- 19. 如何禁用Google地圖sdk for iOS中的谷歌鏈接?
- 20. 谷歌地圖 - iOS:旋轉設備後維護地圖位置
- 21. 谷歌地圖SDK與ios6集成
- 22. 我如何獲得谷歌地圖中定義區域/區域的頂點
- 23. 谷歌地圖iOS SDK,獲取當前位置用戶
- 24. Xamarin iOS谷歌地圖SDK - 與mapView.AddObserver異常()
- 25. 谷歌地圖Django的位置和區域
- 26. 如何檢測Polyline觸摸? - 谷歌地圖iOS SDK
- 27. 如何確保谷歌地圖iOS SDK不使用私有API?
- 28. 如何嵌入帶可變區域的谷歌地圖?
- 29. 如何獲得在谷歌地圖API的區域協調
- 30. 如何在谷歌地圖中定義出區域中的點
但是如何設置這個區域的工作? – Mecid 2013-02-25 12:49:28