GMSMarker infowindow content(snippet) not updating - ios

I have an array in which each element has Latitude Longitude and some other values in it to draw marker on map. I'm using the following code to update the snippet, position and icon of GMSMarker. I'm using a UISlider to iterate through the array. When I do this the icon and position of the marker gets updated and the InfoWindow of the marker not updating. It shows only the old content. But when I deselect and select it again now it shows correct infowindow content. I have tried to use tracksInfoWindowChanges, but it's not working.
#IBAction func progressChanged(_ sender: UISlider) {
let iVal = Int(sender.value)
if iVal < progressList.count {
let str = progressList[iVal]
let arr = str.components(separatedBy: "!")
let lat = Double(arr[4])!
let lng = Double(arr[5])!
let pos = CLLocationCoordinate2DMake(lat, lng)
let fRotation = Float(arr[9])
let rotation = CGFloat(fRotation!)
let strTime = arr[0]
let idleFlag = arr[1]
let strSpeed = arr[2]
let strName = arr[3]
proMarker.position = pos
proMarker.title = strName
if idleFlag == "Y" {
proMarker.snippet = strSpeed + "!" + strTime + "!" + "idle"
proMarker.tracksInfoWindowChanges = true
let markerImage = UIImage(named: "arrow_red")
let rotated = markerImage?.imageRotatedByDegrees(rotation, flip: false)
proMarker.icon = rotated
} else {
proMarker.snippet = strSpeed + "!" + strTime + "!" + "moving"
proMarker.tracksInfoWindowChanges = true
let markerImage = UIImage(named: "arrow_green")
let rotated = markerImage?.imageRotatedByDegrees(rotation, flip: false)
proMarker.icon = rotated
}
proMarker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
let dTime = sdf5.date(from: strTime)
let sTime = sdf6.string(from: dTime!)
hTime.text = sTime
hSpeedIdle.text = strSpeed + " km/h"
}
}
Please help me solve my problem.

This is not a correct way to add lat_long, You should add marker_obj into array because each marker contain all related info, and it can help you to reduce such type of length of code.

As mentioned in a comment, it may be that you are not making new markers, or if you are, you need to remove the map from the old markers (marker.map = nil). So when you click on overlapped markers, the infowindows will sequentially appear.

Related

Polyline starts disappearing after zoom out

I have encountered a strange problem, when i zoom out the my polyline of custom coordinates starts disappearing, following is my code
func setTrackerLines(currentLoc: String, destinationLoc: String) {
let str1 = currentLoc
let LocArray1 = str1.components(separatedBy: ",")
let str2 = destinationLoc
let LocArray2 = str2.components(separatedBy: ",")
let path = GMSMutablePath()
path.add(CLLocationCoordinate2D(latitude: (LocArray1[0] as NSString).doubleValue, longitude: (LocArray1[1] as NSString).doubleValue))
path.add(CLLocationCoordinate2D(latitude: (LocArray2[0] as NSString).doubleValue, longitude: (LocArray2[1] as NSString).doubleValue))
let rectangle = GMSPolyline(path: path)
rectangle.strokeColor = UIColor.blue
rectangle.strokeWidth = 1
rectangle.map = self.myMapView
}
}
have call this method on viewdidload(), This is how i am using the code
for i in 0 ..< pointArray.count {
print("Delta: \(delta)")
if pointArray.count != (i + 1) {
self.setTrackerLines(currentLoc: pointArray[i], destinationLoc: pointArray[i + 1])
}
The result is showing in pictures. Kindly help please

Developing an ARKit app that leaves text for others to view

I am creating an iOS AR app that sets text in a specific location and leaves it there for others to view. Is there a better way to implement it than what I am doing?
Currently, I have it set so that the text is saved to Firebase and loads it by setting the nodes relative to the camera’s position. I’m wondering if there is a way to save ARAnchors in a fashion similar to what I am doing but is that possible?
My current function for saving the text to the location via a user tapping the screen:
/*
* Variables for saving the user touch
*/
var touchX : Float = 0.0
var touchY : Float = 0.0
var touchZ : Float = 0.0
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// will be used for getting the text
let textNode = SCNNode()
var writing = SCNText()
// gets the user’s touch upon tapping the screen
guard let touch = touches.first else {return}
let result = sceneView.hitTest(touch.location(in: sceneView), types: [ARHitTestResult.ResultType.featurePoint])
guard let hitResult = result.last else {return}
let hitTransform = SCNMatrix4.init(hitResult.worldTransform)
let hitVector = SCNVector3Make(hitTransform.m41, hitTransform.m42, hitTransform.m43)
// saves X, Y, and Z coordinates of touch relative to the camera
touchX = hitTransform.m41
touchY = hitTransform.m42
touchZ = hitTransform.m43
// Was thinking of adding the ability to change colors. Probably can skip next seven lines
var colorArray = [UIColor]()
colorArray.append(UIColor.red)
writing = SCNText(string: input.text, extrusionDepth: 1)
material.diffuse.contents = colorArray[0]
writing.materials = [material]
// modifies the node’s position and size
textNode.scale = SCNVector3(0.01, 0.01, 0.01)
textNode.geometry = writing
textNode.position = hitVector
sceneView.scene.rootNode.addChildNode(textNode)
// last few lines save the info to Firebase
let values = ["X" : touchX, "Y" : touchY, "Z" : touchZ, "Text" : input.text!] as [String : Any]
let childKey = reference.child("Test").childByAutoId().key
if input.text != nil && input.text != "" {
let child = reference.child("Test").child(childKey!)
child.updateChildValues(values)
} else {
let child = reference.child("Test").child(childKey!)
child.updateChildValues(values)
} // if
} // override func
/*
* Similar to the previous function but used in next function
*/
func placeNode(x: Float, y: Float, z: Float, text: String) -> Void {
let textNode = SCNNode()
var writing = SCNText()
let hitVector = SCNVector3Make(x, y, z)
touchX = x
touchY = y
touchZ = z
var colorArray = [UIColor]()
colorArray.append(UIColor.red)
writing = SCNText(string: text, extrusionDepth: 1)
material.diffuse.contents = colorArray[0]
writing.materials = [material]
textNode.scale = SCNVector3(0.01, 0.01, 0.01)
textNode.geometry = writing
textNode.position = hitVector
sceneView.scene.rootNode.addChildNode(textNode)
} // func
/*
* This next function is used in my viewDidLoad to load the data
*/
func handleData() {
reference.child("Test").observeSingleEvent(of: .value, with: { (snapshot) in
if let result = snapshot.children.allObjects as? [DataSnapshot] {
for child in result {
let xCoord = Float(truncating: child.childSnapshot(forPath: "X").value as! NSNumber)
let yCoord = Float(truncating: child.childSnapshot(forPath: "Y").value as! NSNumber)
let zCoord = Float(truncating: child.childSnapshot(forPath: "Z").value as! NSNumber)
let inscription = child.childSnapshot(forPath: "Text").value
self.placeNode(x: xCoord , y: yCoord , z: zCoord , text: inscription as! String)
} // for
} // if
}) // reference
} // func
I have looked into a few things such as ARCore but that looks like it uses Objective-C. I’ve made this app in Swift and I am not sure if I can incorporate ARCore with how I have implemented my current application.
Do I just need to get over it and learn Objective-C? Can I still work with what I have?
I think that ARCore anchors are only available for 24 hours, so that could be a problem.
You probably need to use ARKit2.0's ARWorldMap and save it as data on firebase for others to see the text in the same place, otherwise you are assuming in your code that future users will start their AR session in the exact same position and direction as the person who left the text. You probably need to use core location first to see where in the world the user is.

Cannot assign value of type '[String]' to type 'String? Swift 2

I am receiving this error message "Cannot assign value of type '[String]' to type 'String?'" associated with this line "point.title = r" and this line "point.subtitle = z". I've tried lots of things like place.description which makes the entire array a string...
//I have multiple arrays, lat, long and pass besides place.
var place = [String]()
//I have four of the chunks of code for each array
let json = try NSJSONSerialization.JSONObjectWithData(jSONData, options: .AllowFragments)
if let dataFile3 = json["data"] as? [[String: AnyObject]] {
for c in dataFile3 {
if let placeName = c["PlaceName"] {
place.append((placeName as? String)!)
for var (i,x) in zip(lat, long) {
for _ in place {
for _ in pass {
print(i)
print(x)
//print(i + ", " + x)
// String to double conversion, can I do this when I set the array as a string?
let lati = NSString(string: i).doubleValue
let longi = NSString(string: x).doubleValue
//I have a list of values array so I need to have a for loop that inputs the value over and over again.
var point = MGLPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)
point.title = place
point.subtitle = pass
mapView.addAnnotation(point)
}}}
//var i and x for loop
}
//viewDidLoad
}
//creates the markers popup for each point with data
func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
// Always try to show a callout when an annotation is tapped.
return true
}
How do I fix the error above?
EDIT:
With Santosh's help this is what I changed...
let point = MGLPointAnnotation()
var i = lat
var x = long
//lat and long is served and then all the r,z points, then another lat, long...need to fix this and it may work.
for (i,x) in zip(lat, long) {
for aPlace in place {
for aPass in pass {
print(i)
print(x)
// String to double conversion, can I do this when I set the array as a string?
let lati = NSString(string: i).doubleValue
let longi = NSString(string: x).doubleValue
point.coordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)
point.title = aPlace
print(aPlace)
point.subtitle = aPass
print(aPass)
self.mapView.addAnnotation(point)
}}}
This is running correctly now or at least reporting the correct values but the loop just keeps a loopin...
Try this:
//I have four of the chunks of code for each array
let json = try NSJSONSerialization.JSONObjectWithData(jSONData, options: .AllowFragments)
if let dataFile3 = json["data"] as? [[String: AnyObject]] {
for c in dataFile3 {
if let placeName = c["PlaceName"] {
place.append((placeName as? String)!)
for var (i,x) in zip(lat, long) {
for aPlace in place {
for aPass in pass {
print(i)
print(x)
//print(i + ", " + x)
// String to double conversion, can I do this when I set the array as a string?
let lati = NSString(string: i).doubleValue
let longi = NSString(string: x).doubleValue
//I have a list of values array so I need to have a for loop that inputs the value over and over again.
var point = MGLPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)
point.title = aPlace
point.subtitle = aPass
mapView.addAnnotation(point)
}}}
//var i and x for loop
}
//viewDidLoad
}
}

Swift: How to convert a list of String to a list of CGPoint?

I am a complete newbie to Swift and didn't found anything on the web. How do I convert a string formatted this way:
let str:String = "0,0 624,0 624,-48 672,-48 672,192"
to an array of CGPoint's ?
This solution uses the CGPointFromString function provided by iOS.
import UIKit
let res = str
.components(separatedBy: " ")
.map { CGPointFromString("{\($0)}") }
I don't know, something like this?
let str:String = "0,0 624,0 624,-48 672,-48 672,192"
let pointsStringArray = str.componentsSeparatedByString(" ")
var points = [CGPoint]()
for pointString in pointsStringArray {
let xAndY = pointString.componentsSeparatedByString(",")
let xString = xAndY[0]
let yString = xAndY[1]
let x = Double(xString)!
let y = Double(yString)!
let point = CGPoint(x: x, y: y)
points.append(point)
}
print(points)
It's unsafe, of course, and doesn't handle all conditions. But this should take you in the right direction.
Here is a more functional way. Needs error checking added.
import Foundation
let str = "0,0 624,0 624,-48 672,-48 672,192"
let pointStrings = str.characters //get the character view
.split{$0 == " "} //split the pairs by spaces
.map(String.init) //convert the character views to new Strings
let points : [CGPoint] = pointStrings.reduce([]){ //reduce into a new array
let pointStringPair = $1.characters
.split{$0 == ","} //split pairs by commas
.map(String.init) //convert the character views to new Strings
let x = CGFloat(Float(pointStringPair[0])!) //get the x
let y = CGFloat(Float(pointStringPair[1])!) //get the y
return $0 + [CGPoint(x: x, y: y)] //append the new point to the accumulator
}
print(points)

Usage for plotAreaViewPointForPlotPoint in CorePlot 1.5.1

I'm trying to add something like UIPopoverController into the barPlot with coreplot 1.5.1 and swift. Like this one Core Plot: How to present popover from a bar selected by the user
So we need to know the point where the selected bar is ,But looks like some functions are different, like plotAreaViewPointForPlotPoint. In 1.5.1, there are two parameters:
And I'm try to find the right way to use it but failed, here is my code:
func barPlot(plot: CPTBarPlot!, barWasSelectedAtRecordIndex idx: UInt, withEvent event: UIEvent!) {
// Remove all the annotations
graphView.hostedGraph.plotAreaFrame.plotArea.removeAllAnnotations()
// Setup a style for the annotation
let hitAnnotationTextStyle = CPTMutableTextStyle.textStyle() as! CPTMutableTextStyle
hitAnnotationTextStyle.color = CPTColor.whiteColor()
hitAnnotationTextStyle.fontSize = 12.0;
hitAnnotationTextStyle.fontName = FONT_HEITI;
// Determine point of symbol in plot coordinates
let anchorPoint = [Int(idx),0]
// Add annotation
// First make a string for the y value
let string = "\(idx),values is:\(mArray.objectAtIndex(Int(idx)))"
// Now add the annotation to the plot area
let textLayer = CPTTextLayer(text: string, style: hitAnnotationTextStyle)
// popview, DxPopover is something like UIPopover Controller in iPhone
var popView = DXPopover()
annotationLabel.text = string
var pointers = [NSDecimal](count: 2, repeatedValue: CPTDecimalFromUnsignedInteger(0))
pointers[0] = CPTDecimalFromUnsignedInteger(idx)
var plotPointer: UnsafeMutablePointer<NSDecimal> = UnsafeMutablePointer<NSDecimal>.alloc(pointers.count)
plotPointer.initializeFrom(pointers)
var popPoint = graphView.hostedGraph.defaultPlotSpace.plotAreaViewPointForPlotPoint(plotPointer, numberOfCoordinates: idx)
popView.showAtPoint(popPoint, popoverPostion: DXPopoverPosition.Down, withContentView: self.annotationView, inView: graphView)
// selectedBarAnnotation = CPTPlotSpaceAnnotation(plotSpace: graphView.hostedGraph.defaultPlotSpace, anchorPlotPoint: anchorPoint)
// selectedBarAnnotation!.contentLayer = textLayer
// selectedBarAnnotation!.displacement = CGPointMake(0.0, -15.0)
// graphView.hostedGraph.plotAreaFrame.plotArea.addAnnotation(selectedBarAnnotation)
}
And it will crush at this line:
var popPoint = graphView.hostedGraph.defaultPlotSpace.plotAreaViewPointForPlotPoint(plotPointer, numberOfCoordinates: idx)
SO, HOW CAN I GET THE RIGHT CGPOINT?
Thanks very much!
=================EDIT===================
I change my codes to this, and can get the right point, Thanks to #Bannings
var popView = DXPopover()
annotationLabel.text = string
var pointers = [NSDecimal](count: 2, repeatedValue: CPTDecimalFromUnsignedInteger(0))
let plotXvalue = self.numberForPlot(plot, field: UInt(CPTScatterPlotFieldX.value), recordIndex: idx)
pointers[0] = CPTDecimalFromFloat(plotXvalue.floatValue)
println("\(CPTDecimalFromUnsignedInteger(idx))")
let plotspace = graphView.hostedGraph.defaultPlotSpace
println("\(plotspace.numberOfCoordinates)")
var popPoint = plotspace.plotAreaViewPointForPlotPoint(&pointers, numberOfCoordinates: plotspace.numberOfCoordinates)
popView.showAtPoint(popPoint, popoverPostion: DXPopoverPosition.Down, withContentView: self.annotationView, inView: graphView)
Try this:
var popPoint = graphView.hostedGraph.defaultPlotSpace.plotAreaViewPointForPlotPoint(&pointers, numberOfCoordinates: idx)

Resources