如何以编程方式显示自定义用户位置 [英] how to show Custom user location programmatically

查看:91
本文介绍了如何以编程方式显示自定义用户位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 如果我在模拟器中将位置设置为无,则触发动作时什么也不会发生.

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

如果我将位置设置为给定选项中的任何一个,可以说苹果将显示苹果的位置,当我触发操作时,我给出的位置将显示一秒钟,然后再次显示显示了苹果的位置.

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.

推荐答案

有时候,由于您使用的API密钥,模拟器上的位置服务会受到影响.首先,您必须具有此链接,然后按照中的步骤进行操作以便在iOS中实现Google Map.

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

注意:将以下两个键NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription添加到项目的info.plist文件中,然后查看下图.

Note: add these tow keys NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription into your project's info.plist file before see image below.

这篇关于如何以编程方式显示自定义用户位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆