UIView Black Screen in Google Maps iOS - ios

After joining the Google Maps project covers the default application screen, which are already added objects. Trying to implement it to a UIView application screen turns black.
I would like to add it to the element UIView.
Please help.
.h
#property (weak, nonatomic) IBOutlet UIView *mapViewWithFrame;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *btnLocate;
#property (weak, nonatomic) IBOutlet GMSMapView *mapView_;
.m
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:54.104382
longitude:22.9403031
zoom:15];
self.mapView_ = [GMSMapView mapWithFrame:self.mapView.bounds camera: camera];
self.mapView_.myLocationEnabled = YES;
// Add as subview the mapview
[self.mapViewWithFrame addSubview: self.mapView_];

I am also get the same issue. im using this code getting correctly, try this
1) Add subview in uiviewcontroller
2) Set custom class name in "GMSMapView" in identity inspector
3) add .h file in this code
#property (strong, nonatomic) IBOutlet GMSMapView *mapView;
4) in .m file add this code in viewDidLoad
-(void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: 11.066853 longitude: 76.9460043 zoom: 17];
[self.mapView animateToCameraPosition:camera];
GMSMarker *marker = [ [GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(11.066853, 76.9460043);
marker.title = #"Tet Festival 2015";
marker.snippet = #"VAYA Tet";
marker.map = self.mapView;
self.mapView.mapType = kGMSTypeNormal;
}
5) uiviewcontroller should mapping the uiview in "GMSMapView" this object

Related

UISearchBar not working even with delegate set in viewDidLoad

The searchBarBookmarkButtonClicked method isn't firing when I tap on the search bar. I have already added the self.searchBar.deleagate = self; and in my .h file I added the <UISearchBarDelegate>.
.h file:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ViewController : UIViewController <UISearchBarDelegate,MKMapViewDelegate>
#property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
#property (strong, nonatomic) IBOutlet MKMapView *myMap;
#end
.m file:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.searchBar.delegate = self;
self.myMap.delegate = self;
}
- (void)searchBarBookmarkButtonClicked:(UISearchBar * )searchBar{
NSLog(#"working");
[self.searchBar resignFirstResponder];
NSLog(#"working");
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:self.searchBar.text completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
MKCoordinateRegion region;
CLLocationCoordinate2D newLocation = [placemark.location coordinate];
region.center = [(CLCircularRegion *)placemark.region center];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:newLocation];
[annotation setTitle:self.searchBar.text];
[self.myMap addAnnotation:annotation];
MKMapRect mr = [self.myMap visibleMapRect];
MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
mr.origin.x = pt.x - mr.size.width * 0.5;
mr.origin.y = pt.y -mr.size.width * 0.25;
[self.myMap setVisibleMapRect:mr animated:YES];
}];
}
#end
Make sure you have
All delegate being set properly, this can be done either in code or
in storyboard.
Make use of correct delegate method to capture the
event.
If you think
- (void)searchBarBookmarkButtonClicked:(UISearchBar * )searchBar
then try using the other method which is more straight forward to trigger search
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
Try this UISearchBar delegate method:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
Thank you #Rahul! Turns out I was using the wrong method!
.h:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ViewController : UIViewController <UISearchBarDelegate,MKMapViewDelegate>
#property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
#property (strong, nonatomic) IBOutlet MKMapView *myMap;
#end
.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.searchBar.delegate = self;
self.myMap.delegate = self;
}
- (void)searchBarBookmarkButtonClicked:(UISearchBar * )searchBar{
[self.searchBar resignFirstResponder];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:self.searchBar.text completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
MKCoordinateRegion region;
CLLocationCoordinate2D newLocation = [placemark.location coordinate];
region.center = [(CLCircularRegion *)placemark.region center];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:newLocation];
[annotation setTitle:self.searchBar.text];
[self.myMap addAnnotation:annotation];
MKMapRect mr = [self.myMap visibleMapRect];
MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
mr.origin.x = pt.x - mr.size.width * 0.5;
mr.origin.y = pt.y -mr.size.width * 0.25;
[self.myMap setVisibleMapRect:mr animated:YES];
}];
}
#end

Unable to navigate Google Map to location

I am using Google Maps in iOS. I have added map in subview. But animateToLocation method doesn't work although it works fine if I assign mapView to whole view with
self.view = mapView_
[mapView_ animateToLocation:CLLocationCoordinate2DMake(-33.868, 151.208)];
I found solution by change outlet type GMSMapView and setting camera in viewDidLoad method.
in .h file
#property (weak, nonatomic) IBOutlet GMSMapView *mapView;
in viewDidLoad
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 longitude:131.20 zoom:6];
self.mapView.camera = camera;
You can try out the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:131.20
zoom:6];
self.map1.camera = camera;

Google Maps SDK not displaying properly in UIView?

I'm having some problems displaying google maps in Xcode 6.1
I managed to get the map to display in a section of my UIView, but it displays the world map instead of at a particular coordinate.
Here is the issue:
.h:
#interface MapViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIView *googleMapView;
- (IBAction)mapToMain:(UIButton *)sender;
#end
.m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: 32.915134
longitude: -117.140269 zoom: 17];
GMSMapView *mapView = [GMSMapView mapWithFrame:self.googleMapView.bounds
camera:camera];
mapView.myLocationEnabled = YES;
self.googleMapView = mapView;
GMSMarker *marker = [ [GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(32.915134, -117.140269);
marker.title = #"Tet Festival 2015";
marker.snippet = #"VAYA Tet";
marker.map = mapView;
}
I saw some suggestions here, but it didn't work:
Cannot put a google maps GMSMapView in a subview of main main view?
Using the Google Maps SDK in views other than the main view
Any suggestions?
You have to add a UIView to your Storyboard... but after that you have to turn this simple UIView into a GMSMapView! Select the view you just added and open the Identity Inspector by selecting the third tab from the left in the Utilities toolbar, change the view’s class name from UIView to GMSMapView, as shown in the screenshot below:
Your Outlet will not be like this
#property (strong, nonatomic) IBOutlet UIView *googleMapView;
it will be like this
#property (strong, nonatomic) IBOutlet GMSMapView *mapView;
Your ViewDidLoad can be like this:
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: 32.915134
longitude: -117.140269 zoom: 17];
[self.mapView animateToCameraPosition:camera];
GMSMarker *marker = [ [GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(32.915134, -117.140269);
marker.title = #"Tet Festival 2015";
marker.snippet = #"VAYA Tet";
marker.map = self.mapView;
UPDATE
Here you have an example project (insert your Key)
Finally figured it out...
removed:
self.googleMapView = mapView;
replaced:
[self.googleMapView addSubview:mapView];
I am not sure what is wrong with the code above. It looks fine for me. But to move the camera to certain location. You can animate the camera there as well.
var newLocation = CLLocationCoordinate2DMake(latitude, longitude)
googleMapView.animateToLocation(newLocation)
Hope that it works for you.

MKMapView alloc initWithFrame crash

I'm trying to add a map to my ViewController but it crashes on the init method. This is my code:
In TAMap.h:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface TAMap : UIViewController<MKMapViewDelegate>
#property (nonatomic, strong) MKMapView *mapView;
#end
In TAMap.m:
#implementation TAMap
#synthesize mapView = _mapView;
- (void)viewDidLoad {
[super viewDidLoad];
// Crashes here:
self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
I'm using ARC and my code seems to be entirely generic. I looked around and everywhere it's the same code for adding a map view. And yet it crashes with EXE_BAD_ACCESS. What am I missing?
My working code:
- (void)viewDidLoad
{
[super viewDidLoad];
MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
[self.view addSubview:mapView];
}

the map could not show my location

I want to display a map and show the location. My code is running well, but the location could not be displayed on the map. I do not know how to do it.
Well here are my classes:
ViewController.h
#interface ViewController :
UIViewController<AGSMapViewLayerDelegate,CLLocationManagerDelegate>
{
AGSMapView *_mapView;
AGSGraphicsLayer *_graphicsLayer;
AGSTiledMapServiceLayer *_tiledLayer;
CLLocationManager *locationManager;
CLLocation *startingPoint;
}
#property (nonatomic,retain) IBOutlet AGSMapView *mapView;
#property (nonatomic, retain) AGSTiledMapServiceLayer *tiledLayer;
#property (nonatomic, retain) CLLocationManager *locationManager;
#property (nonatomic, retain) AGSGraphicsLayer *graphicsLayer;
#property (nonatomic, retain) CLLocation *startingPoint;
ViewController.m
#interface ViewController()
#end
#implementation ViewController
#synthesize mapView = _mapView;
#synthesize graphicsLayer = _graphicsLayer;
#synthesize tiledLayer = _tiledLayer;
#synthesize locationManager;
#synthesize startingPoint;
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.layerDelegate =self;
self.graphicsLayer = [AGSGraphicsLayer graphicsLayer];
[self.mapView addMapLayer:self.graphicsLayer withName:#"graphicsLayer"];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self; // send loc updates to myself
self.locationManager.distanceFilter = 1000.0f; // 1 kilometer
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
}
- (void)mapViewDidLoad:(AGSMapView *)mapView{
[self.locationManager startUpdatingLocation]; //启用位置监控
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
self.startingPoint = [locations lastObject];
[self.locationManager stopUpdatingLocation]; //获取位置信息
CGPoint coord;
coord.x = startingPoint.coordinate.longitude;
coord.y = startingPoint.coordinate.latitude;
//将坐标用AGSPoint来表示
AGSPoint *mappoint = [[AGSPoint alloc]initWithX:coord.x y:coord.y spatialReference:nil];
//[self.graphicsLayer removeAllGraphics];
AGSPictureMarkerSymbol *pt;
pt = [AGSPictureMarkerSymbol pictureMarkerSymbolWithImageNamed:#"LocationDisplay.png"];
AGSGraphic *LocationDisplay = [[AGSGraphic alloc] initWithGeometry:mappoint symbol:pt attributes:nil infoTemplateDelegate:nil];
// 添加要素到GraphicsLayer
[self.graphicsLayer addGraphic:LocationDisplay];
[self.graphicsLayer refresh];
//漫游到指定级别
[self.mapView centerAtPoint:mappoint animated:YES];
int levelToZoomTo = 12;
AGSLOD* lod = [self.tiledLayer.mapServiceInfo.tileInfo.lods objectAtIndex:levelToZoomTo];
float zoomFactor = lod.resolution/self.mapView.resolution;
AGSMutableEnvelope *newEnv = [AGSMutableEnvelope envelopeWithXmin:self.mapView.visibleAreaEnvelope.xmin
ymin:self.mapView.visibleAreaEnvelope.ymin
xmax:self.mapView.visibleAreaEnvelope.xmax
ymax:self.mapView.visibleAreaEnvelope.ymax
spatialReference:self.mapView.spatialReference];
[newEnv expandByFactor:zoomFactor];
[self.mapView zoomToEnvelope:newEnv animated:YES];
}
You need to provide a spatial reference when you create the AGSPoint from the CLLocation coordinates. Use [AGSSpatialReference wgs84SpatialReference]
If the map is not using WGS84 spatial reference then you need to reproject the point to the map spatial reference before you add it to the map using the geometry engine.
OR
you can use the locationDisplay property built into AGSMapView, by setting mapView.locationDisplay.startDataSource to display your current location.
See the docs for AGSMapView
func mapViewDidLoad(mapView: AGSMapView!) {
//do something now that the map is loaded
//for example, show the current location on the map
mapView.locationDisplay.startDataSource()
}
Try setting mapview.showUserLocation = YES ; or set it in XIB

Resources