how to show Custom user location programmatically

[亡魂溺海] 提交于 2019-12-26 03:09:10

问题


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

  1. if i set the location in simulator to none nothing happen when i trigger the action.

  2. 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.


回答1:


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.




回答2:


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)



来源:https://stackoverflow.com/questions/39178990/how-to-show-custom-user-location-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!