当前位置Google地图swift,无法访问defaultLocation.coordinate

时间:2019-01-26 16:17:07

标签: ios swift google-maps

我一直在查看google文档以获取当前位置,但是我正在努力在ViewDidLoad中指定位置,如您所见,我已将Location设置为Syndey Australia。在google doc文档中,当指定当前位置时,它们使用的是“ default location.coordinate”,我无法在我的代码中访问defaultLocation.coordinate。

这是文档的链接:https://developers.google.com/maps/documentation/ios-sdk/current-place-tutorial

这是viewDidLoad内部的代码

private void button1_Click(object sender, EventArgs e)
    {
        Font font = new Font("Arial", 52, FontStyle.Regular);
        Image i = GetTextAsImage(textBox1.Text,400, font, Color.Black, Color.LightGray);

        i.Save("myImage.jpeg", ImageFormat.Jpeg);

    }
    private Image GetTextAsImage(String text, int widthInPixel, Font textFont, Color textColor, Color backColor)
    {
        //first, create a dummy bitmap just to get a graphics object
        Image img = new Bitmap(1, 1);
        Graphics drawing = Graphics.FromImage(img);

        //measure the string to see how big the image needs to be
        SizeF textSize = drawing.MeasureString(text, textFont);

        //free up the dummy image and old graphics object
        img.Dispose();
        drawing.Dispose();

        //create a new image of the right size
        img = new Bitmap((int)textSize.Width, textFont.Height);

        drawing = Graphics.FromImage(img);
        drawing.SmoothingMode = SmoothingMode.AntiAlias;
        drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        //paint the background
        drawing.Clear(backColor);

        //create a brush for the text
        Brush textBrush = new SolidBrush(textColor);


        drawing.DrawString(text, textFont, textBrush, 0, 0,StringFormat.GenericTypographic);

        drawing.Save();

        textBrush.Dispose();
        drawing.Dispose();

        return img;

    }

这是我的扩展代码

    GMSServices.provideAPIKey("AIzaSyARsPfNs54hrUAC4yKtHeu2jXfGjeA0b-E")
    GMSPlacesClient.provideAPIKey("AIzaSyARsPfNs54hrUAC4yKtHeu2jXfGjeA0b-E ")
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
    mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    view = mapView

    // creating the marker //

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView

2 个答案:

答案 0 :(得分:0)

我认为您错过了在Info.plist文件中添加键 NSLocationWhenInUseUsageDescription

将此密钥添加到用法说明中。 然后应显示一个iOS系统弹出窗口以允许位置访问。

答案 1 :(得分:0)

我已经阅读了您的问题,并检查了给定的Map Tutorial链接。我发现您的viewDidLoad方法中缺少一些代码:

locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.distanceFilter = 50
locationManager.startUpdatingLocation()
locationManager.delegate = self

placesClient = GMSPlacesClient.shared()

没有像 CLLocationManager

那样将委托分配给locationManager.delegate = self的对象

我认为您的分机号都不会被称为( CLLocationManagerDelegate )。

按照@ Let's_Create 的答案,info.plist机架具有以下键,用于向用户说明应用程序如何使用它。 dataNSLocationAlwaysAndWhenInUseUsageDescriptionNSLocationWhenInUseUsageDescription