Hey I'm a beginner and learning Ios App development and i want to know that how to set the the user location programmatically like we did here using IDE (Custom Location) :-
I setup everything and location system is working fine in my simulator (fake location) what i have to do if i want to do the same thing programmatically i worked myself and found a little solution but its not helping me out like something is missing here's my code :-
- (IBAction)showMyLocation:(id)sender {
CLLocation *currentLocation=[[CLLocation alloc]initWithLatitude:75.14254 longitude:75.14254];
_map.userLocation.coordinate=currentLocation.coordinate;
_map.showsUserLocation=YES;
}
this code works but let me tell you how
if i set the location in simulator to none nothing happen when i trigger the action.
if i set the location to any of the given option lets just say apple it will show location of the apple and when i trigger the action the location that i give just showed for once like a second and than again the location of the apple is showed.
soo anyone who can help me out will be truly appreciated and my apologies to everyone to whom the question seem inappropriate.
Sometimes the location services on simulator are affected because of API key you are using. Your must have a look of this link firstly and follow the steps in order to implement google map in iOS.
Use following code to get user current location.
CLLocationManager *locationManager;
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager requestWhenInUseAuthorization];
Save latitude & longitude accordingly.
float latitude = locationManager.location.coordinate.latitude;
float longitude = locationManager.location.coordinate.longitude;
Now you can navigate user location programatically using this method
- (IBAction)showMyLocation:(id)sender {
CLLocationCoordinate2D centre;
centre.latitude = latitude; // getting latitude
centre.longitude = longitude; // getting longitude
// IF USING MkMapView
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:MKCoordinateRegionMakeWithDistance(centre, 200, 200)];
[self.mapView setRegion:adjustedRegion animated:YES];
// IF USING GMSMapView
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude
longitude:longitude
zoom:15];
[self.mapView animateToCameraPosition:camera];
}
Note: add these tow keys NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription into your project's info.plist file before see image below.
You can use this AppleScript to open the simulator and set the location automatically:
tell application "Simulator"
activate
end tell
tell application "System Events"
tell process "Simulator"
tell menu bar 1
tell menu bar item "Debug"
tell menu "Debug"
tell menu item "Location"
click
tell menu "Location"
click menu item "Custom Location…"
end tell
end tell
end tell
end tell
end tell
tell window 1
set value of text field 1 to "52,625"
set value of text field 2 to "13,51"
click button "OK"
end tell
end tell
end tell
I used run-applescript to run AppleScript inside my javascript code.
runApplescript('tell application "Simulator" \nactivate\nend tell\ntell application "System Events"\ntell process "Simulator"\ntell menu bar 1\ntell menu bar item "Debug"\ntell menu "Debug"\ntell menu item "Location"\nclick\ntell menu "Location"\nclick menu item "Custom Location…"\nend tell\nend tell\nend tell\nend tell\nend tell\ntell window 1\nset value of text field 1 to "52,625"\nset value of text field 2 to "13,51"\nclick button "OK"\nend tell\nend tell\nend tell')
PS: You need to give permission to terminal. (System Preferences -> Security & Privacy -> Privacy tab -> Select Accessibility on the left menu -> select terminal on the list)
Related
Is there one way to customize myLocationButton GoogleMaps ? I want to add my own button with a background image which make the same function as the original Google button
First your Application needs to get current location using CLLocation Manager. If you don't know how, refer to my answer here : For google maps iOS sdk, current location occasionally gets plotted at 0,0
Now for button press action, do this
- (void)locationButtonPressed
{
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:self.mapView.myLocation.coordinate.latitude
longitude:self.mapView.myLocation.coordinate.longitude
zoom:self.mapView.camera.zoom];
[self.gMapView animateToCameraPosition:camera];
}
Hope that helps.
I'am trying to add a view to google map which both have been created using the storyboard. In order to do this, I've changed this part of the code:
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
to this:
mapView = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
self.mapView.myLocationEnabled = YES;
[self.view insertSubview:mapView atIndex:0];
it works, but the default "My location button" disappeared after this. Is there a way to get back the default button? I would greatly appreciate any help.
I tried this solution My location button not appearing in view (Google maps SDK, ios)
but it didn't work or maybe I didn't get it correctly, if this is the only solution could you please explain it in details?
I got around this problem by creating a button from the storyboard and connect it to this action.
- (IBAction)MyLocation:(id)sender {
CLLocation *location = mapView.myLocation;
if (location) {
[mapView animateToLocation:location.coordinate];
} }
In my case, if I place the created button on the lower right/left corner still can't get it but everywhere else is just fine. (I don't know why I think because of inserting the map at index 0 or something like that).
Are you requesting access to location services correctly? It's done a bit different in iOS 8. Try adding the following to you Info.plist file and let me know if it works:
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your message to request location usage permission goes here</string>
What I'm trying to do is set the location of my iPhone through my app. The only thing I've been able to do is in xcode go to debug->simulate location. My question is, is it possible to set the location without using xcode.
Thanks!
You may do some modification when you achieve your locations.
You can use a viewController with a map view to choose a location.
when you want to simulate a location, just achieve from your simulated locations.
I have wrote a similar function before, you can make a reference.
The source codes
And the operations start->TabBarController->The Third Tab->The right Bar Item->The Second Row in the First Section->Long press to pick a location
If you have a locationManager, it will be very convenient. like
coordinate = [STDLocationPickerController cachedFakeLocationCoordinate]
if (coordinate.long * lat > 0) {
continue your logic
} else {
locate or pick a location
}
In WatchKit, the documentation for WKInterfaceMap specify that:
"Tapping the map launches the Maps app on the user’s Apple Watch and
displays the corresponding location.
However: I want to show multiple map annotations on the map - is there a way to specify which map annotation will be displayed in Maps.app when the map is tapped?
There is currently no visibility as to exactly what happens when Apple Watch transitions from a WKInterfaceMap to Apple's native Maps application.
Try the WatchKit Developer Forums, exactly the right place for this question!
Currently in WatchOS 2 (at least), tapping on a WKInterfaceMap will open up the Watch's Maps app with the option the get directions to the location that was at the centre point of your WKInterfaceMap. When viewing these directions to this location it will be marked with a Red Pin annotation.
None of your custom annotations will be displayed in the Watch's Maps app.
You can use following methods to add multiple annotations. As per Apple documentation, currently only 5 annotations can be displayed on watch app's map
addAnnotation:withPinColor
addAnnotation:withImageNamed:centerOffset:
addAnnotation:withPinColor:
Here is a sample code, which displays 2 annotations:
CLLocationCoordinate2D mapLocation1 = CLLocationCoordinate2DMake(37.787730, -122.403370);
CLLocationCoordinate2D mapLocation2 = CLLocationCoordinate2DMake(37.794873, -122.397892);
//
MKCoordinateSpan coordinateSpan = MKCoordinateSpanMake(0.1, 0.1);
// Other colors include red and green pins
[self.map addAnnotation:mapLocation1 withPinColor: WKInterfaceMapPinColorPurple];
[self.map addAnnotation:mapLocation2 withPinColor: WKInterfaceMapPinColorRed];
[self.map setRegion:(MKCoordinateRegionMake(mapLocation, coordinateSpan))];
i use the following code to launch Google Maps to show step by step navigation from my current location to a specific location:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"comgooglemaps://?daddr=%f,%f&saddr=%f,%f",destLocation.coordinate.latitude, destLocation.coordinate.longitude,_currentLocation.coordinate.latitude,_currentLocation.coordinate.longitude]];
however, when Google Maps app opens, it displays for example "Howard Street" as the starting point label, which is correct, but how can I let Google Maps app displays "My Location" label instead of the street name label as the starting point ?
p.s. the user can display "My Location" label if he/she taps on the coordinates, then choose "My Location" as the starting point from inside Google Maps, but I want this behavior to be done on behalf of the user.
from Google Maps SDK, if I didn't provide the source coordinates, Google Maps will automatically display "My Location" as the starting point label.