How to Add Custom Data to Marker (Google Maps API Swift)

How to add custom data to marker (Google Maps API SWIFT)

I created a struct that has all the data i need and put it in marker.userdata since it supports Any type.

let data = structName(lat: place.lat, long: place.long) // initialize struct
marker.userData = data

Aceess it like this:

let markerLat = (marker.userData as! structName).lat
let markerLong = (marker.userData as! structName).long

Google Map API V3: How to add Custom data to markers

As a Google Marker is a JavaScript object, you may add custom information in the form key: value, where key is a valid string. They are called object properties and can be approached in many different ways. The value can be anything legal, as simple as numbers or strings, and also functions, or even other objects. Three simple ways: in the declaration, dot notation and square brackets

var markerA = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(0, 0),
customInfo: "Marker A"
});

var markerB = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(-10, 0)
});
markerB.customInfo = "Marker B";

var markerC = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(-20, 0)
});
markerC['customInfo'] = "Marker C";

Then to retrieve it in a similar manner:

google.maps.event.addListener(markerA, 'click', function() {
alert(this.customInfo);
});

How to add custom markers from server to google map? Swift

You have to add marker icons and positions in array like this:

 let marker = GMSMarkerInfo()
marker.icon = UIImage(named: "locationPinNeon")
self.markerArray.append(marker)

Then to display markers in locations which you got from server on map you have to implement below code:

 let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: Double(self.lat)!, longitude: Double(self.long)!, zoom: 5.0)
self.mapView.camera = camera

delay(seconds: 1) { () -> () in
//fit map to markers
var bounds = GMSCoordinateBounds()
for marker in self.markerArray {
bounds = bounds.includingCoordinate(marker.position)
}
let update = GMSCameraUpdate.fit(bounds, withPadding: 100.0)
self.mapView.animate(with: update)
}

google maps iOS SDK: custom icons to be used as markers

Here is what I have done

let marker = GMSMarker()

// I have taken a pin image which is a custom image
let markerImage = UIImage(named: "mapMarker")!.withRenderingMode(.alwaysTemplate)

//creating a marker view
let markerView = UIImageView(image: markerImage)

//changing the tint color of the image
markerView.tintColor = UIColor.red

marker.position = CLLocationCoordinate2D(latitude: 28.7041, longitude: 77.1025)

marker.iconView = markerView
marker.title = "New Delhi"
marker.snippet = "India"
marker.map = mapView

//comment this line if you don't wish to put a callout bubble
mapView.selectedMarker = marker

The output is

Sample Image

And my marker image was

Sample Image

You can change your color as per your need. Also if you want something in rectange, you can just create a simple small rectangular image and use it like I did above and change the color of your need.

Or if you want a rectangle with text within it, you can just create a small UIView with some label and then convert that UIView in UIImage and can do the same thing.

//function to convert the given UIView into a UIImage
func imageWithView(view:UIView) -> UIImage {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}

Hope it helps!!



Related Topics



Leave a reply



Submit