I'm try to adding marker on run time then press button. Button method call and code run but no marker show on map. if i add marker on adding subview time then show perfectly but i need to different case on button press.
Button Click (Not Showing any maker on Map)
-(IBAction)addingMarkerOnMap:(id)sender
{
dispatch_async(dispatch_get_main_queue(), ^{
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
GMSMarker *london = [GMSMarker markerWithPosition:position];
london.title = #"London";
london.icon = [UIImage imageNamed:#"pinIcon"];
london.map = self.mapView;
});
}
Adding Time (Showing marker)
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:self.currentLocation.coordinate.latitude
longitude:self.currentLocation.coordinate.longitude
zoom:16];
self.mapView = [GMSMapView mapWithFrame:self.view.frame camera:camera];
self.mapView.myLocationEnabled = YES;
[self.mapView setDelegate:self];
self.mapView.mapType = kGMSTypeSatellite;
[self.googleMapView addSubview:self.mapView];
self.marker = [[GMSMarker alloc] init];
self.marker.position = CLLocationCoordinate2DMake(43.649580, -79.385440);
self.marker.title = #"Toronto";
self.marker.snippet = #"Canada";
self.marker.map = self.mapView;
Please use this code on a demo project then check in your own project
#IBOutlet var mapView: GMSMapView!
//use the following code in you button action
let state_marker = GMSMarker()
let long = //Double long
let lat = //Double lat
state_marker.position = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
state_marker.map = mapView
// add in subview if you want to add, or place a view on story board and assign GMSMapView to this
Related
I'm use button for adding pin on specific location and my map already load and showing on screen. then press button not response even marker create and assign coordinates as well.
-(IBAction)addingMarkerOnMap:(id)sender
{
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
GMSMarker *london = [GMSMarker markerWithPosition:position];
london.title = #"London";
london.icon = [UIImage imageNamed:#"Standard"];
london.map = self.mapView;
}
Try this code:
-(IBAction)addingMarkerOnMap:(id)sender
{
dispatch_async(dispatch_get_main_queue(), ^{
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
GMSMarker *london = [GMSMarker markerWithPosition:position];
london.title = #"London";
london.icon = [UIImage imageNamed:#"Standard"];
london.map = self.mapView;
});
}
I want the GMSMarker to move to camera. this is my code
-(void)addMap {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude
longitude:longitude
zoom:15];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
CGRect newFrame = CGRectMake( 0, 0, [Util window_width],[Util window_height]);
mapView = [GMSMapView mapWithFrame:newFrame camera:camera];
mapView.delegate = self;
mapView.myLocationEnabled = YES;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = camera.target;
marker.snippet = #"Hello World";
marker.icon =[Util imageWithImage:[UIImage imageNamed:#"set_address.png"] scaledToSize:CGSizeMake(170, 65)];
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = mapView;
[self.MapContentView addSubview:mapView];
}
I have a marker on the map, normally it is located at the center of the map if user does not scroll the map.
When the user scroll the map, I want marker to move to the new location, along with the camera , so it is always centered of the map .
I try the following code for it
-(BOOL) mapView:(GMSMapView *) mapView didTapMarker:(GMSMarker *)marker
{
[mapView animateToLocation:marker.position];
return YES;
}
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position
{
marker.position = camera.target;
marker.appearAnimation = kGMSMarkerAnimationPop;
NSLog(#"camera.target.latitude %f,%f",camera.target.latitude,camera.target.longitude);
latitude=camera.target.latitude;
longitude=camera.target.longitude;
}
You can try this:
- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture
{
[self recenterMarkerInMapView:mapView];
}
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position
{
[self recenterMarkerInMapView:mapView];
}
- (void)recenterMarkerInMapView:(GMSMapView *)mapView
{
// Get the center of the mapView
CGPoint center = [mapView convertPoint:mapView.center fromView:self.view];
// Reset the marker position so it moves without animation
[mapView clear];
marker.appearAnimation = kGMSMarkerAnimationNone;
marker.position = [mapView.projection coordinateForPoint:center];
marker.map = mapView;
}
Swift 4. To move the selected marker to centre of mapView.
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
mapView.animate(toLocation: marker.position)
return true
}
I have setup a UIView to display a specific region of a Map using Google Maps. I now want to add an image overlay on top of this selected region but I dont know how to calculate the correct coordinates for this. I have set the map with a center coordinate, but the Overlay needs NorthWest and South East coordinates. Can someone help please? I am trying to put an image of a race track over some roads.
Below is my code so far:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:14.5809268
longitude:120.975319
zoom:16.5
bearing:90
viewingAngle:0];
// Indicating the map frame bounds
mapView_ = [GMSMapView mapWithFrame:self.mapViewOnScreen.bounds camera: camera];
mapView_.myLocationEnabled = YES;
// Add as subview the mapview
[self.mapViewOnScreen addSubview: mapView_];
//this is where I need to figure out the coordinates but get stuck...
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(40.712216,-74.22655);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(40.773941,-74.12544);
GMSCoordinateBounds *overlayBounds = [[GMSCoordinateBounds alloc] initWithCoordinate:southWest
coordinate:northEast];
//Add track image over the road map to show a race track - roads need to match up
UIImage *icon = [UIImage imageNamed:#"Track.jpg"];
GMSGroundOverlay *overlay =
[GMSGroundOverlay groundOverlayWithBounds:overlayBounds icon:icon];
overlay.bearing = 0;
overlay.map = mapView_;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(14.5809268, 120.975319);
marker.title = #"Race Day";
marker.snippet = #"Manila";
marker.map = mapView_;
}
I haven't tried this, but after doing some research on the API, I think you can achieve what you want by doing the following:
GMSProjection *projection = mapView_.projection;
GMSVisibleRegion visibleRegion = [projection visibleRegion];
GMSCoordinateBounds *coordinateBounds = [[GMSCoordinateBounds alloc] initWithRegion:visibleRegion];
CLLocationCoordinate2D northEast = coordinateBounds.northEast;
CLLocationCoordinate2D southWest = coordinateBounds.southWest;
Hope this helps.
I'm trying to implement current locations in my app. Please see my code for the viewDidLoad below:
- (void)viewDidLoad {
[super viewDidLoad];
[self hideTabBar:self.tabBarController];
// 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:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
mapView_.settings.compassButton = YES;
mapView_.settings.myLocationButton = YES;
mapView_.accessibilityElementsHidden = NO;
self.view = 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_;
}
I can see the map in the iOS simulator (iPhone 4s) with the marker set to Sydney, but no blue dot to be seen anywhere. Is current location perhaps working but the UI element is hidden?
Thanks for any help/suggestions!
In my app I need to implement the CLLocationManager and set NSLocationWhenInUseUsageDescriptionin the Custom Target Properties.
Hope this helps.
In simulator, you need to set custom location like following:
Debug > Locations > Custom Location... then enter a latitude and longitude for the location you would like to simulate.
NOTE: if this method didn't work, then just select Freeway Drive instead of Custom Location. Once you clicked on Freeway Drive, then do the same step as above mentioned.
func didTapMyLocationButton(for mapView: GMSMapView) -> Bool {
//if want to change the camera position
if self.latDouble != 0.0 && self.longDouble != 0.0
{
let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: self.latDouble, longitude: self.longDouble, zoom: 18.0)
self.mapView.camera = camera
}
self.mapView.isMyLocationEnabled = true
self.mapView.reloadInputViews()
return true
}
I am trying to make my google maps map view only half of the screen. I have resized the map view to be half the screen and also changed my code so it's only the bounds of the map view which is half the screen but it still goes full screen. Anyone got a solution?
// setting up mapView
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
_mapView = [GMSMapView mapWithFrame:_mapView.bounds camera:camera];
_mapView.myLocationEnabled = YES;
self.view = _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;
// Creates a marker in the center of the map.
GMSMarker *marker2 = [[GMSMarker alloc] init];
marker2.position = CLLocationCoordinate2DMake(-36.86, 151.20);
marker2.title = #"Sydney2";
marker2.snippet = #"Australia2";
marker2.map = _mapView;
Thanks,
Curtis
You may can try to add the _mapView as a subview instead of assigning it to self.view.
[self.view addSubview:_mapView];
You can do it with the help of storyboard.
Drag a UIView in your ViewController and add GMSMapView class to the
UIView.
Set the frame size for your UIView using storyboard. Create outlet
for the UIView( name it mapView) and connect it.
In your ViewController class under viewDidLoad add these two lines
two view the google map.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: YOUR_LATITUDE
longitude: YOUR_LONGITUDE zoom:8];
[self.mapView setCamera:camera];
Add marker to the location using following code.
GMSMarker *marker = [[GMSMarker alloc]init];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(YOUR_LATITUDE, YOUR_LONGITUDE);