Showing multiple location using 'MKMapView'












0















I am trying to show multiple locations which saved in Mysql using the code below. The data is loading but I have no idea how to show multiple locations depending on latitude and longitude.
Mysql is connected to application via PHP file.
Here is my code, the part which I called from NSObject:



func downloadItems() {
// the download function

// return the nsuserdefaults which hold the lati and longi from the notification table
UserDefaults.standard.string(forKey: "test");
let myUrl = UserDefaults.standard.string(forKey: "test");


let urlPath: String = myUrl!
let url: URL = URL(string: urlPath)!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)

let task = defaultSession.dataTask(with: url) { (data, response, error) in

if error != nil {
print("Failed to download data")
}else {
print("Data downloaded")
self.parseJSON(data!)
}

}

task.resume()

}

func parseJSON(_ data:Data) {

var jsonResult = NSArray()

do{
jsonResult = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray



} catch let error as NSError {
print(error)

}

var jsonElement = NSDictionary()
let locations = NSMutableArray()

for i in 0 ..< jsonResult.count
{

jsonElement = jsonResult[i] as! NSDictionary

let location = LocationModel()

//the following insures none of the JsonElement values are nil through optional binding
if let evIdL = jsonElement["id"] as? String,
let evUserNameL = jsonElement["username"] as? String,
let evNotikindL = jsonElement["notikind"] as? String,
let evLatiL = jsonElement["lati"] as? String,
let evLongiL = jsonElement["longi"] as? String,
let evLocatL = jsonElement["locat"] as? String,
let evTimedateL = jsonElement["timedate"] as? String,
let evDistanceL = jsonElement["distance"] as? String

{

location.evId = evIdL
location.evUsername = evUserNameL
location.evNotikind = evNotikindL
location.evLati = evLatiL
location.evLongi = evLongiL
location.evLocat = evDistanceL
location.evTimedate = evTimedateL
location.evDisatnce = evDistanceL
location.evLocat = evLocatL

}

locations.add(location)

}

DispatchQueue.main.async(execute: { () -> Void in

self.delegate.itemsDownloaded(items: locations)


})
}


}



I have no idea how to show few location on map.










share|improve this question

























  • You can google addannotation mapkit

    – Kosuke Ogawa
    Nov 26 '18 at 5:52













  • @KosukeOgawa Yes, but how can I convert the data to nsarray and load the map

    – Ahmed
    Nov 26 '18 at 5:57
















0















I am trying to show multiple locations which saved in Mysql using the code below. The data is loading but I have no idea how to show multiple locations depending on latitude and longitude.
Mysql is connected to application via PHP file.
Here is my code, the part which I called from NSObject:



func downloadItems() {
// the download function

// return the nsuserdefaults which hold the lati and longi from the notification table
UserDefaults.standard.string(forKey: "test");
let myUrl = UserDefaults.standard.string(forKey: "test");


let urlPath: String = myUrl!
let url: URL = URL(string: urlPath)!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)

let task = defaultSession.dataTask(with: url) { (data, response, error) in

if error != nil {
print("Failed to download data")
}else {
print("Data downloaded")
self.parseJSON(data!)
}

}

task.resume()

}

func parseJSON(_ data:Data) {

var jsonResult = NSArray()

do{
jsonResult = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray



} catch let error as NSError {
print(error)

}

var jsonElement = NSDictionary()
let locations = NSMutableArray()

for i in 0 ..< jsonResult.count
{

jsonElement = jsonResult[i] as! NSDictionary

let location = LocationModel()

//the following insures none of the JsonElement values are nil through optional binding
if let evIdL = jsonElement["id"] as? String,
let evUserNameL = jsonElement["username"] as? String,
let evNotikindL = jsonElement["notikind"] as? String,
let evLatiL = jsonElement["lati"] as? String,
let evLongiL = jsonElement["longi"] as? String,
let evLocatL = jsonElement["locat"] as? String,
let evTimedateL = jsonElement["timedate"] as? String,
let evDistanceL = jsonElement["distance"] as? String

{

location.evId = evIdL
location.evUsername = evUserNameL
location.evNotikind = evNotikindL
location.evLati = evLatiL
location.evLongi = evLongiL
location.evLocat = evDistanceL
location.evTimedate = evTimedateL
location.evDisatnce = evDistanceL
location.evLocat = evLocatL

}

locations.add(location)

}

DispatchQueue.main.async(execute: { () -> Void in

self.delegate.itemsDownloaded(items: locations)


})
}


}



I have no idea how to show few location on map.










share|improve this question

























  • You can google addannotation mapkit

    – Kosuke Ogawa
    Nov 26 '18 at 5:52













  • @KosukeOgawa Yes, but how can I convert the data to nsarray and load the map

    – Ahmed
    Nov 26 '18 at 5:57














0












0








0








I am trying to show multiple locations which saved in Mysql using the code below. The data is loading but I have no idea how to show multiple locations depending on latitude and longitude.
Mysql is connected to application via PHP file.
Here is my code, the part which I called from NSObject:



func downloadItems() {
// the download function

// return the nsuserdefaults which hold the lati and longi from the notification table
UserDefaults.standard.string(forKey: "test");
let myUrl = UserDefaults.standard.string(forKey: "test");


let urlPath: String = myUrl!
let url: URL = URL(string: urlPath)!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)

let task = defaultSession.dataTask(with: url) { (data, response, error) in

if error != nil {
print("Failed to download data")
}else {
print("Data downloaded")
self.parseJSON(data!)
}

}

task.resume()

}

func parseJSON(_ data:Data) {

var jsonResult = NSArray()

do{
jsonResult = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray



} catch let error as NSError {
print(error)

}

var jsonElement = NSDictionary()
let locations = NSMutableArray()

for i in 0 ..< jsonResult.count
{

jsonElement = jsonResult[i] as! NSDictionary

let location = LocationModel()

//the following insures none of the JsonElement values are nil through optional binding
if let evIdL = jsonElement["id"] as? String,
let evUserNameL = jsonElement["username"] as? String,
let evNotikindL = jsonElement["notikind"] as? String,
let evLatiL = jsonElement["lati"] as? String,
let evLongiL = jsonElement["longi"] as? String,
let evLocatL = jsonElement["locat"] as? String,
let evTimedateL = jsonElement["timedate"] as? String,
let evDistanceL = jsonElement["distance"] as? String

{

location.evId = evIdL
location.evUsername = evUserNameL
location.evNotikind = evNotikindL
location.evLati = evLatiL
location.evLongi = evLongiL
location.evLocat = evDistanceL
location.evTimedate = evTimedateL
location.evDisatnce = evDistanceL
location.evLocat = evLocatL

}

locations.add(location)

}

DispatchQueue.main.async(execute: { () -> Void in

self.delegate.itemsDownloaded(items: locations)


})
}


}



I have no idea how to show few location on map.










share|improve this question
















I am trying to show multiple locations which saved in Mysql using the code below. The data is loading but I have no idea how to show multiple locations depending on latitude and longitude.
Mysql is connected to application via PHP file.
Here is my code, the part which I called from NSObject:



func downloadItems() {
// the download function

// return the nsuserdefaults which hold the lati and longi from the notification table
UserDefaults.standard.string(forKey: "test");
let myUrl = UserDefaults.standard.string(forKey: "test");


let urlPath: String = myUrl!
let url: URL = URL(string: urlPath)!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)

let task = defaultSession.dataTask(with: url) { (data, response, error) in

if error != nil {
print("Failed to download data")
}else {
print("Data downloaded")
self.parseJSON(data!)
}

}

task.resume()

}

func parseJSON(_ data:Data) {

var jsonResult = NSArray()

do{
jsonResult = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray



} catch let error as NSError {
print(error)

}

var jsonElement = NSDictionary()
let locations = NSMutableArray()

for i in 0 ..< jsonResult.count
{

jsonElement = jsonResult[i] as! NSDictionary

let location = LocationModel()

//the following insures none of the JsonElement values are nil through optional binding
if let evIdL = jsonElement["id"] as? String,
let evUserNameL = jsonElement["username"] as? String,
let evNotikindL = jsonElement["notikind"] as? String,
let evLatiL = jsonElement["lati"] as? String,
let evLongiL = jsonElement["longi"] as? String,
let evLocatL = jsonElement["locat"] as? String,
let evTimedateL = jsonElement["timedate"] as? String,
let evDistanceL = jsonElement["distance"] as? String

{

location.evId = evIdL
location.evUsername = evUserNameL
location.evNotikind = evNotikindL
location.evLati = evLatiL
location.evLongi = evLongiL
location.evLocat = evDistanceL
location.evTimedate = evTimedateL
location.evDisatnce = evDistanceL
location.evLocat = evLocatL

}

locations.add(location)

}

DispatchQueue.main.async(execute: { () -> Void in

self.delegate.itemsDownloaded(items: locations)


})
}


}



I have no idea how to show few location on map.







ios swift mkmapview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 12:56









wvteijlingen

8,52612544




8,52612544










asked Nov 26 '18 at 3:25









AhmedAhmed

84




84













  • You can google addannotation mapkit

    – Kosuke Ogawa
    Nov 26 '18 at 5:52













  • @KosukeOgawa Yes, but how can I convert the data to nsarray and load the map

    – Ahmed
    Nov 26 '18 at 5:57



















  • You can google addannotation mapkit

    – Kosuke Ogawa
    Nov 26 '18 at 5:52













  • @KosukeOgawa Yes, but how can I convert the data to nsarray and load the map

    – Ahmed
    Nov 26 '18 at 5:57

















You can google addannotation mapkit

– Kosuke Ogawa
Nov 26 '18 at 5:52







You can google addannotation mapkit

– Kosuke Ogawa
Nov 26 '18 at 5:52















@KosukeOgawa Yes, but how can I convert the data to nsarray and load the map

– Ahmed
Nov 26 '18 at 5:57





@KosukeOgawa Yes, but how can I convert the data to nsarray and load the map

– Ahmed
Nov 26 '18 at 5:57












1 Answer
1






active

oldest

votes


















1














try this code....



 var locations = NSMutableArray()
var mapView = GMSMapView()

for i in 0..< location.count{
let obj = location[i]
lat = obj["lati"] as? Double
lng = obj["longi"] as? Double
let markerPoint = GMSMarker()
markerPoint.position = CLLocationCoordinate2D(latitude: lat!, longitude: lng!)
markerPoint.iconView = self.avtarImage() // Your image name
markerPoint.map = mapView // your mapview object name
markerPoint.zIndex = Int32(i)
markerPoint.infoWindowAnchor = CGPoint(x: 0, y: 0)
markerPoint.accessibilityLabel = String(format: "%d", i)

}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474416%2fshowing-multiple-location-using-mkmapview%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    try this code....



     var locations = NSMutableArray()
    var mapView = GMSMapView()

    for i in 0..< location.count{
    let obj = location[i]
    lat = obj["lati"] as? Double
    lng = obj["longi"] as? Double
    let markerPoint = GMSMarker()
    markerPoint.position = CLLocationCoordinate2D(latitude: lat!, longitude: lng!)
    markerPoint.iconView = self.avtarImage() // Your image name
    markerPoint.map = mapView // your mapview object name
    markerPoint.zIndex = Int32(i)
    markerPoint.infoWindowAnchor = CGPoint(x: 0, y: 0)
    markerPoint.accessibilityLabel = String(format: "%d", i)

    }





    share|improve this answer




























      1














      try this code....



       var locations = NSMutableArray()
      var mapView = GMSMapView()

      for i in 0..< location.count{
      let obj = location[i]
      lat = obj["lati"] as? Double
      lng = obj["longi"] as? Double
      let markerPoint = GMSMarker()
      markerPoint.position = CLLocationCoordinate2D(latitude: lat!, longitude: lng!)
      markerPoint.iconView = self.avtarImage() // Your image name
      markerPoint.map = mapView // your mapview object name
      markerPoint.zIndex = Int32(i)
      markerPoint.infoWindowAnchor = CGPoint(x: 0, y: 0)
      markerPoint.accessibilityLabel = String(format: "%d", i)

      }





      share|improve this answer


























        1












        1








        1







        try this code....



         var locations = NSMutableArray()
        var mapView = GMSMapView()

        for i in 0..< location.count{
        let obj = location[i]
        lat = obj["lati"] as? Double
        lng = obj["longi"] as? Double
        let markerPoint = GMSMarker()
        markerPoint.position = CLLocationCoordinate2D(latitude: lat!, longitude: lng!)
        markerPoint.iconView = self.avtarImage() // Your image name
        markerPoint.map = mapView // your mapview object name
        markerPoint.zIndex = Int32(i)
        markerPoint.infoWindowAnchor = CGPoint(x: 0, y: 0)
        markerPoint.accessibilityLabel = String(format: "%d", i)

        }





        share|improve this answer













        try this code....



         var locations = NSMutableArray()
        var mapView = GMSMapView()

        for i in 0..< location.count{
        let obj = location[i]
        lat = obj["lati"] as? Double
        lng = obj["longi"] as? Double
        let markerPoint = GMSMarker()
        markerPoint.position = CLLocationCoordinate2D(latitude: lat!, longitude: lng!)
        markerPoint.iconView = self.avtarImage() // Your image name
        markerPoint.map = mapView // your mapview object name
        markerPoint.zIndex = Int32(i)
        markerPoint.infoWindowAnchor = CGPoint(x: 0, y: 0)
        markerPoint.accessibilityLabel = String(format: "%d", i)

        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 7:40









        ayu guptaayu gupta

        263




        263






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474416%2fshowing-multiple-location-using-mkmapview%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks