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

how to show Custom user location programmatically

嘿,我是初学者,正在学习 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. 如果我将位置设置为任何给定选项,只需说苹果,它就会显示苹果的位置,当我触发操作时,我给出的位置只显示一次,就像一秒钟一样然后再次显示苹果的位置。

任何能帮助我的人都将不胜感激,我向所有觉得这个问题不合适的人表示歉意。

有时,由于您使用的 API 密钥,模拟器上的定位服务会受到影响。您必须首先拥有 look of this link 并按照步骤在 iOS.

中实施 google 映射

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

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

我在 javascript 代码中使用 run-applescript 到 运行 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: 您需要给终端权限。 (系统偏好设置 -> 安全和隐私 -> 隐私选项卡 -> Select 左侧菜单的辅助功能 -> select 列表中的终端)