func setMarkeronmap(isFitAllPins : Bool)
{
var myTagValue : Int = 0
var bounds = GMSCoordinateBounds()
// loop for set marker icon image
for item in self.arrFirstThirty
{
if let jsonDict = item as? NSDictionary
{
var floatLat = Double()
var floatLong = Double()
if let temp = jsonDict.value(forKey: "Latitude") as? String {
floatLat = Double ((jsonDict.value(forKey: "Latitude") as? String)!)!
floatLong = Double((jsonDict.value(forKey: "Longtitude") as? String)!)!
}
else {
floatLat = Double ((jsonDict.value(forKey: "Latitude") as? NSNumber)!)
floatLong = Double((jsonDict.value(forKey: "Longtitude") as? NSNumber)!)
}
self.Mapview.isMyLocationEnabled = true
let mapMarker = GMSMarker()
mapMarker.position = CLLocationCoordinate2DMake(floatLat
, floatLong)
bounds = bounds.includingCoordinate(mapMarker.position)
self.myImageView.image = UIImage (named: "\((jsonDict.value(forKey: "CompanyBrand") as! String).lowercased())_map_pin")
var sourceView = UIView()
sourceView = self.viewMarker
let copiedView: UIView = sourceView.copyView()
copiedView.tag = myTagValue
let image = self.image(with: copiedView)
mapMarker.icon = image
mapMarker.tracksViewChanges = true
mapMarker.map = self.Mapview
mapMarker.userData = myTagValue
mapMarker.zIndex = Int32(myTagValue)
myTagValue += 1
}
}
如果我只保留上面的代码,那么地图滚动会根据需要过于平滑。但是当我添加下面的代码时,地图滚动并不顺畅。
// loop for set marker iconView
myTagValue = 10000
for item in self.arrFirstThirty
{
if let jsonDict = item as? NSDictionary
{
var floatLat = Double()
var floatLong = Double()
if let temp = jsonDict.value(forKey: "Latitude") as? String {
floatLat = Double ((jsonDict.value(forKey: "Latitude") as? String)!)!
floatLong = Double((jsonDict.value(forKey: "Longtitude") as? String)!)!
}
else {
floatLat = Double ((jsonDict.value(forKey: "Latitude") as? NSNumber)!)
floatLong = Double((jsonDict.value(forKey: "Longtitude") as? NSNumber)!)
}
let mapMarker = GMSMarker()
mapMarker.position = CLLocationCoordinate2DMake(floatLat
, floatLong)
bounds = bounds.includingCoordinate(mapMarker.position)
Mapview.needsUpdateConstraints()
var strCurrType = String("Euro")
if Defaults[PDUserDefault.CurrencyNameEnglish] != nil && Defaults[PDUserDefault.CurrencyRate] != nil
{
strCurrType = Defaults[PDUserDefault.CurrencyNameSymbol]!
let abc = ("\((jsonDict.value(forKey: "FuelPrice") as? String)!)")
let strPrice = Float(abc.replacingOccurrences(of: ",", with: ".", options: .literal, range: nil))
var updatedPrice = Float(Defaults[PDUserDefault.CurrencyRate]!)! * strPrice!
updatedPrice = (updatedPrice * 1000).rounded() / 1000
var finalPrice = String.init(format: "%.3f", updatedPrice)
if Defaults[PDUserDefault.LanguageType] == "1"
{
finalPrice = finalPrice.replacingOccurrences(of: ",", with: ".")
}
else
{
finalPrice = finalPrice.replacingOccurrences(of: ".", with: ",")
}
self.lblmarkername.text = ("\(finalPrice) \(strCurrType!)")
let strokeTextAttributes = [
NSStrokeColorAttributeName : UIColor.init(red: 1.0/255.0, green: 123.0/255.0, blue: 146.0/255.0, alpha: 1.0),NSForegroundColorAttributeName : UIColor.white,NSStrokeWidthAttributeName : -3.0,] as [String : Any]
self.lblmarkername.attributedText = NSAttributedString(string: ("\(finalPrice) \(strCurrType!)"), attributes: strokeTextAttributes)
}
else
{
strCurrType = Defaults[PDUserDefault.CurrencyNameSymbol]!
let abc = ("\((jsonDict.value(forKey: "FuelPrice") as? String)!)")
var strmyPrice = Float(abc.replacingOccurrences(of: ",", with: ".", options: .literal, range: nil))
strmyPrice = (strmyPrice! * 1000).rounded() / 1000
var finalPrice = String.init(format: "%.3f", strmyPrice!)
if Defaults[PDUserDefault.LanguageType] == "1"
{
finalPrice = finalPrice.replacingOccurrences(of: ",", with: ".")
}
else
{
finalPrice = finalPrice.replacingOccurrences(of: ".", with: ",")
}
self.lblmarkername.text = ("\(finalPrice) \(strCurrType!)")
let strokeTextAttributes = [
NSStrokeColorAttributeName : UIColor.init(red: 1.0/255.0, green: 123.0/255.0, blue: 146.0/255.0, alpha: 1.0),NSForegroundColorAttributeName : UIColor.white,NSStrokeWidthAttributeName : -3.0,] as [String : Any]
self.lblmarkername.attributedText = NSAttributedString(string: ("\(finalPrice) \(strCurrType!)"), attributes: strokeTextAttributes)
}
var sourceView = UIView()
sourceView = self.viewMarker2
let copiedView: UIView = sourceView.copyView()
copiedView.tag = myTagValue
mapMarker.iconView = copiedView
mapMarker.tracksViewChanges = true
mapMarker.map = self.Mapview
mapMarker.userData = myTagValue
mapMarker.zIndex = Int32(myTagValue)
myTagValue += 1
markerArray.add(mapMarker)
}
}
if isFitAllPins
{
let update = GMSCameraUpdate.fit(bounds, withPadding: 80)
self.Mapview.animate(with: update)
}
}
使用第一个循环,我只添加了带图像的标记。然后在第二个循环中,我在市场图像上增加了价格,因为我必须始终保持价格不会隐藏在标记图像之后。但如果我以这种方式添加价格,地图滚动就不顺利了。那么请有人可以帮我解决这个问题吗?