I'm trying to have an info box, same one as the standard for markers, to appear when a my polyline is tapped. I've gotten a NSLog to output when the line is tapped, but now I need to have the infobox appear instead of the NSLog. I've seen some Javascript examples but no objective c ones.
- (void)loadView {
// Create a GMSCameraPosition
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:37.551927
longitude:-77.456292
zoom:18];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(37.551709, -77.456510);
mapView.settings.myLocationButton = YES;
mapView.myLocationEnabled = YES;
self.view = mapView;
mapView.delegate = self;
GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:CLLocationCoordinate2DMake(37.552243, -77.457415)];
[path addCoordinate:CLLocationCoordinate2DMake(37.551054, -77.455443)];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
UILabel *myLabel = [[UILabel alloc] init];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelTapped)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[myLabel addGestureRecognizer:tapGestureRecognizer];
myLabel.userInteractionEnabled = YES;
polyline.spans = #[[GMSStyleSpan spanWithColor:[UIColor greenColor]]];
polyline.strokeWidth = 5.f;
polyline.tappable = true;
polyline.map = mapView;
}
- (void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay
{
NSLog(#"in didTapOverlay");
}
#end
Check the Events guide from the iOS Maps tutorial.
Here's a snippet in Objective-C:
- (void)loadView {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:1.285
longitude:103.848
zoom:12];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView.delegate = self;
self.view = mapView;
}
#pragma mark - GMSMapViewDelegate
- (void)mapView:(GMSMapView *)mapView
didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
NSLog(#"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
}
Here's an SO thread demonstrating the use of GMSMapViewDelegate:
1.conform to the GMSMapViewDelegate protocol.
#interface YourViewController () <GMSMapViewDelegate>
// your properties
#end
2.set your mapView_ delegate.
mapView_.delegate = self;
3.implement the GMSMapViewDelegate method
- (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker {
// your code
}
Addition: marker.userData is useful. you can set your needed data into it and use it in - mapView:didTapInfoWindowOfMarker:
Related
I am new in iOS and I am facing problem regarding to show India maps on Google Maps
My code is like this first
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:18.516726
longitude:73.856255
zoom:3];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//Scrollview
mapView = [GMSMapView mapWithFrame:CGRectMake(10, 140, 750, 350) camera:camera];
}
else
{
//Scrollview
mapView = [GMSMapView mapWithFrame:CGRectMake(10, 140, 550, 350) camera:camera];
}
But When I used UIView and create it using IBOutlet like this
IBOutlet GMSMapView *mapView;
Than the above code not work.How can I show the India map first by using above coordinate.Thanks in Advance!
Get current Location coordinate and Pass on these camera position
CLLocationCoordinate2D coordinate;
float latitude;
float longitude;
coordinate = [self getLocation];
latitude = coordinate.latitude;
longitude = coordinate.longitude;
//pragma mark ----: GET Coordinate2D :----
-(CLLocationCoordinate2D) getLocation
{
CLLocation *location = [_locationManager location];
coordinate = [location coordinate];
return coordinate;
}
CGRect frame = CGRectMake(0, 0, kSCREEN_WIDTH,kSCREEN_HEIGHT);
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude longitude:longitude zoom:12];
mapView_ = [GMSMapView mapWithFrame:frame camera:camera];
mapView_.myLocationEnabled = YES;
mapView_.delegate = self;
[mapView_ setCamera:camera];
Below is a Sample Code:
#import <GoogleMaps/GoogleMaps.h>
#interface ViewController ()<GMSMapViewDelegate>
{
GMSMapView *mapView;
GMSMarker *marker;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self showMarker];
}
- (void)showMarker
{
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:18.516726 longitude:73.856255 zoom:3];
mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
[self.view addSubview:mapView];
}
#end
Try This code:
#interface ViewController ()<GMSMapViewDelegate>
{
GMSMapView *mapView_;
}
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:18.516726
longitude:73.856255
zoom:3];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//Scrollview
mapView_ = [GMSMapView mapWithFrame:CGRectMake(10, 140, 750, 350) camera:camera];
}
else
{
//Scrollview
mapView_ = [GMSMapView mapWithFrame:CGRectMake(10, 140, 550, 350) camera:camera];
}
NSError *error;
// Set the map style by passing a valid JSON string.
GMSMapStyle *style = [GMSMapStyle styleWithJSONString:kMapStyle error:&error];
if (!style) {
NSLog(#"The style definition could not be loaded: %#", error);
}
mapView_.mapStyle = style;
mapView_.mapType = kGMSTypeNormal;
[self.scrllView addSubview:mapView_];
[_btnMap setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
mapView_.delegate = self;
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(
18.516726,
73.856255);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.map = mapView_;
marker.icon=[UIImage imageNamed:#"locationBlue"];
marker.title = #"From";
I'm using a google map in iPhone app by objective-c, it is ok and the marker is in a given location. I want when click on a specific location change the marker location to the selected location by clicking the map.
How to do this?
Thanks in advance.
my code:
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:29.964996
longitude:30.939680 zoom:5
bearing:0
viewingAngle:0
];
_mapView = [GMSMapView mapWithFrame:viewOfMap.bounds camera:camera];
_mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth|
UIViewAutoresizingFlexibleHeight;
[_mapView addObserver:self
forKeyPath:#"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
[viewOfMap addSubview:_mapView];
dispatch_async(dispatch_get_main_queue(), ^{
//_mapView.myLocationEnabled = YES;
_mapView.myLocationEnabled = YES;
});
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(lat, lag);
marker.map = _mapView;
- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker
{
marker.icon=[UIImage imageNamed:#"selectedicon.png"];//selected marker
for (int i=0; i<[markerArray count]; i++)
{
GMSMarker *unselectedMarker=markerArray[i];
//check selected marker and unselected marker position
if(unselectedMarker.position.latitude!=marker.position.latitude && unselectedMarker.position.longitude!=marker.position.longitude)
{
unselectedMarker.icon=[UIImage imageNamed:#"unselectedicon.png"];
}
}
return NO;
}
If I understand your question, you want to tap a location and have a marker move to that location.
Be sure that your view controller implements GMSMapViewDelegateand use the didTapAtCoordinate delegate method. Something like this:
-(void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
[marker setPosition:coordinate];
}
For some reason, I can't seem to get tap and longtap and all the other delegate events to work in my simulator. I think I installed everything correctly because the map shows up on the screen, with a custom button click I can put a marker on the map, but it will not recognize the delegate events. Am I doing something wrong???
mapviewcontroller.h
#import <UIKit/UIKit.h>
#import <GoogleMaps/GoogleMaps.h>
#import <CoreLocation/CoreLocation.h>
#interface MapViewController : UIViewController <GMSMapViewDelegate, CLLocationManagerDelegate>
#property (weak, nonatomic) IBOutlet UIView *mapContainer;
- (IBAction)addmarker:(id)sender;
#end
mapviewcontroller.m
#import "MapViewController.h"
#interface MapViewController ()
#end
#implementation MapViewController{
GMSMapView *mapView;
}
#synthesize mapContainer;
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.delegate = self;
mapView.myLocationEnabled = YES;
mapView.settings.myLocationButton = YES;
mapView.settings.compassButton = YES;
//CLLocation *myLoc = mapView.myLocation;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 longitude:151.20 zoom:15];
mapView = [GMSMapView mapWithFrame:CGRectMake(0, 0, self.mapContainer.frame.size.width, self.mapContainer.frame.size.height) camera:camera];
[self.mapContainer addSubview:mapView];
}
- (void) mapView: (GMSMapView *) mapView didTapAtCoordinate: (CLLocationCoordinate2D) coordinate{
NSLog(#"%f, %f", coordinate.latitude, coordinate.longitude);
}
- (void) mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate{
NSLog(#"tapped");
}
- (void) mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker{
NSLog(#"tapped info");
}
- (void) mapView:(GMSMapView *)mapView didBeginDraggingMarker:(GMSMarker *)marker{
NSLog(#"dragged");
}
- (IBAction)addmarker:(id)sender {
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(-33.86, 151.20);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = #"hello";
marker.map = mapView;
}
#end
The only thing that works is the IBAction. Also my location and compass buttons do not show up. Any ideas of why it's not working? I declared the delegates...
You need to set all properties after the object initialization.
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 longitude:151.20 zoom:15];
mapView = [GMSMapView mapWithFrame:CGRectMake(0, 0, self.mapContainer.frame.size.width, self.mapContainer.frame.size.height) camera:camera];
mapView.delegate = self;
mapView.myLocationEnabled = YES;
mapView.settings.myLocationButton = YES;
mapView.settings.compassButton = YES;
[self.mapContainer addSubview:mapView];
}
I have a viewcontroller in a storyboard with a View in it that loads googlemaps. Markers are however, not showing up!
What have I done wrong?
Here is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createBasicGSMView];
}
- (IBAction)AddPointButtonPressed:(id)sender {
// Creates a marker in the center of the map.
GMSMarker *CurrentLocationMarker = [[GMSMarker alloc] init];
CLLocation *CurrentLocationNote = mapView_.myLocation;
CurrentLocationMarker.position = CLLocationCoordinate2DMake(CurrentLocationNote.coordinate.latitude, CurrentLocationNote.coordinate.longitude);
CurrentLocationMarker.title = #"Current Location";
CurrentLocationMarker.snippet = #"This is the place to be";
[CurrentLocationMarker setMap:mapView_];
}
- (void) createBasicGSMView{
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:1];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.mapViewHolder = mapView_;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = #"Sydney";
marker.snippet = #"Australia";
marker.map = mapView_;
}
Figured it out. had to add the view as a subview:
[self.view addSubview:mapView_];
Doesnt work otherwise!
I want to add a google map above a UIViewController. But I can't see anything except some markers I defined. The problem looks like this:
And here is the related code:
- (void)viewDidLoad {
previousContext = [EAGLContext currentContext];
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:10];
mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, 1136, 640) camera:camera];
//mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.settings.compassButton = YES;
mapView_.settings.myLocationButton = YES;
mapView_.delegate = self;
// Listen to the myLocation property of GMSMapView.
[mapView_ addObserver:self
forKeyPath:#"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
[EAGLContext setCurrentContext: previousContext];
//add makers
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(39.900285, 116.274020);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = #"aaa";
marker.snippet = #"population : 5";
marker.infoWindowAnchor = CGPointMake(0.5, 0.5);
marker.map = mapView_;
marker.icon = [UIImage imageNamed:#"tempmarker.png"];
CLLocationCoordinate2D position2 = CLLocationCoordinate2DMake(39.860285, 116.274020);
GMSMarker *marker2 = [GMSMarker markerWithPosition:position2];
marker2.title = #"bbb";
marker2.snippet = #"population : 5";
marker2.infoWindowAnchor = CGPointMake(0.5, 0.5);
marker2.map = mapView_;
marker2.icon = [UIImage imageNamed:#"tempmarker2.png"];
//add buttons:directRoom, rank, achievement, mission, home
....
[self.view insertSubview: mapView_ atIndex: 0];
[EAGLContext setCurrentContext:nil];
// Ask for My Location data after the map has already been added to the UI.
dispatch_async(dispatch_get_main_queue(), ^{
mapView_.myLocationEnabled = YES;
});
}
So, how to deal with this problem, thanks a lot!
For reference, see the demo from Google (copied below). It's similar to what you posted, but you can try a couple of things:
make sure your API key is valid and the API can fetch tiles
check the z-index ordering of the map view you're inserting: [self.view insertSubview: mapView_ atIndex: 0]; inserts behind everything else. Try [self.view addSubview:mapView_]
Demo code, from Google (https://developers.google.com/maps/documentation/ios/):
#import <GoogleMaps/GoogleMaps.h>
#import "DemoViewController.h"
#implementation DemoViewController
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:6];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = camera.target;
marker.snippet = #"Hello World";
marker.animated = YES;
self.view = mapView;
}
#end
Check whether you have entered the google API key correctly in the didFinishLaunchingWithOption in your app delegate
[GMSServices provideAPIKey:#"yourGoogleAssignedSDKKeyGoesHere"];
And your xcode should be 4.5 or later