I'm getting this error:
This application has been blocked by the Google Maps API. This might be because of an incorrectly registered key.
When I try to set google maps panorama street view. If I display a regular map view, it works so seems like the key is registered properly.
I followed the directions for a panorama view exactly as per google docs:
import UIKit
import GoogleMaps
class ViewController: UIViewController, GMSMapViewDelegate {
override func loadView() {
let panoView = GMSPanoramaView(frame: .zero)
self.view = panoView
panoView.moveNearCoordinate(CLLocationCoordinate2D(latitude: -33.732, longitude: 150.312))
}
}
Any ideas?
For me, the solution was that I needed to link my project in Google Cloud Platform to a billing account.
Once I did that the street view started working correctly.
The standard Google map worked fine without a billing account, which confused me initially.
You need to configure the delegate for this object. Something like:
import UIKit
import GoogleMaps
class ViewController: UIViewController, GMSMapViewDelegate {
override func loadView() {
let panoView = GMSPanoramaView(frame: .zero)
panoView.delegate = self
self.view = panoView
panoView.moveNearCoordinate(CLLocationCoordinate2D(latitude: -33.732, longitude: 150.312))
}
}
extension ViewController: GMSPanoramaViewDelegate {
func panoramaView(_ view: GMSPanoramaView, error: Error, onMoveNearCoordinate coordinate: CLLocationCoordinate2D) {
print("error: \(error.localizedDescription)")
}
}
Related
I want to plot a specific point on the map having the lat and lon from an api.
Program flow:
Get LAT & LON from api (done)
Ping api again via timer after every 5 seconds to get the latest location (done)
Plot location with retrieved LAT & LON on map
The issue is every code on the net has to do with 2 points, so user loc and destination loc. I cant seem to get it to work without user loc. I have however coded this to plot the location. However, with this when I touch the map, the map zooms out. Another issue is when I get another point the previous one also remains on the screen. (for testing purpose I hard coded the lat and lon but when I connect the api code that refreshes, prior points remain and the map code is the same as this. The lat and lon are passed via func parameters in createAnnotation().)
My code:
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self // or connect in storyboard
createAnnotation()
}
func createAnnotation(){
let annotations = MKPointAnnotation()
annotations.coordinate = CLLocationCoordinate2D(latitude: 41.87369, longitude: -87.813293)
mapView.addAnnotation(annotations)
}}
How Do I plot the coordinates properly? and then delete the prior and show the new one?.
For the "previous one also remains on the screen" problem: don't keep making a new annotation and calling addAnnotation if you don't want to keep adding new annotations. Instead, keep hold of the annotation that you add, and move it later using its coordinate property. Something like this maybe:
class ViewController: UIViewController, MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
var annotationForThing: MKPointAnnotation?
var coordinateOfThing: CLLocationCoordinate2D? {
didSet {
guard let newCoord = coordinateOfThing else {
if let existing = annotationForThing {
mapView.removeAnnotation(existing)
}
return
}
if let existing = annotationForThing {
existing.coordinate = coordinateOfThing
}
else {
let newAnnotation = MKPointAnnotation()
newAnnotation = coordinateOfThing
mapView.addAnnotation(newAnnotation)
annotationForThing = newAnnotation
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self // or connect in storyboard
}
I am new to XCode 11 and SwiftUI and have been following a tutorial (famous last words): https://developers.google.com/maps/documentation/ios-sdk/styling, trying to create a custom Google Maps interface using the Google Maps SDK. I have a basic map view in my app already, but I am trying to use a JSON file to style the map (specifically, I am trying to highlight local roads). My problem is that the solution provided by the Google documentation is not for SwiftUI, as it uses a ViewController. How should I use a JSON file to style the maps in SwiftUI? As I said I am a total newbie to Swift, so I would greatly appreciate answers on a beginner level, if possible. My code for the Map View called in ContentView.swift looks like this (not sure if it's relevant):
import SwiftUI
import GoogleMaps
struct GoogleMapsView: UIViewRepresentable {
private let zoom: Float = 15.0
func makeUIView(context: Self.Context) -> GMSMapView {
let camera = GMSCameraPosition.camera(withLatitude: 33.3969, longitude: -84.5963, zoom: 13.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
return mapView
}
func updateUIView(_ mapView: GMSMapView, context: Context) {
}
}
struct GoogleMapsView_Previews: PreviewProvider {
static var previews: some View {
GoogleMapsView()
}
}
I think it doesn't matter if you're using SwiftUI or not.
The important part is:
if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
just add it between this:
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
->
return mapView
I am walking through the basic iOS guide to add a map to my App here: https://developers.arcgis.com/ios/10-2/swift/guide/develop-your-first-map-app.htm
When running the app, it displays the basemap I have added but it does not seem to respond to any actions so I can not pane/zoom.
Here is my exact Swift controller code:
import UIKit
import ArcGIS
class ViewController: UIViewController, AGSMapViewLayerDelegate {
#IBOutlet var mapView: AGSMapView!
override func viewDidLoad() {
super.viewDidLoad()
let tiledLayer = AGSLocalTiledLayer(name: "Norfolk")
self.mapView.addMapLayer(tiledLayer, withName: "Norfolk")
mapView.locationDisplay.startDataSource()
}
}
I have tested on both an iPad and iPhone and the behavior is the same.
I am using ArcGIS 10.2.5
Where is your map url?
let url = NSURL(string: "http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer")
let tiledLayer = AGSTiledMapServiceLayer(URL: url)
self.mapView.addMapLayer(tiledLayer, withName: "Basemap Tiled Layer")
And have you added Privacy - Location When In Use Usage Description into info.plist of your app? You should do that if you call
mapView.locationDisplay.startDataSource()
I am not sure what I did incorrectly the first time, but I removed the view and re added it and it worked the 2nd time.
I seted a UITableViewController as TWTRTimeLineController and it shows tweets and everything but it doesn't recognizes videos. Thats really weird it shows them as only pictures. The code is very simple i don't know if Im forgetting anything.
import UIKit
import TwitterKit
class TimeLineController: TWTRTimelineViewController {
var navController: personalNavController!
override func viewDidLoad() {
super.viewDidLoad()
let client = TWTRAPIClient()
self.dataSource = TWTRSearchTimelineDataSource(searchQuery: "#CamNowAppBeta", APIClient: client)
self.showTweetActions = true
}
func tweetView(tweetView: TWTRTweetView, didTapVideoWithURL videoURL: NSURL){
}
}
TWTRTweetViewDelegate method didTapVideoWithURL must be removed
I'm trying to convert the simple GMSPlacePicker example to Swift
but the Picker appears but then disappears immediately as soon as the transition from Right to left completes.
// ViewController.swift
import UIKit
import CoreLocation
import GoogleMaps
class ViewController: UIViewController {
#IBAction func buttonPlacesPicker_TouchUpInside(sender: AnyObject) {
//-----------------------------------------------------------------------------------
var southWestSydney : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-33.8659, 151.1953)
var northEastSydney : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-33.8645, 151.1969)
var sydneyBounds : GMSCoordinateBounds = GMSCoordinateBounds(coordinate: southWestSydney, coordinate:northEastSydney)
//var config : GMSPlacePickerConfig = GMSPlacePickerConfig(viewport:sydneyBounds)
var config : GMSPlacePickerConfig = GMSPlacePickerConfig(viewport:nil)
//---------------------------------------------------------------------
var placePicker : GMSPlacePicker = GMSPlacePicker(config: config)
//typealias GMSPlaceResultCallback = (GMSPlace?, NSError?) -> Void
var error: NSError? = nil
var gmsPlace: GMSPlace? = nil
placePicker.pickPlaceWithCallback(){
(gmsPlace, error) -> Void in
if let error = error{
println("error:\(error)")
}else{
if let gmsPlace = gmsPlace{
if let formattedAddress = gmsPlace.formattedAddress{
println("formattedAddress:\r\(formattedAddress)")
}else{
println("gmsPlace.formattedAddress is nil")
}
}else{
println("gmsPlace is nil")
}
println("info")
}
}
}
}
My app has asked for Location sucessfully
I have a bridging header to Google Maps.
I didnt use cocoa pods to install the framework
but I've used the framework in Obj-C before
so just dragged the GoogleMaps.framework to project
and the internal resources bundle
I added all the following from previous tutorials and linker errors:
When I run it I can see sydney in the pickers maps.
It transitions from right side fo the screen to the left.
When it reaches the left it disappears
I added Reveal app and I cant see the picker view offline.
My GMS services api key is correct as its one I used in obj-c app to show Places Picker.
Bundle id is correct.
My Swift knowledge is "I think I know it. I probably don't"
any ideas?
You should retain the reference to your GMSPlacePicker. Make it a property instead of local variable.
FIXED made it an ivar...DOH!
class ViewController: UIViewController {
var placePicker : GMSPlacePicker?
#IBAction func buttonPlacesPicker_TouchUpInside(sender: AnyObject) {
self.placePicker?.pickPlaceWithCallback(){
...