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];
}
Related
In Android on tapping on a marker two buttons show on the screen at the bottom for direction and opening google maps. However, on iOS tapping marker only shows info window.
I need to show the info window and directions and google maps buttons on tapping marker just as in Android.
My code for adding markers:
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = [(CLLocation*)dict[#"position"] coordinate];
marker.icon = [GMSMarker markerImageWithColor:[UIColor ubnBlue]];
marker.title = dict[#"title"];
marker.snippet = dict[#"snippet"];
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.tappable = YES;
marker.map = mapView_;
First set GMSMapViewDelegate method
- (BOOL) mapView: (GMSMapView *)mapView didTapMarker:(GMSMarker *)marker
{
[mapView setSelectedMarker:marker];
return YES;
}
- (UIView *GMS_NULLABLE_PTR)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker
{
NSLog(#"The amrker snippet is - %#",marker.snippet);
if([marker.snippet isEqualToString:#"your string"]) //check here
{
....//Do the stuff here
}
}
First, you need to set the delegate method in yourController.h file then you can handle it by using the method
- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
return Yes;
}
to show the info window you need to call a marker.title = #"title";
GMSMarker has property iconView. You can assign UIView to iconView.
Implement GMSMapViewDelegate delegate methods
(BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
}
Now you can change your view as you need.
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:
I set the marker on google map but when i drag it's all of map are drag.
I want drag marker when click and drag on it's
and drag map when click and drag outside marker.
this is my code
self.camera = [GMSCameraPosition cameraWithLatitude:-33.86 longitude:151.20 zoom:6 bearing:0 viewingAngle:0];
self.Map = [GMSMapView mapWithFrame:self.MapView.bounds camera:self.camera];
self.Map.myLocationEnabled = YES;
self.Map.delegate = self;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = self.camera.target;
marker.draggable = YES;
marker.title = #"Sydney";
marker.snippet = #"Australia";
marker.map = self.Map;
marker.icon = [GMSMarker markerImageWithColor:[UIColor blueColor]];
marker.appearAnimation = kGMSMarkerAnimationPop;
[self.MapView addSubview:self.Map];
and this is event on drag drop
- (void) mapView:(GMSMapView *)mapView didBeginDraggingMarker:(GMSMarker *)marker
{
}
- (void) mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker
{
}
- (void) mapView:(GMSMapView *)mapView didDragMarker:(GMSMarker *)marker
{
}
when i run my app and debug all above event not work.
but event click on marker work well.
how i implement drag drop ?
You have to press-and-hold on the marker before it will begin dragging. I thought it wasn't working either until someone pointed this out... really needs to be in Google's documentation.
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
I am currently working with Google Maps API in my project. I am trying to set the default camera/zoom to the users location. I do this:
#implementation ViewController{
GMSMapView *mapView_;
}
#synthesize currentLatitude,currentLongitude;
- (void)viewDidLoad
{
[super viewDidLoad];
mapView_.settings.myLocationButton = YES;
mapView_.myLocationEnabled = YES;
}
- (void)loadView{
CLLocation *myLocation = mapView_.myLocation;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude);
marker.title = #"Current Location";
marker.map = mapView_;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude
longitude:myLocation.coordinate.longitude
zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view = mapView_;
NSLog(#"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude);
}
However, it does not work, since when I do
NSLog(#"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude);
it returns 0, 0, and it does not give the current location coordinates. How can I properly get the user's coordinates?
.h
#import <CoreLocation/CoreLocation.h>
#property(nonatomic,retain) CLLocationManager *locationManager;
.m
- (NSString *)deviceLocation
{
NSString *theLocation = [NSString stringWithFormat:#"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
return theLocation;
}
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
answered here.
When an app first starts it may not yet know your location, as it usually takes a while for the GPS device to lock on to your location (if it has just been started), and especially if this is the first time the application has been run, and so the user hasn't yet answered the prompt to give the app access to their location. Also it seems like mapView.myLocation is always empty (either nil or has coordinates 0,0) when the map view has just been created.
So you will need to wait until the user's location is known, and then update the camera position.
One way might be using the code at how to get current location in google map sdk in iphone as suggested by Puneet, but note that the sample code there is missing the details of setting up the location manager (like setting the location manager's delegate), which might be why it didn't work for you.
Another option could be to use KVO on mapView.myLocation, as described here: about positioning myself,some problems
By the way in your sample code you are accessing mapView.myLocation before you create the mapView, and so the location would always be nil anyway.
I just Downloaded the new GoogleMap SDK Demo for iOS. Here is what I have seen from the source code that how the "Current Location" is achieved via KVO.
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
#import "SDKDemos/Samples/MyLocationViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#implementation MyLocationViewController {
GMSMapView *mapView_;
BOOL firstLocationUpdate_;
}
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:12];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.settings.compassButton = YES;
mapView_.settings.myLocationButton = YES;
// Listen to the myLocation property of GMSMapView.
[mapView_ addObserver:self
forKeyPath:#"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
self.view = mapView_;
// Ask for My Location data after the map has already been added to the UI.
dispatch_async(dispatch_get_main_queue(), ^{
mapView_.myLocationEnabled = YES;
});
}
- (void)dealloc {
[mapView_ removeObserver:self
forKeyPath:#"myLocation"
context:NULL];
}
#pragma mark - KVO updates
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (!firstLocationUpdate_) {
// If the first location update has not yet been recieved, then jump to that
// location.
firstLocationUpdate_ = YES;
CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
zoom:14];
}
}
#end
Hope it can help you.
Just in case anyone wants DrDev's excellent answer in Swift 3:
var locationManager: CLLocationManager?
func deviceLocation() -> String {
let theLocation: String = "latitude: \(locationManager.location.coordinate.latitude) longitude: \(locationManager.location.coordinate.longitude)"
return theLocation
}
func viewDidLoad() {
locationManager = CLLocationManager()
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
// 100 m
locationManager.startUpdatingLocation()
}