あなたは(私はそれが「私の場所」であり、あなたがあなたのマップビュー「myMapView」という名前を付けていることと仮定)ユーザの位置を示すために、[myMapView setShowsUserLocation: YES];
を使用することができます。地図アプリはありません同じように、青い点が表示されます
。ただし、ビューを変更することはありません。
すべての注釈を作成し、すべての注釈の最大値と最小経度と緯度にまたがる新たな領域を設定することにより表示されているようにビューを更新したりすることができます。コードは次のようになります。
// Get all the coordinates
NSArray *coordinates = [myMapView valueForKeyPath:@"annotations.coordinate"];
// Longitude and latitude max and min
CLLocationCoordinate2D maxCoord = {-90.0f, -180.0f};
CLLocationCoordinate2D minCoord = {90.0f, 180.0f};
for(NSValue *value in coordinates) {
CLLocationCoordinate2D coord = {0.0f, 0.0f};
// Unpack the NSValue to get the CLLocationCoordinate2D struct inside
[value getValue:&coord];
// Update max and min to
if(coord.longitude > maxCoord.longitude) {
maxCoord.longitude = coord.longitude;
}
if(coord.latitude > maxCoord.latitude) {
maxCoord.latitude = coord.latitude;
}
if(coord.longitude < minCoord.longitude) {
minCoord.longitude = coord.longitude;
}
if(coord.latitude < minCoord.latitude) {
minCoord.latitude = coord.latitude;
}
}
// Create a new region that covers all the annotations
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center.longitude = (minCoord.longitude + maxCoord.longitude)/2.0;
region.center.latitude = (minCoord.latitude + maxCoord.latitude)/2.0;
region.span.longitudeDelta = (maxCoord.longitude - minCoord.longitude);
region.span.latitudeDelta = (maxCoord.latitude - minCoord.latitude);
region = [myMapView regionThatFits:region];
// Animate to the new region
[myMapView setRegion:region animated:YES];
これは、青いユーザーの位置のドットも注釈であるために機能します。そのため、アノテーションコードのビューを更新する必要があるかもしれません。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView *view = nil;
if (annotation != mapView.userLocation) {
// Your code here...
}
return view;
}
ただし、これはユーザーの場所の更新として更新されません。マップ領域を自分で更新するコードを呼び出す必要があります。ユーザーが「マイロケーション」ボタンをタップした場合、またはコアロケーションを使用してユーザーが十分に移動したときにイベントを取得することができます。ユーザーが数百メートル移動するたびにマップ領域を更新すると、アプリケーションに応じてバッテリが多量に消費され、おそらく過度の使用になります。
乾杯デビッド、私はそれを知っていました。私はズームアウトして、場所を見ることができるようにしたい=それを四角形の一部にする。 –
自分の答えを編集した後で、必要なことをするコードを手に入れたことに気づきました。私はまだ回答に私のコードを残します。私の長いコードブロックの後の部分はおそらくあなたの質問に答えます。つまり、[myMapView setShowsUserLocation:YES]を設定した後です。ユーザーの場所は地図の注釈の間にある必要があります。ユーザーの場所を表示した後にコードを実行して地図を再入力してください。表示されない場合は、マップビューからユーザーの位置を取得し、残りの注釈の領域を使用してその上のリージョンユニオンを実行できます。 –