如何使用json和swift 4.1在谷歌地图上添加多个标记?

时间:2018-05-23 07:58:50

标签: ios objective-c json swift4.1

使用swift 4.1在谷歌地图上不显示多个标记我在控制台上连接JSON和结果显示但我如何使用谷歌地图连接纬度和经度。

let jsonUrl = URL(string: "http://assetlinkasia.no-ip.biz:8001/hf_tracker/api/userdevices.php?accesskey=12345&user_id=1")            
    if let url = jsonUrl {
        let data = NSData(contentsOf: url)
        if let data = data {
            do{
                let jsonObject = try JSONSerialization.jsonObject(with: data as Data, options: .allowFragments)                    
                if let object = jsonObject as? [NSString: AnyObject]{
                    if let allDevices = object["data"] as? [[NSString: AnyObject]]{                            
                        print("Successfull")
                        print(allDevices)                            
                    }
                }
            }catch{
                print("Error Eccurred")
            }
        }
    }

这是我的谷歌地图代码:

    let getLongijson = Double("67.0011")
    let getlatijson = Double("24.8607")

    let camera = GMSCameraPosition.camera(withLatitude: getlatijson!, longitude: getLongijson!, zoom: 13.0)

    let mapView = GMSMapView.map(withFrame: view.bounds, camera: camera)
    self.view = mapView

   // meanView.addSubview(mapView)

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: getlatijson!, longitude: getLongijson!)
    marker.infoWindowAnchor = CGPoint(x:0.10, y:0.10)
    marker.title = HeadingName
    marker.snippet = Countory
    marker.icon = UIImage(named: "markers")
    marker.map = mapView

    //User Location
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()

   // self.menuFunction()

    mapView.delegate = self

1 个答案:

答案 0 :(得分:2)

试试这个

<强>目标c

     count = (int)locatorObject.LocationList.count; 
     for(int i=0;i< count;i++)
            {
            LocationDetails *locator = locatorObject.LocationList[i];
            CLLocationCoordinate2D position = CLLocationCoordinate2DMake([locator.Lat floatValue],[locator.Long floatValue]);
            GMSMarker *marker = [GMSMarker markerWithPosition:position];
            marker.title = locator.Address;
            marker.map = _googleMapView;
            }

<强>迅速

 for i in 0..<count {
        var locator: LocationDetails? = locatorObject.locationList[i]
        var position: CLLocationCoordinate2D = CLLocationCoordinate2DMake(locator?.lat, locator?.long)
        var marker = GMSMarker(position: position)
        marker.title = locator?.address
        marker.map = googleMapView
    }