graphhopper-ios
graphhopper-ios copied to clipboard
Example project using Swift
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?
Make sure you checkout the 0.8.2 tag instead of using master (git checkout 0.8.2; git submodule update --init). See https://github.com/graphhopper/graphhopper-ios/issues/23#issuecomment-323340504
Sorry was my mistake i change the isuue name and the question.
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 https://github.com/graphhopper/graphhopper-ios/issues/7#issuecomment-286578009). There isn't any eta however, any contributions are appreciated.
@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
}()
@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! jdoubleIs it more accurate if I use it? -
How do you get the distance from one point to another in km?
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
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 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
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")???