如何以编程方式显示自定义用户位置

嘿,我是一个初学者,学习iOS应用程序开发,我想知道如何设置用户位置编程,就像我们在这里使用IDE(自定义位置): –

在这里输入图像说明

我设置一切和位置系统工作正常在我的模拟器(假的位置)我要做什么,如果我想做的同样的事情编程我工作我自己,发现了一个小解决scheme,但它不帮我出来就像缺less这里是我的代码: –

- (IBAction)showMyLocation:(id)sender { CLLocation *currentLocation=[[CLLocation alloc]initWithLatitude:75.14254 longitude:75.14254]; _map.userLocation.coordinate=currentLocation.coordinate; _map.showsUserLocation=YES; } 

这个代码的作品,但让我告诉你如何

  1. 如果我在模拟器中设置的位置没有任何事情发生时,我触发的行动。

  2. 如果我设置的位置任何给定的选项,让我们只要说苹果,它会显示苹果的位置,当我触发行动的位置,我只给了一次像一秒钟,再次显示苹果的位置。

所以任何能够帮助我的人都会得到真正的赞赏,我向所有问题似乎都不合适的人道歉。

有时模拟器上的位置服务会因您使用的API密钥而受到影响。 你必须首先看看这个链接,然后按照步骤在iOS中实现谷歌地图。

使用下面的代码来获取用户当前的位置。

 CLLocationManager *locationManager; locationManager = [[CLLocationManager alloc] init]; locationManager.distanceFilter = kCLDistanceFilterNone; locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; [locationManager requestWhenInUseAuthorization]; 

相应地保存经度和纬度。

 float latitude = locationManager.location.coordinate.latitude; float longitude = locationManager.location.coordinate.longitude; 

现在,您可以使用此方法以编程方式导航用户位置

 - (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]; } 

注意 :在下面的图像之前,将这些拖动键NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription添加到项目的info.plist文件中。

在这里输入图像说明

您可以使用这个AppleScript来打开模拟器并自动设置位置:

 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 

我使用run-applescript在我的javascript代码中运行AppleScript

 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:你需要给terminal许可。 (系统首选项 – >安全和隐私 – >隐私选项卡 – >在左侧菜单中select辅助function – >在列表中selectterminal)