GMSMarker icon from center (iOS)

北城余情 提交于 2019-12-18 11:41:50

问题


I just switched from Apple Maps to Google Maps. An issue that I can't seem to find an answer to is how do you make the icon for a GMSMarker to start from the center rather then from the bottom of the image.

An example of what I mean is the current location dot icon starts centered at the coordinates it is meant to express. However GMSMarkers icons start from the bottom of the icon.


回答1:


You can change the start position of your marker icon with the property groundAnchor.

Google Maps SDK for iOS documentation:

The ground anchor specifies the point in the icon image that is anchored to the marker's position on the Earth's surface. This point is specified within the continuous space [0.0, 1.0] x [0.0, 1.0], where (0,0) is the top-left corner of the image, and (1,1) is the bottom-right corner.

Example:

The below example rotates the marker 90°. Setting the groundAnchor property to 0.5,0.5 causes the marker to be rotated around its center, instead of its base.

CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
CLLocationDegrees degrees = 90;
GMSMarker *london = [GMSMarker markerWithPosition:position];
london.groundAnchor = CGPointMake(0.5, 0.5);
london.rotation = degrees;
london.map = mapView_;



回答2:


I figured out how to do it after reading Google Maps Documentation very closely. I believe this is how it was intended to be done.

UIImage *markerIcon = [UIImage imageNamed:@"markericon.png"];
markerIcon = [markerIcon imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, 0, (markerIcon.size.height/2), 0)];
self.marker.icon = markerIcon;


来源:https://stackoverflow.com/questions/28955976/gmsmarker-icon-from-center-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!