2016-08-27 118 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

相關問題