Browse Source

Device location displayed in map. Location permission asked. Better location section UI.

Iñigo Valentin 9 years ago
parent
commit
36040a8088
5 changed files with 108 additions and 11 deletions
  1. 2 0
      app/Info.plist
  2. 27 0
      app/LocationView.swift
  3. 66 7
      app/LocationView.xib
  4. 2 2
      app/RowBlog.xib
  5. 11 2
      app/ViewController.swift

+ 2 - 0
app/Info.plist

@@ -8,6 +8,8 @@
 	<string>Gasteizko Margolariak</string>
 	<key>CFBundleExecutable</key>
 	<string>$(EXECUTABLE_NAME)</string>
+	<key>NSLocationWhenInUseUsageDescription</key>
+	<string>Permitiendo acceso a tu ubicación, podrás conocer la distancia a la cuadrilla y a determinados eventos.</string>
 	<key>CFBundleIdentifier</key>
 	<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
 	<key>CFBundleInfoDictionaryVersion</key>

+ 27 - 0
app/LocationView.swift

@@ -21,12 +21,16 @@
 import Foundation
 import CoreData
 import UIKit
+import GoogleMaps
 /**
 Class to handle the home view.
 */
 class LocationView: UIView {
 	
 	@IBOutlet var container: UIView!
+	@IBOutlet weak var map: GMSMapView!
+	
+	var didFindMyLocation = false
 	
 	override init(frame: CGRect){
 		super.init(frame: frame)
@@ -44,6 +48,12 @@ class LocationView: UIView {
 		Bundle.main.loadNibNamed("LocationView", owner: self, options: nil)
 		self.addSubview(self.container)
 		self.container.frame = self.bounds
+		
+		// Set map up
+		map.isMyLocationEnabled = true
+		//map.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.new, context: nil)
+		let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: 42.846399, longitude: -2.673365, zoom: 12.0)
+		map.camera = camera
 		/*self.postList = self.section.getContentStack()
 		
 		// Get viewController from StoryBoard
@@ -59,4 +69,21 @@ class LocationView: UIView {
 		NSLog(":LOCATION:DEBUG: Finished loading LocationView")
 		
 	}
+	
+	func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
+		if status == CLAuthorizationStatus.authorizedWhenInUse {
+			map.isMyLocationEnabled = true
+		}
+	}
+	
+	/*func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutableRawPointer) {
+		if !didFindMyLocation {
+			let myLocation: CLLocation = change[NSKeyValueChangeNewKey] as CLLocation
+			map.camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate, zoom: 10.0)
+			map.settings.myLocationButton = true
+			
+			didFindMyLocation = true
+		}
+	}
+	*/
 }

+ 66 - 7
app/LocationView.xib

@@ -12,6 +12,7 @@
         <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="LocationView" customModule="app">
             <connections>
                 <outlet property="container" destination="iN0-l3-epB" id="HCq-e3-37n"/>
+                <outlet property="map" destination="guk-vu-ykd" id="zo6-Wc-NCg"/>
             </connections>
         </placeholder>
         <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
@@ -29,20 +30,78 @@
                             <fontDescription key="fontDescription" type="boldSystem" pointSize="14"/>
                             <textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
                         </textView>
-                        <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="guk-vu-ykd" customClass="GMSMapView">
-                            <rect key="frame" x="10" y="80" width="335" height="557"/>
-                            <color key="backgroundColor" red="1" green="0.20334201390000001" blue="0.32001410590000001" alpha="1" colorSpace="calibratedRGB"/>
+                        <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="3aZ-8G-e8X" userLabel="Entry">
+                            <rect key="frame" x="10" y="43" width="335" height="594"/>
+                            <subviews>
+                                <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="La cuadrilla está por ahí!" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="jwp-Sd-GF5" userLabel="Title">
+                                    <rect key="frame" x="10" y="10" width="315" height="22"/>
+                                    <constraints>
+                                        <constraint firstAttribute="height" constant="22" id="JQk-vd-DLN"/>
+                                    </constraints>
+                                    <fontDescription key="fontDescription" type="boldSystem" pointSize="18"/>
+                                    <color key="textColor" red="0.1529411765" green="0.33333333329999998" blue="0.45098039220000002" alpha="1" colorSpace="calibratedRGB"/>
+                                    <nil key="highlightedColor"/>
+                                </label>
+                                <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="A X metros de ti." textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="lB5-i1-GOe" userLabel="Message">
+                                    <rect key="frame" x="11.5" y="42" width="311" height="21"/>
+                                    <constraints>
+                                        <constraint firstAttribute="height" constant="21" id="ann-h3-NzS"/>
+                                    </constraints>
+                                    <fontDescription key="fontDescription" type="system" pointSize="17"/>
+                                    <nil key="textColor"/>
+                                    <nil key="highlightedColor"/>
+                                </label>
+                                <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="guk-vu-ykd" customClass="GMSMapView">
+                                    <rect key="frame" x="10" y="73" width="315" height="492"/>
+                                    <color key="backgroundColor" red="1" green="0.20334201390000001" blue="0.32001410590000001" alpha="1" colorSpace="calibratedRGB"/>
+                                    <userDefinedRuntimeAttributes>
+                                        <userDefinedRuntimeAttribute type="color" keyPath="borderColor">
+                                            <color key="value" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
+                                        </userDefinedRuntimeAttribute>
+                                        <userDefinedRuntimeAttribute type="number" keyPath="borderWidth">
+                                            <real key="value" value="1"/>
+                                        </userDefinedRuntimeAttribute>
+                                        <userDefinedRuntimeAttribute type="number" keyPath="cornerRadius">
+                                            <real key="value" value="3"/>
+                                        </userDefinedRuntimeAttribute>
+                                    </userDefinedRuntimeAttributes>
+                                </view>
+                            </subviews>
+                            <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
+                            <constraints>
+                                <constraint firstItem="jwp-Sd-GF5" firstAttribute="centerX" secondItem="3aZ-8G-e8X" secondAttribute="centerX" id="6vZ-Ex-Gdp"/>
+                                <constraint firstItem="lB5-i1-GOe" firstAttribute="centerX" secondItem="3aZ-8G-e8X" secondAttribute="centerX" id="8VC-kx-sMo"/>
+                                <constraint firstItem="lB5-i1-GOe" firstAttribute="width" secondItem="3aZ-8G-e8X" secondAttribute="width" constant="-24" id="MQn-K0-SK5"/>
+                                <constraint firstItem="jwp-Sd-GF5" firstAttribute="top" secondItem="3aZ-8G-e8X" secondAttribute="top" constant="10" id="Ni3-zk-KAJ"/>
+                                <constraint firstItem="guk-vu-ykd" firstAttribute="width" secondItem="3aZ-8G-e8X" secondAttribute="width" constant="-20" id="OK6-Tx-KhU"/>
+                                <constraint firstItem="guk-vu-ykd" firstAttribute="top" secondItem="lB5-i1-GOe" secondAttribute="bottom" constant="10" id="Q8e-BQ-NFd"/>
+                                <constraint firstItem="lB5-i1-GOe" firstAttribute="top" secondItem="jwp-Sd-GF5" secondAttribute="bottom" constant="10" id="i7u-s7-Fz0"/>
+                                <constraint firstItem="guk-vu-ykd" firstAttribute="centerX" secondItem="3aZ-8G-e8X" secondAttribute="centerX" id="ifj-50-FJg"/>
+                                <constraint firstItem="jwp-Sd-GF5" firstAttribute="width" secondItem="3aZ-8G-e8X" secondAttribute="width" constant="-20" id="jF5-0R-cru"/>
+                                <constraint firstAttribute="bottom" secondItem="guk-vu-ykd" secondAttribute="bottom" constant="29" id="kMB-uO-8bG"/>
+                            </constraints>
+                            <userDefinedRuntimeAttributes>
+                                <userDefinedRuntimeAttribute type="color" keyPath="borderColor">
+                                    <color key="value" red="0.42745098040000001" green="0.41176470590000003" blue="0.56470588239999997" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
+                                </userDefinedRuntimeAttribute>
+                                <userDefinedRuntimeAttribute type="number" keyPath="borderWidth">
+                                    <integer key="value" value="1"/>
+                                </userDefinedRuntimeAttribute>
+                                <userDefinedRuntimeAttribute type="number" keyPath="cornerRadius">
+                                    <integer key="value" value="3"/>
+                                </userDefinedRuntimeAttribute>
+                            </userDefinedRuntimeAttributes>
                         </view>
                     </subviews>
                     <color key="backgroundColor" red="0.59607843140000005" green="0.7843137255" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                     <constraints>
+                        <constraint firstAttribute="bottom" secondItem="3aZ-8G-e8X" secondAttribute="bottom" constant="10" id="0ng-XI-amL"/>
                         <constraint firstItem="c4V-0M-oWK" firstAttribute="top" secondItem="Z2m-Sw-4v4" secondAttribute="top" id="3RS-0J-fYo"/>
-                        <constraint firstItem="guk-vu-ykd" firstAttribute="centerX" secondItem="Z2m-Sw-4v4" secondAttribute="centerX" id="7ge-Go-0Hw"/>
                         <constraint firstItem="c4V-0M-oWK" firstAttribute="width" secondItem="Z2m-Sw-4v4" secondAttribute="width" id="Amn-ze-UpO"/>
-                        <constraint firstAttribute="bottom" secondItem="guk-vu-ykd" secondAttribute="bottom" constant="10" id="K6I-L4-YhN"/>
+                        <constraint firstItem="3aZ-8G-e8X" firstAttribute="width" secondItem="Z2m-Sw-4v4" secondAttribute="width" constant="-20" id="LaM-9I-jov"/>
                         <constraint firstItem="c4V-0M-oWK" firstAttribute="leading" secondItem="Z2m-Sw-4v4" secondAttribute="leading" id="UGS-qP-Yv3"/>
-                        <constraint firstItem="guk-vu-ykd" firstAttribute="top" secondItem="Z2m-Sw-4v4" secondAttribute="top" constant="80" id="i0e-tX-0LF"/>
-                        <constraint firstItem="guk-vu-ykd" firstAttribute="width" secondItem="Z2m-Sw-4v4" secondAttribute="width" constant="-20" id="yDs-Kq-7TW"/>
+                        <constraint firstItem="3aZ-8G-e8X" firstAttribute="centerX" secondItem="Z2m-Sw-4v4" secondAttribute="centerX" id="edw-qA-hiY"/>
+                        <constraint firstItem="3aZ-8G-e8X" firstAttribute="top" secondItem="c4V-0M-oWK" secondAttribute="bottom" constant="10" id="obC-uk-Htx"/>
                     </constraints>
                     <userDefinedRuntimeAttributes>
                         <userDefinedRuntimeAttribute type="color" keyPath="borderColor">

+ 2 - 2
app/RowBlog.xib

@@ -1,11 +1,11 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
+<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
     <device id="retina4_7" orientation="portrait">
         <adaptation id="fullscreen"/>
     </device>
     <dependencies>
         <deployment identifier="iOS"/>
-        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
         <capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
     </dependencies>

+ 11 - 2
app/ViewController.swift

@@ -20,11 +20,12 @@
 
 import UIKit
 import CoreData
+import CoreLocation
 
 /**
   The view controller of the app.
  */
-class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIGestureRecognizerDelegate {
+class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIGestureRecognizerDelegate, CLLocationManagerDelegate {
 	
 	var delegate: AppDelegate?
 	
@@ -44,6 +45,10 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 	
 	var passId: Int = -1
 	
+	// Location-related variables
+	var locationManager = CLLocationManager()
+	var didFindMyLocation = false
+	
 	/**
 	 Controller initializer.
 	*/
@@ -144,7 +149,11 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 	 Run when the app loads.
 	*/
 	override func viewDidLoad() {
-						
+		
+		// Ask for location permissions.
+		locationManager.delegate = self
+		locationManager.requestWhenInUseAuthorization()
+		
 		//Hide all sections, except for the first one
 		self.containerViewLocation.alpha = 0
 		self.containerViewLablanca.alpha = 0