将静态谷歌地图加载到 UIImageView 中?

发布于 2024-09-02 21:27:44 字数 635 浏览 3 评论 0原文

我正在尝试在我的应用程序中显示一个小型静态谷歌地图。我想知道是否可以使用 UIImageView 来做到这一点?它似乎对我不起作用,但也许我做错了什么。

这就是我所拥有的(我删除了 URL,以便更容易阅读):

NSURL *mapurl = [[NSURL alloc]    initWithString:@"http://maps.google.com/maps/api/staticmap?[...]"]; 
NSData *mapdata = [[NSData alloc] initWithContentsOfURL:mapurl];
UIImage *uimap = [[UIImage alloc] initWithData:mapdata];
self.map.image = uimap; // map is my UIImageView    
[mapurl release];   
[mapdata release];
[uimap release];

我知道我的 UIImageView 设置正确,因为如果我用一张图像替换 URL,那么它会加载正常。我还知道谷歌地图 URL 是正确的,因为如果我直接将其加载到浏览器中,它会显示得很好。

我开始认为这可能是不可能的。如果有人有任何想法或替代方案,请分享。

谢谢。

I'm trying to show a small static google map in my app. I'm wondering if it's possible to do this using UIImageView? It doesn't seem to be working for me but maybe I'm doing something wrong.

This is what I have (I stripped the URL so that it's easier to read):

NSURL *mapurl = [[NSURL alloc]    initWithString:@"http://maps.google.com/maps/api/staticmap?[...]"]; 
NSData *mapdata = [[NSData alloc] initWithContentsOfURL:mapurl];
UIImage *uimap = [[UIImage alloc] initWithData:mapdata];
self.map.image = uimap; // map is my UIImageView    
[mapurl release];   
[mapdata release];
[uimap release];

I know my UIImageView is setup correctly because if I replace the URL with one of an image then it loads fine. Also I know the google maps URL is correct because if I load it straight into a browser it shows up fine.

I'm starting to think that this might not be possible. If anyone has any ideas or alternatives please share.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

电影里的梦 2024-09-09 21:27:45

好吧,我最终找到了解决方案,因此我会将其发布,以防其他人遇到同样的问题。

首先,我将加载图像的方式更改为使用 NSURLConnection。我不认为这个特殊的改变有什么影响,但这就是让我发现问题的原因。当尝试加载 URL 时,我收到“错误的 url”错误。所以我对此进行了一些搜索,结果发现谷歌静态地图 URL 中有一些无效字符必须转义。

所以基本上只要在 url 上调用它,你就可以开始了:

// The URL you want to load (snipped for readability)
NSString *mapurl = @"http://maps.google.com/maps/api/staticmap?[...]";  

// Escape the string
mapurl = [mapurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

我还没有用我原来帖子中的代码测试这个解决方案,但我确实用我的新 NSURLConnection 实现测试了它,它工作得很好。我认为上面的代码也能正常工作,因为这只是转义字符串的问题。

Well I ended up finding a solution for this so I'll post it in case someone else runs into the same issue.

First I changed the way I load the image to using NSURLConnection. I don't think this particular change makes a difference but this is what allowed me to find the problem. When trying to load the URL I got a "bad url" error. So I did some searching on that and turns out there are some invalid characters in the google static map URL that must be escaped.

So basically just call this on the url and you should be good to go:

// The URL you want to load (snipped for readability)
NSString *mapurl = @"http://maps.google.com/maps/api/staticmap?[...]";  

// Escape the string
mapurl = [mapurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

I haven't tested this solution with the code I have in my original post but I did test it with my new NSURLConnection implementation and it works perfectly. I assume it'll work fine with the code above as well since it was just a matter of escaping the string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文