how to display generated message in the console in flutter ? - printing

hello I tried to extract longitude and latidude message printed in the console, I use map view plugin https://pub.dartlang.org/packages/map_view. when I tap on the map a printed longitude and latitude is generated in the console, the function use is:
Receive map touches
mapView.onMapTapped
.listen((location) => print("Touched location $location"));
But I want to read the longitude and latitude generated in the console to upload my variable and show the map with the new coordinate.

You can do something like this:
var longitude;
var latitude;
mapView.onMapTapped.listen((location) {
print("Touched location $location");
// in a stateful widget
setState(() {
longitude = location.longitude;
latitude = location.latitude;
};
});
Then use it with the var longitude.
Where someFunction is the function that updates the map. (For updating the map you can use a state)
Next time read the starting guide before asking.

Related

How can I use myLocation with Swift using Google Map SDK for iOS?

I am making a single view application in Xcode, with Google Map SDK. I have followed instructions online and my application can successfully load the google map view. I have also enabled myLocation, so that myLocation button shows on the map view.
I understand that clicking the myLocation button will change the camera location automatically, but I'm wondering what I should do to use the data of myLocation (say to add a marker or add a path node)?
I've tried directly accessing mapView.myLocation, for example
let lat = mapView.myLocation?.coordinate.latitude
let long = mapView.myLocation?.coordinate.longitude
path.addCoordinate(CLLocationCoordinate2D(latitude: lat!, longitude: long!))
However, this will crash the applicaton and throw:
fatal error: unexpectedly found nil while unwrapping an Optional value
What does this error message mean and how should I resolve this?
The error says that myLocation property of mapView is nil. You should check if there is a value before accessing it.
if let myLocation = mapView.myLocation {
let lat = myLocation.coordinate.latitude
let long = myLocation.coordinate.longitude
path.addCoordinate(CLLocationCoordinate2D(latitude: lat, longitude: long))
}
Also verify why myLocation is nil. It might be that the user didn't allow location services.
This will not give any errors:
let lat = mapView.myLocation?.coordinate.latitude ?? give any value of latitude
let long = mapView.myLocation?.coordinate.longitude ?? give any value of longitude

How to trigger an EventKit alarm with Coordinates in Swift

I don't know about anyone else, but EventKit seems to have very little in terms resources and tutorials online for you to refer to for help.
I need to trigger an alarm when a user hits a radius of a set of coordinates, I wasn't sure of the best ay to do this, I was torn between local notifications, and EventKit reminders.
I decided to go for eventKit as I felt that I could do more with more the alarms and it was the most practical way to do it however, having not known much about EventKit i had some issues.
Anyway I've managed to get together and build a sample project which works and triggers an alert when the user leaves their current location, the only problem is, is that I almost want to do the complete opposite of that, I want to trigger an alert when the user enters a set of coordinates, I assume that most of the code is transferrable, however I seem to be stuck on one bit mainly.
// Creates an EKStructuredLocation Instance with a title of "Current Location"
let location = EKStructuredLocation(title: "Current Location")
// Uses the last location update extracted from the locations array to supply you're current location
location.geoLocation = locations.last as! CLLocation
// Location is added to a newly created alarm instance
let alarm = EKAlarm()
alarm.structuredLocation = location
// This alarm is triggered when the user moves away from the location proximity
alarm.proximity = EKAlarmProximityLeave
stationReminder.addAlarm(alarm)
I'm struggling to find how to set the location of the alarm to coordinates rather than users location.
I tried changing this
location.geoLocation = locations.last as! CLLocation
to
location.geoLocation = CLCircularRegion(circularRegionWithCenter: CLLocationCoordinate2D(latitude: 37.33233141, longitude: -122.03121860), radius: 50.0, identifier: "Location1")
but this doesn't work, i believe i am on the right track but i am throwing up this error: Cannot assign a value of type 'CLCircularRegion!' to a value of type 'CLLocation!'
I've tried loads of things with no resolve, does anybody have any experience with this and know how to help?
I also assume i'll have to change the following from this
alarm.proximity = EKAlarmProximityLeave
to this
alarm.proximity = EKAlarmProximityEnter
UPDATE
I've taken on board some comments below and tried a bunch of other things to get this to work, I feel like I am so close but somethings just missing. I cannot get this alarm to trigger. Excuse all the code comments, it's just so you can see some of the attempts i have made at fixing this.
can anyone see anything wrong with this code for the alarm?
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
// Stops location manager from sending further updates
locationManager.stopUpdatingLocation()
// Creates a new EKReminder which is named and initialised with text from the UITextField
let stationReminder = EKReminder(eventStore: appDelegate!.eventStore)
stationReminder.title = locationText.text
// Stores the previously created EKReminder in the default calendar
stationReminder.calendar = appDelegate!.eventStore!.defaultCalendarForNewReminders()
// Creates an EKStructuredLocation Instance with a title of "Current Location"
let location = EKStructuredLocation(title: "Destination: Bournemouth Station")
// Uses the last location update extracted from the locations array to supply you're current location
// location.geoLocation = locations.last as! CLLocation
// location.geoLocation = CLCircularRegion(circularRegionWithCenter: CLLocationCoordinate2D(latitude: 37.33233141, longitude: -122.03121860), radius: 50.0, identifier: "Location1")
// location.geoLocation = CLLocation(latitude: 50.742771, longitude: -1.895072)
location.radius = 50.0
location.geoLocation = CLLocation(latitude:50.742771, longitude:-1.895072)
// location.radius = 10.0 // metres
// Location is added to a newly created alarm instance
let alarm = EKAlarm()
alarm.structuredLocation = location
// This alarm is triggered when the user moves away from the location proximity
// alarm.proximity = EKAlarmProximityEnter
alarm.proximity = EKAlarmProximityEnter // "geofence": we alarm when *arriving*
// but this will have no effect until Reminders is granted Location access...
// and in iOS 8 it won't even ask for it until it is launched
// also, in iOS 8 the separate background usage pref is withdrawn;
// instead, auth of Reminders for "when in use" covers this...
// ...because it means "this app *or one of its features* is visible on screen"
stationReminder.addAlarm(alarm)
// Now we have a fully configured reminder which we save in the Event Store
var error: NSError?
appDelegate!.eventStore?.saveReminder(stationReminder,
commit: true, error: &error)
if error != nil {
println("Reminder failed with error \(error?.localizedDescription)")
}
}
Looks like EKEvent has an EKStructuredLocation, which you are using correctly. However, you need to be careful of the type of the geoLocation property. It should be a CLLocation, which is not the same as a CLCircularRegion.
Steps to fix:
check the docs for EKStructuredLocation https://developer.apple.com/library/mac/documentation/EventKit/Reference/EKStructuredLocationClassRef/index.html
set the location.geoLocation to a CLLocation that you create from latitude, longitude coordinates. (check the docs for CLLocation: https://developer.apple.com/library/mac/documentation/CoreLocation/Reference/CLLocation_Class/index.html#//apple_ref/swift/cl/CLLocation)
geoLocation = CLLocation(latitude: 37.33233141, longitude: -122.03121860)
set the geoLocation.radius separately location.radius = 50.0
at that point setting proximityEnter should work as you expected

geocodeAddressString not working for some addresses

I'm trying to covert the address to respected latitude and longitude values i.e Geocoding.
From Google definition:
What is Geocoding?
Geocoding is the process of converting addresses (like "1600 Amphitheatre Parkway, Mountain View, CA") into geographic coordinates (like latitude 37.423021 and longitude -122.083739), which you can use to place markers or position the map.
I am trying the same in iOS using Swift.
My code is working fine. But some addresses are not converting into lat, lng values. I am sure the address I'm searching is 100% valid.
Here is my code
override func viewDidLoad() {
super.viewDidLoad()
var address = "Kloten Airport, Switzerland" // This Address not working
var geocoder = CLGeocoder()
geocoder.geocodeAddressString(address, {(placemarks: [AnyObject]!, error: NSError!) -> Void in
if let placemark = placemarks?[0] as? CLPlacemark {
let title = ABCreateStringWithAddressDictionary(placemark.addressDictionary, false)
println(title);
let location = CLLocationCoordinate2D(
latitude: placemark.location.coordinate.latitude,
longitude: placemark.location.coordinate.longitude
)
}
})
}
I tried different addresses to geocode like Houston USA (this worked well), "Rautistrasse 12" (this also worked well), but when I search the address Kloten Airport, Switzerland this not worked for me.
Is there any bug, in iOS geocodeAddressString, or something I'm doing wrong?
I think you are expecting the iOS geocoding to perform as well as Google's geocoding. It certainly won't, though.
It is not working because Apple's geocoding is not as good as Google's geocoding.
To see what I mean, put
Kloten Airport, Sweden
into the iOS Maps app. It gives nothing. Put it into maps.google.com and you see the airport.

iOS 7 Parse Geolocation

I am working on an app for iOS 7 and I need to find the location of the user and then check to see what other users are also at that same location. I need it to update as soon as the user opens the app as well as update every so often and will display the users at the same location. I have looked at the available examples, but there doesn't seem to be enough on this using Parse. Can anyone give me any help on how to go about doing this, or if anyone knows of some examples similar to what I'm trying to do I would greatly appreciate it. Thanks in advance!
You need to break your problem down and tackle the various pieces -
Obtain the user's location when the app opens and periodically
The Location and Maps programming guide is a good starting point. You can use CLLocationManager to obtain your initial location and you can register for the significant location change event to get updates periodically, even when your app isn't running. Apple has example code plus there are plenty of other examples out there - Just search using "Core Location examples"
Store the user's location in your Parse database
There are examples and documentation on Parse.com showing how to do this. You can also provide a web service that allows your app to query the database for other users at the same location
Identify other users at the same location when your app isn't running
You can use Parse background jobs to trawl your database, match user locations and send push notifications to your users. Again there are examples on Parse.com showing how to set up background jobs and push notifications
this may help this will get your current location then save to parse, its just a matter of querying parse to pull the data down
func locationManager(manager: CLLocationManager!, didUpdateLocations
locations: [AnyObject]!) {
var userLocation : CLLocation = locations[0] as! CLLocation
var latitude = userLocation.coordinate.latitude
var longitude = userLocation.coordinate.longitude
var latDelta : CLLocationDegrees = 0.01
var lonDelta : CLLocationDegrees = 0.01
var span : MKCoordinateSpan = MKCoordinateSpanMake(latDelta,
lonDelta)
var location : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
var region : MKCoordinateRegion = MKCoordinateRegionMake(location, span)
self.mapView.setRegion(region, animated: true)
let testObject = PFObject(className: "User")
let currentPoints = PFGeoPoint(latitude: latitude, longitude: longitude)
testObject.setValue(currentPoints, forKey: "currentLocation")
testObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
if (success) {
dispatch_async(dispatch_get_main_queue()) {
println("added to parse")
}
} else {
println(error)
}
}
}

Update a map windows phone mango

I have a problem with the routing in a bing maps with windows phone mango. I need to update the map eliminating the old path when the user click on search everytime. I used the following guide: http://msdn.microsoft.com/en-us/library/ee681887.aspx and it works, but when i search again on the map remains the old paths. Thanks for the help!
I resolved the problem. i inserted in xaml map an attribute MapPolyline "percorso" and this is the code c#
// If the route calculate was a success and contains a route, then draw the route on the map.
if ((e.Result.ResponseSummary.StatusCode == BingMapRouteService.ResponseStatusCode.Success) & (e.Result.Result.Legs.Count != 0))
{
this.percorso.Locations = new LocationCollection();
// Retrieve the route points that define the shape of the route.
foreach (Location p in e.Result.Result.RoutePath.Points)
{
Location location = new Location();
location.Latitude = p.Latitude;
location.Longitude = p.Longitude;
this.percorso.Locations.Add(location);
}

Resources