由于本次項目 需要在國外使用,所以最后選取的是使用google地圖,google的地圖怎么導入工程這里就不說了伤疙,網上有很多,官網也給出了cocoapods的例子辆影⊥较瘢可能你需要考慮被墻的原因,我這里是使用framework導入的方式蛙讥,地圖版本 GoogleMaps.1.13.2锯蛀。
這里說的是一個關于谷歌地圖上畫圓的方法,并且符合地圖的縮放層級次慢,圓在地圖上的半徑 旁涤,符合自己設置的公里數(shù)半徑翔曲。
使用百度和高德地圖的時候,都有簡單的畫圓方法劈愚,but,google沒有瞳遍,這就坑了,由于我們需要設置一個地理圍欄菌羽,這個是需要顯示出來的掠械,中心店和半徑,以及范圍算凿。所以這些東西在地圖上畫出來的時候需要準確份蝴。
接下來直接說實現(xiàn)方式:
首先。google 有提供一個畫任意多邊形的類 GMSPolygon氓轰,看樣子我們就從這個類入手婚夫。
思路:1、取得圓中心點(界面點) ->2署鸡、使用半徑獲取圓周上的點(界面點案糙,這里我們只能畫一個無限接近圓的多邊形)->3、界面點影射到地圖的經緯度點->4靴庆、繪畫
思路很簡單时捌,獲取中心和半徑 這個自己設置, 這里的關鍵在于 如何將界面的點影射到GMSMaps 上炉抒。索性 有這個屬性
/**
* Returns a GMSProjection object that you can use to convert between screen
* coordinates and latitude/longitude coordinates.
*
* This is a snapshot of the current projection, and will not automatically
* update when the camera moves. It represents either the projection of the last
* drawn GMSMapView frame, or; where the camera has been explicitly set or the
* map just created, the upcoming frame. It will never be nil.
*/
@property(nonatomic, readonly) GMSProjection *projection;
這個屬性解決我們地圖到界面的影射關系奢讨,最開始 我是沒找的的,自己計算焰薄,還要考慮縮放層級拿诸,真是苦,所以先看看 屬性 還是有好處的塞茅。
@interface GMSProjection?
- (CGPoint)pointForCoordinate:(CLLocationCoordinate2D)coordinate;
- (CLLocationCoordinate2D)coordinateForPoint:(CGPoint)point;
使用的也就這二個方法 1地圖->界面點 ? ? ? 2 界面點->經緯度
OK,影射問題解決了亩码,還有個問題就是計算圓周上的點,
這里直接給大家一個公式:
circle_x = center_x + r* cos(PI);
circle_y = center_y + r* sin(PI);
我這里是取得60 個點(注意這里的半徑是米)野瘦,代碼如下描沟,包括畫圓所有方法都在這里。
GMSMutablePath * path = [[GMSMutablePath alloc]init];
CGFloat xx = 0;
CGFloat yy = 0;
CLLocationCoordinate2D ll;
for (int i = 1; i <= 60; i+=1) { //計算圓周上的60個點
xx = point.x + radius*cos(M_PI*2 * (i/60.0));
yy = point.y + radius*sin(M_PI*2 * (i/60.0));
ll = [mapsView.projection coordinateForPoint:CGPointMake(xx, yy)]; //界面點影射到地圖的經緯度
[path addCoordinate:ll];
}
if (!polygon) {
polygon = [GMSPolygon polygonWithPath:path];
}else{
[polygon setPath:path];
}
polygon.fillColor = [ThemeColor(@"ihomeStyle") colorWithAlphaComponent:.4];
polygon.strokeColor = ThemeColor(@"ihomeStyle");
polygon.strokeWidth = 1;
polygon.map = mapsView;
最后鞭光,由于地球是橢圓形吏廉,所以在地圖上,如果你的半徑太大惰许,那么出來的是橢圓 而不是圓席覆, 這個是地圖針對地球球面做的優(yōu)化。實際給的是圓啡省,只是由于球面拉伸成了橢圓娜睛。