IOS Swift Google Maps truncate the title - ios

I'm adding markers with the titles to the map using below code.
func showAllMarker(){
loadLocatons(){
completion in
if completion {
super.hideLoading()
self.mapView.clear()
self.circle(withRadius: self.Defradius, circleCenter: self.myLocation2D)
for point in self.mobLocationData {
if point.status == ConstantValues.activeStatus{
let marker = GMSMarker()
let latitude = point.latitude
let longitude = point.longitude
let position = CLLocationCoordinate2D(latitude: NSString(string: latitude!).doubleValue, longitude: NSString(string: longitude!).doubleValue)
marker.position = position
marker.title = point.description
marker.userData = LocationMarkerData(id: point.id!, address: point.address!)
marker.map = self.mapView
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {
let camera = GMSCameraPosition.camera(withLatitude: self.myLocation.coordinate.latitude, longitude: self.myLocation.coordinate.longitude, zoom: 10.5,bearing: 350,viewingAngle: 10)
CATransaction.begin()
CATransaction.setValue(1.5, forKey: kCATransactionAnimationDuration)
self.mapView.animate(to: camera)
CATransaction.commit()
})
}
}
}
Some times when tapped on the marker doesn't show the full title. It truncates the title. See below image.
In the google maps docs it says that only the very long messages may be truncated.
Strings that are longer than the width of the info window are
automatically wrapped over several lines. Very long messages may be
truncated.
But the title of the above marker in the image is "test". So it is not a very long title. Also there are some other markers which have more longer titles than this and they are not getting truncated. See below image
So could someone explain me what I'm doing wrong and how to correct this?

Just make the first letter of your word Capital case like this:
let outlet_name = "\(DataDict["routeName"] ?? "")".lowercased()
marker.title = outlet_name.capitalizingFirstLetter()
This magically worked for me.

Related

Using Firebase data and create Google Maps API markers on iOS

I am trying to make an iOS app, working with Google Maps API and Firebase. It should show many markers on the map.
The maps part works well. However, I want to retrieve location data (latitude, longitude) from Firebase Real-time database, then put into marker.position. (then it should create a marker, right?)
It struck many days, I still can't show a marker (but I can print correct data from Firebase database).
ref = Database.database().reference()
ref.child("locations").observeSingleEvent(of: .value) { (snapshot) in
if snapshot.exists() {
if let location = snapshot.value as? [String:Any] {
for eachLocation in location {
print("Location: \(eachLocation)")
if let locationCoordinate = eachLocation.value as? [String: Any] {
if let lavLatitude = locationCoordinate["latitude"] as? Double {
if let lavLongitude = locationCoordinate["longitude"] as? Double {
print(lavLatitude)
print(lavLongitude)
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(lavLatitude, lavLongitude)
marker.map = mapView
}
}
}
}
}
}
}
My code reference from Github: appsmall/Map-Demo.
It really weird, when I copy and replace my GoogleService-Info.plist. My app can show markers from his Firebase database!
I am wondering anything wrong in my database: Screenshot.
Of course, I searched and tried many different solutions here, but still not work, if you need more information, please tell me, thanks!
Try using Annotation instead of GMSMarker():
Replace this
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(lavLatitude, lavLongitude)
marker.map = mapView
With this
let position = CLLocationCoordinate2D(latitude: lavLatitude, longitude: lavLongitude)
let marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = mapView

How to check and load coordinate into Google Map (iOS)?

I have a list of coordinates in database, but i would like to load only the coordinates that currently in the view of Google Map and add a marker onto it. How can i do that ?
Once you get latitude and longitude, you can use the following function to add map marker and zoom to marker location :
func showMapMarker() {
self.currentLocationMapView.clear()
DispatchQueue.main.async {
let position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let marker = GMSMarker(position: position)
let bounds: GMSCoordinateBounds = GMSCoordinateBounds(coordinate: position, coordinate: position)
marker.title = "Snippet Title"
marker.map = self.currentLocationMapView
marker.snippet = "Snippet Text"
let camera = self.currentLocationMapView.camera(for: bounds, insets: UIEdgeInsets())
self.currentLocationMapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 15.0))
self.currentLocationMapView.camera = camera!
}
}

Swift loop through array

I'm trying to loop through an array with coordinates and show markers on a map with this code:
for index in 0...3 {
let latitude: Double = latCoordinate.objectAtIndex(index) as! Double
let longitude: Double = longCoordinate.objectAtIndex(index) as! Double
let position = CLLocationCoordinate2DMake(latitude, longitude)
print([latitude],[longitude])
let marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = self.mapView
}
But I just get one marker on the map, it should be 4. Why can't I see the other markers on the map?
Your code looks good for displaying the marker, but are you sure the coordinates are correct? - Just looked at the comments again and the issue is in how you're populating your array, if you only have one coordinate in your array then you're only going to see one marker on the map.
This code from my app adds all the coordinates to a GMSCoordinateBounds and then sets the GMSMapView camera to the bounds of all the coordinates - so everything should be visible on the displayed map.
func plotAll(){
let bounds = GMSCoordinateBounds.init()
for property in fetchedResultsController.fetchedObjects!
{
let capitalAsset : CapitalAsset = property as! CapitalAsset
let marker = GMSMarker.init()
marker.draggable = false
marker.snippet = capitalAsset.address
let location = CLLocationCoordinate2DMake(Double(capitalAsset.latitude!), Double(capitalAsset.longitude!))
marker.position = location
marker.map = mapView
// Update bounds to include marker
bounds.includingCoordinate(marker.position)
}
let camera = mapView.cameraForBounds(bounds, insets:UIEdgeInsetsZero)
mapView.camera = camera;
}
Hope that helps.

Fit entire Google Map in zoom level in Swift project

I have a google map with a bunch of coordinates
path.addCoordinate(CLLocationCoordinate2DMake(-37.813047, 144.959911))
path.addCoordinate(CLLocationCoordinate2DMake(-37.814895, 144.960759))
path.addCoordinate(CLLocationCoordinate2DMake(-37.814361, 144.963140))
path.addCoordinate(CLLocationCoordinate2DMake(-37.812386, 144.962239))
I would like the map to be automatically zoomed to the best level based on the points however I can't find anything relating to this.
I have this working:
var vancouver = CLLocationCoordinate2DMake(-37.813047, 144.959911)
var calgary = CLLocationCoordinate2DMake(-37.814361, 144.963140)
var bounds = GMSCoordinateBounds(coordinate: vancouver, coordinate: calgary)
var camera = viewMap.cameraForBounds(bounds, insets:UIEdgeInsetsZero)
viewMap.camera = camera
however it only accepts 2 coordinates where I may have up to 100
Thanks
You can use GMSCoordinateBounds(path:) to fit all coordinates. But it will display a world size scale if you update the camera right after your another update. So you can use dispatch_after to solve the problem.
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor();
let camera = GMSCameraPosition.cameraWithLatitude(-37.813047, longitude: -72.8561644, zoom:5)
mapView = GMSMapView.mapWithFrame(CGRectZero, camera:camera)
let marker = GMSMarker()
marker.position = camera.target
marker.snippet = "Hello World"
marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = mapView
self.view = mapView
delay(seconds: 2) { () -> () in
let path = GMSMutablePath()
path.addCoordinate(CLLocationCoordinate2DMake(37.36, -122.0))
path.addCoordinate(CLLocationCoordinate2DMake(37.45, -122.0))
path.addCoordinate(CLLocationCoordinate2DMake(37.45, -122.2))
path.addCoordinate(CLLocationCoordinate2DMake(37.36, -122.2))
path.addCoordinate(CLLocationCoordinate2DMake(37.36, -122.0))
let rectangle = GMSPolyline(path: path)
rectangle.map = self.mapView
let bounds = GMSCoordinateBounds(path: path)
self.mapView!.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 15.0))
}
}
The delay method uses the dispatch_after:
func delay(#seconds: Double, completion:()->()) {
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64( Double(NSEC_PER_SEC) * seconds ))
dispatch_after(popTime, dispatch_get_main_queue()) {
completion()
}
}
Iterate through your points. The southwest corner is the minimum latitude and longitude. The northeast corner is the maximum latitude and longitude. Once you have those two points, pass them into your cameraForBounds method.
Try finding an optimal viewing box on google maps by:
-Clicking on the north east corner of the area you think will cover the coordinates in a browser. Note the lat & long.
-Then click the south east corner in the same area which you think will enclose all the coordinates & note the lat & long.
Put the latitudes & longitudes in the respective variables of the below code:
var southWest = CLLocationCoordinate2DMake(latitide,longititude)
var northEast = CLLocationCoordinate2DMake(latitude,longitude)
var bounds = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
var camera = mapView.cameraForBounds(bounds, insets:UIEdgeInsetsZero)
mapView.camera = camera;

How to set visible region/zoom level for google maps IOS to show all markers added to the mapview

I have used Google maps in ios app, I wanna set zoom level dynamically depends on the search i made over map. Basically am adding pins by searching with city names or lat/long query. after every search am adding pins & i need to show all added markers by the recent search i made.
#IBOutlet weak var mapView: GMSMapView!
let camera = GMSCameraPosition.cameraWithLatitude(23.0793, longitude:
72.4957, zoom: 5)
mapView.camera = camera
mapView.delegate = self
mapView.myLocationEnabled = true
*** arry has dictionary object which has value of Latitude and Longitude. ***
let path = GMSMutablePath()
for i in 0..<arry.count {
let dict = arry[i] as! [String:AnyObject]
let latTemp = dict["latitude"] as! Double
let longTemp = dict["longitude"] as! Double
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: latTemp, longitude: longTemp)
marker.title = "Austrilia"
marker.appearAnimation = kGMSMarkerAnimationNone
marker.map = self.mapView
path.addCoordinate(CLLocationCoordinate2DMake(latTemp, longTemp))
}
let bounds = GMSCoordinateBounds(path: path)
self.mapView!.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))
See this answer for a simple way to iterate over a given array of markers and then set the bounds accordingly.

Resources