【问题标题】:how to show Custom user location programmatically如何以编程方式显示自定义用户位置
【发布时间】:2017-01-03 21:00:57
【问题描述】:

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

我设置了所有东西并且定位系统在我的模拟器(假位置)中运行良好如果我想以编程方式做同样的事情我必须做的事情我自己工作并找到了一个小解决方案,但它并没有像某些事情一样帮助我缺少这是我的代码:-

- (IBAction)showMyLocation:(id)sender {

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

}

这段代码有效,但让我告诉你如何

  1. 如果我将模拟器中的位置设置为 none,则触发操作时不会发生任何事情。

  2. 如果我将位置设置为任何给定选项,让我们说苹果,它将显示苹果的位置,当我触发操作时,我给出的位置只显示一次显示苹果的位置。

任何可以帮助我的人都将受到真正的感激,我向所有觉得这个问题不合适的人道歉。

【问题讨论】:

  • 在自定义位置尝试一次
  • 我是通过自定义方式完成的,我想知道如何以编程方式实现它,以防用户触发操作位置每次都会更改。
  • 你能在这行解释更多触发位置每次都会改变的动作
  • 我的意思是当用户点击按钮时,位置坐标会增加或减少,事实上我只是想以编程方式添加位置@Anbu.Karthik
  • 好的,但是你的编码很好,问题是什么

标签: ios objective-c xcode mapkit cllocationmanager


【解决方案1】:

有时模拟器上的定位服务会因为您使用的 API 密钥而受到影响。您必须首先拥有look of this link 并按照步骤在 iOS 中实现 google map。

使用以下代码获取用户当前位置。

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 文件中。

【讨论】:

  • 让我看看,我会尽快回复您,感谢您的时间。现在我会把它标记为正确的,因为没有其他答案:)
  • @dreamBegin 欢迎 :),首先理解并仔细实施,您将不需要任何其他答案来解决您的问题。谢谢。
  • 这对我不起作用兄弟我应该在调试器的位置选择什么?
  • 我正在使用 mapView。
  • 嘿,我找到了解决方案,无是正确的,但现在问题是我们知道我们已将位置设置为无,现在我们正在以编程方式添加一个位置,因此 locationPoint 应该显示为蓝色现在显示为灰色,就像它已禁用一样我现在不知道该怎么办?
【解决方案2】:

您可以使用这个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:你需要给终端权限。 (系统偏好设置 -> 安全和隐私 -> 隐私选项卡 -> 在左侧菜单中选择辅助功能 -> 在列表中选择终端)

【讨论】:

  • 太棒了.. 一些新的东西:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-26
  • 2011-06-29
  • 2018-02-28
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多