2016-08-27 102 views
0

嘿,我是一名初学者,正在学习Ios App开发,我想知道如何以编程方式设置用户位置,就像我们在此处使用IDE(自定义位置)一样: -如何以编程方式显示自定义用户位置

enter image description here

我设置的一切,定位系统工作正常在我的模拟器(假的位置)我有什么做的,如果我想要做同样的事情编程我绞尽脑汁,并发现了一个小的解决方案,但它不是帮助我像这里缺少的东西是我的代码: -

- (IBAction)showMyLocation:(id)sender { 

    CLLocation *currentLocation=[[CLLocation alloc]initWithLatitude:75.14254 longitude:75.14254]; 
    _map.userLocation.coordinate=currentLocation.coordinate; 
    _map.showsUserLocation=YES; 

} 

此代码的工作,但让我告诉你如何

  1. ,如果我在模拟器上没有什么设置的位置时,我触发动作发生。

  2. 如果我将位置设置为任何给定的选项让我们只说苹果,它会显示苹果的位置,当我触发动作时,我给出的位置只是一次显示,而不是再次显示位置的苹果被展示出来。

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

+0

尝试一次在自定义位置 –

+0

我做了它的自定义方式我想知道如何以编程方式实现它,以防用户是否触发该动作,每次都会更改位置。 – dreamBegin

+0

你能解释更多在这一行触发动作的位置将每次都改变 –

回答

2

由于您使用的是API密钥,有时模拟器上的位置服务会受到影响。您必须首先有look of this link,并按照步骤执行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文件之前,见下图。

enter image description here

+0

让我看看,我会很快回复你,欣赏你的时间。现在我会标记它是正确的,因为没有其他答案:) – dreamBegin

+0

@dreamBegin欢迎:),首先明白和实施,你将不需要任何其他答案来解决你的概率..谢谢。 – vaibhav

+0

它不适合我哥哥我应该选择在调试器的位置? – dreamBegin

1

您可以使用此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运行AppleScriptjavascript代码中。

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:您需要授予终端权限。(系统首选项 - >安全&隐私 - >隐私标签 - >选择辅助功能在左侧菜单 - >选择终端的列表)

+0

伟大的..新的东西:) – dreamBegin

相关问题