2017-03-09 66 views
0

是否有任何想法如何創建浮動標記,可以選擇其位置,但保持在其中心視圖,再加上其地圖可以滾動? 我發現JavaScript版本創建浮動標記谷歌地圖iOS

function initialize() { 
 
    var crosshairShape = { 
 
    coords: [0, 0, 0, 0], 
 
    type: 'rect' 
 
    }; 
 
    var latlng = new google.maps.LatLng(54.62279178711505, -5.895538330078125); 
 
    var myOptions = { 
 
    zoom: 12, 
 
    center: latlng, 
 
    mapTypeId: google.maps.MapTypeId.SATELLITE, 
 
    mapTypeControlOptions: { 
 
     style: google.maps.MapTypeControlStyle.DROPDOWN_MENU 
 
    } 
 
    }; 
 
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
 
    var marker = new google.maps.Marker({ 
 
    map: map, 
 
    }); 
 
    document.getElementById('coordinates').innerHTML = "<b>center coordinates</b>: " + map.getCenter().toUrlValue(6) 
 
    google.maps.event.addListener(map, 'center_changed', function() { 
 
    document.getElementById('coordinates').innerHTML = "<b>center coordinates</b>: " + map.getCenter().toUrlValue(6); 
 
    }); 
 
    marker.bindTo('position', map, 'center'); 
 
} 
 
google.maps.event.addDomListener(window, 'load', initialize);
html, 
 
body, 
 
#map_canvas { 
 
    height: 500px; 
 
    width: 500px; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry,places&ext=.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script> 
 
<div id="map_canvas" style="width:750px; height:450px; border: 2px solid #3872ac;"></div> 
 
<div id="coordinates"></div>

目前已經實現了這個應用程序,但我真的沒有線索如何做到這一點。這裏是圖像

Center marker

我的觀點是,每個用戶滾動地圖,其標記不斷中心視圖,則我們可以得到其座標(GMSPlace或任何座標)。

任何幫助或線索將不勝感激。非常感謝!

回答

1

我會添加在圖形頁面的中心圖像,並會採取其座標爲用戶停止滾動。

創建的ImageView:

UIImageView *pin = [[UIImageView alloc] initWithFrame(0,0,10,10)]; 
pin.center = self.view.center; 
pin.image = [UIImage imageNamed:@"pin"]; 
[self.view addSubview:pin]; 
[self.view bringSubviewToFront:pin]; 

要獲取座標:

-(void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position 
{ 
CGPoint point = pin.center; 
CLLocationCoordinate2D coor = [mapView.projection coordinateForPoint:point]; 
CGFloat longitude = coor.longitude; 
CGFloat latitude = coor.latitude; 
} 
3

我不會它寫出來,但:

不使用此標誌在所有的;)只需使用普通的UIView添加到地圖的一個子視圖..中心。

和中心座標爲可見光區

+0

太感謝你了,@ Daij-Djan! –

2

下面的代碼應該幫助你的只是中心座標。

// Call this function from -viewdidload with valid GMSMapView input. 
- (UIImageView *)addMarkerOnMapViewCenter:(GMSMapView *)mapView 
{ 
    UIView *vwMapParentView = mapView.superview; 
    UIImageView *vwMarker = [UIImageView new]; 
    [vwMarker setFrame:(CGRect){0,0,20,40}]; 
    // Customize the Background color as per your need. 
    [vwMarker setBackgroundColor:[UIColor greenColor]]; 
    // Drop an icon named markerIcon.png in your project bundle 
    [vwMarker setImage:[UIImage imageNamed:@"markerIcon.png"]]; 
    [vwMarker setCenter:(CGPoint){vwMapParentView.center.x,(vwMapParentView.center.y - vwMarker.frame.size.height)}]; // if needed subtract navigation bar height as well as per the requirement. 
    [vwMapParentView addSubview:vwMarker]; 
    [vwMapParentView bringSubviewToFront:vwMarker]; 
    return vwMarker; 
} 
// And then,Implement this GMSMapView's delegate method. 
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position 
{ 
    CLLocationCoordinate2D target = position.target; 
    NSLog(@"%f : %f",target.latitude,target.longitude); 
} 

希望有幫助!

謝謝,Naresh。

1

由於Daij-Djan表示,不要添加爲標記。只需使用普通的UIView/UIImageView添加爲地圖中心的子視圖。然後實現此代碼谷歌地圖didChangeCameraPositionidleAtCameraPosition代表:

- (void) mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position 
{ 
    CGPoint point = mapView.center; 
    CLLocationCoordinate2D coor = [mapView.projection coordinateForPoint:point]; 
}