Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example project using Swift #26

Open
randyhbh opened this issue Oct 3, 2017 · 9 comments
Open

Example project using Swift #26

randyhbh opened this issue Oct 3, 2017 · 9 comments

Comments

@randyhbh
Copy link

randyhbh commented Oct 3, 2017

i create a swift project and import graphhopper using the xcode method, everything ok so far. There is any chance to make a full example in swift?

@randyhbh randyhbh changed the title Swift project, error importing Directions.h Example project using Swift Oct 3, 2017
@clns
Copy link
Contributor

clns commented Oct 3, 2017

Make sure you checkout the 0.8.2 tag instead of using master (git checkout 0.8.2; git submodule update --init). See #23 (comment)

@randyhbh
Copy link
Author

randyhbh commented Oct 3, 2017

Sorry was my mistake i change the isuue name and the question.

@clns
Copy link
Contributor

clns commented Oct 3, 2017

graphhopper-ios uses J2ObjC to translate GraphHopper's Java sources to Objective-C. So the actual GH sources will always be Objective-C, however they can be bridged and used from Swift.

There are plans to change this repo to include only the translated GH sources, to make it easier to include in projects. Maybe even a CocoaPod library but this is debatable (see #7 (comment)). There isn't any eta however, any contributions are appreciated.

@tnmendes
Copy link

tnmendes commented Oct 4, 2017

@randyhbh see if this help you

ignore the

callback: @escaping RCTResponseSenderBlock

I am using that because I am developing solution for React Native

private var hopper : GraphHopper!

@objc func route(_ arrCoordinates: NSArray, callback: @escaping RCTResponseSenderBlock) -> Void {
    
    self.test()
    
    DispatchQueue.global().async {
      
      //let request = GHRequest(double: 41.157964, with: -8.682544, with: 41.158344, with: -8.630827).setAlgorithmWith("astarbi")
      //let request = GHRequest(double: 41.157964, with: -8.682544, with: 41.158344, with: -8.630827)
      //let request = GHRequest(double: 44.5038082, with: 25.9881952, with: 44.479994, with: 26.0251586) Romenia
      let request = GHRequest(double: arrCoordinates[0] as! jdouble, with: arrCoordinates[1] as! jdouble, with: arrCoordinates[2] as! jdouble, with: arrCoordinates[3] as! jdouble).setVehicleWith("foot")
      //let hints = HintsMap()
      var response = GHResponse()
      response = self.hopperFunc(city: arrCoordinates[4] as! String).route(with: request)
    
      if(response?.hasErrors())!{
        
        print("Guide :: Route :: hasErrors")
      }
      
      let best : PathWrapper = (response?.getBest())!
      let points : PointList = best.getPoints()
      print("Guide :: Route :: Points :: ",points.getSize())
      var arrPoints = [Array<Any>]()
      
      for i in 0..<(points.getSize()-1){
        
        //print("Guide :: Route :: Points :: ", points.getLatitudeWith(i), " :: ", points.getLongitudeWith(i))
        arrPoints.append([points.getLatitudeWith(i), points.getLongitudeWith(i)])
      }

      callback([NSNull() ,["success" : true, "points" : arrPoints]])
    }
  }

func hopperFunc(city: String) -> GraphHopper {
    
    if(!hopperState){
      
      let path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
      let urlTemplateSetComplete = "/"+city+"/"+city+"-gh"
      let dataPath = path.appending(urlTemplateSetComplete)
      
      print("Guide :: hopperFunc :: ",dataPath )

      //let exists = FileManager.default.fileExists(atPath: (path?.absoluteString)!)
      let exists = FileManager.default.fileExists(atPath: (dataPath))
      print("Guide :: hopperFunc :: ",exists)
      
      hopper = GraphHopper.init()
      //hopper.setCHEnableWithBoolean(true)
      //hopper.setAllowWritesWithBoolean(true)
      //hopper.setEncodingManagerWith(EncodingManager.init(nsString: "car"))
      hopper.forMobile()
      hopper.load__(with: dataPath)
      hopperState = true
    }
    
    return hopper
  }

//location manager
  lazy var locationManager: CLLocationManager = {
    var _locationManager = CLLocationManager()
    _locationManager.delegate = self
    _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    _locationManager.activityType = .fitness
    _locationManager.distanceFilter = 0.1  // Movement threshold for new events
    //  _locationManager.allowsBackgroundLocationUpdates = true // allow in background
    
    return _locationManager
  }()

@randyhbh
Copy link
Author

randyhbh commented Oct 4, 2017

@tnmendes thanks for your help, use your example to compare what I had done, basically the same :), I only havea few questions from your example

let request = GHRequest(double: arrCoordinates[0] as! jdouble, with: arrCoordinates[1] as! jdouble, with: arrCoordinates[2] as! jdouble, with: arrCoordinates[3] as! jdouble).setVehicleWith("foot")

  • Why do you use the height? , with: arrCoordinates[3] as! jdouble Is it more accurate if I use it?

  • How do you get the distance from one point to another in km?

@randyhbh
Copy link
Author

randyhbh commented Oct 4, 2017

with respect to the example in swift as soon as I finish with mine, with pleasure I will realize a merge request, I will try to describe it as much as possible and to use the greater amount of functionalities that gives graphhopper :D

@tnmendes
Copy link

tnmendes commented Oct 4, 2017

Why do you use the height? , with: arrCoordinates[3] as! jdouble Is it more accurate if I use it?

@randyhbh I'm not understanding the question of height...are you talking about latitude and longitude?

How do you get the distance from one point to another in km?

I never need that information about distance but have look to:
distances-for-vehicle-routing-with-graphhopper

@randyhbh
Copy link
Author

randyhbh commented Oct 4, 2017

@randyhbh I'm not understanding the question of height...are you talking about latitude and longitude?

yes, i was seeing that you use three coordinates, i imagine that the first two are longitude and latitude, and the third one is height or z

I never need that information about distance but have look to:
distances-for-vehicle-routing-with-graphhopper

Thanks for the article

@randyhbh
Copy link
Author

randyhbh commented Oct 4, 2017

i have a dude, when i generate de data using

FILE=cuba-latest.osm.pbf JAVA_OPTS="-Xmx4096m -Xms1000m -server" ./graphhopper-ios-sample/import-sample.sh

all necesary data is genrated for vehicle ("car", "foot" or "bike")???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants