How to get CGPoint of location with google maps sdk for iOS? - google-maps-sdk-ios

How to get CGPoint of location with google maps sdk for iOS ?
I need add pinview on user location.

It's basically the opposite of this question, for example:
GMSMapView* mapView = ...;
CLLocationCoordinate2D coordinate = ...;
...
CGPoint point = [mapView.projection pointForCoordinate: coordinate];

Ok, according to your question do you want to convert the userlocation into the cgpoint, is that..
try use this.
CGPoint newCenter = [self.map convertCoordinate:coord toPointToView:self.map];
Regards.

Related

know if a marker is contained within a polygon in ionic

I have a doubt, beyond looking for the code that solves my problem. Is it possible from the native cordova-plugin-googlemaps to perform this type of calculations? or should I import the javascript script from google maps? and would the google maps functions on the native google maps plugin? thank you very much
Use poly.containsLocation()
https://github.com/ionic-team/ionic-native-google-maps/tree/master/documents/poly#containslocationlocation-path
marker.on("position_changed").subscribe((params: any[]) => {
let latLng: ILatLng = params[0]; // or marker.getPosition();
let contain: boolean = Poly.containsLocation(position, POLYGON_POINTS);
marker.setIcon(contain ? "blue" : "red");
});
https://github.com/mapsplugin/cordova-plugin-googlemaps-doc/blob/master/v2.3.0/class/utilities/geometry/poly/containsLocation/README.md

How to add drop pin in Google Map iOS

How can I add "drop pin" and the dropping on above animation in Google Map iOS?
I can only find marker in SDK, and the appearAnimation property only provides pop up animation.
Use MKAnnotationView from MapKit. From the documentation on Using the Standard Annotation Views:
MKAnnotationView* aView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:#"MyCustomAnnotation"] autorelease];
In the GMSMarker Class reference it says for the appearAnimation property:
Controls the animation used when this marker is placed on a GMSMapView (default kGMSMarkerAnimationNone, no animation).
Using the Google Maps SDK for iOS a marker can be made like this:
GMSMarker *startMarker = [GMSMarker markerWithPosition:#"NYC"];
startMarker.appearAnimation = kGMSMarkerAnimationPop;
startMarker.title = #"Start";
startMarker.snippet = #"My address";
startMarker.map = mapView;
If you set the title or snippet property of a Marker then the tooltip will show when tapped.
marker.title = #"Hello World";
and/or
marker.snippet = #"Hello Snippet";
For swift:
locationMarker.appearAnimation = kGMSMarkerAnimationPop
locationMarker.icon = UIImage(named: "testImage") as UIImage?

about positioning myself,some problems

I am a new to google map sdk for ios.I have added a map on a view.When I enter this mapView,I want to positioning myself.So I wrote :
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
longitude:151.2086
zoom:6];
_iMapView = [GMSMapView mapWithFrame:CGRectMake(0, 0, 320, 480) camera:camera];
self.iMapView.myLocationEnabled = YES;
self.iMapView.delegate=self;
GMSMarkerOptions *annotation = [[GMSMarkerOptions alloc] init];
annotation.position = CLLocationCoordinate2DMake(-33.8683, 151.2086);
annotation.title = #"Sydney";
annotation.snippet = #"Australia";
//annotation.infoWindowAnchor=CGPointMake(0.5, 0.5);
[self.iMapView addMarkerWithOptions:annotation];
//[self.view addSubview:self.iMapView];
self.view=self.iMapView;
but I find the mapView view in the coordinate(33.8683,151.2086),I just want to move the mapView to the wyposition. I also find google have no callback function reference to
self.iMapView.myLocationEnabled = YES;
thank you for you reply.
To animate/set the camera to your current position you first have to:
self.googleMapsView.myLocationEnabled = YES;
Then in the documentation of the GMSMapView header file you will find the following comment:
/**
* If My Location is enabled, reveals where the user location dot is being
* drawn. If it is disabled, or it is enabled but no location data is available,
* this will be nil. This property is observable using KVO.
*/
#property (nonatomic, strong, readonly) CLLocation *myLocation;
So you can setup a key value observer in your viewWillAppear Method and then you get your location update with the Location Manager of the GoogleMaps SDK.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Implement here to check if already KVO is implemented.
...
[self.googleMapsView addObserver:self forKeyPath:#"myLocation" options:NSKeyValueObservingNew context: nil]
}
And then observe the property.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:#"myLocation"] && [object isKindOfClass:[GMSMapView class]])
{
[self.googleMapsView animateToCameraPosition:[GMSCameraPosition cameraWithLatitude:self.googleMapsView.myLocation.coordinate.latitude
longitude:self.googleMapsView.myLocation.coordinate.longitude
zoom:self.googleMapsView.projection.zoom]];
}
}
Do not forget to deregister your observer in the viewWillDisappear.
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Implement here if the view has registered KVO
...
[self.googleMapsView removeObserver:self forKeyPath:#"myLocation"];
}
Best regards
NOTE: This answer is wrong, see Robert's answer.
The Google Maps SDK for iOS doesn't have any delegate to let you know when the device's position has changed.
You would need to use the CLLocationManager class yourself, to get the device's current position, and then update the map view.
UPDATE:
To update the map view from the new CLLocation provided by the location manager, you do something like this:
GMSMapView* mapView = ...;
CLLocation* location = ...;
GMSCameraPosition* camera = [GMSCameraPosition
cameraWithLatitude: location.coordinate.latitude
longitude: location.coordinate.longitude
zoom: 6];
mapView.camera = camera;
Or if you want the map view to animate to the new location, then use this:
[mapView animationToCameraPosition: camera];

disable all other views not including map view Gmap2

I am using Google Map v2 and I only want map view and nothing more. I also would like to get rid of the zoom in and out buttons.
If anyone knows what I need to add to the following that would be great.
function stores()
{
$('#storelist ul#stores').html("");
fetch(203,"task=location&location=vic");
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(-37.810013, 144.962683), 8);
map.setUIToDefault();
yellowIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/orange/blank.png";
markerOptions = { icon:yellowIcon };
}
Remove map.setUIToDefault(). It adds the default look and feel to the Map.
(Reference docs: http://code.google.com/apis/maps/documentation/javascript/v2/reference.html#GMap2.setUIToDefault)
If you want, you can also customise how the map can be interacted with. For example map.disableDoubleClickZoom() or map.disableDragging(). See the reference above for details.

Simplest way to make a Google Map mashup?

Given a list of locations such as
<td>El Cerrito, CA</td>
<td>Corvallis, OR</td>
<td>Morganton, NC</td>
<td>New York, NY</td>
<td>San Diego, CA</td>
What's the easiest way to generate a Google Map with pushpins for each location?
I'm assuming you have the basics for Maps in your code already with your API Key.
<head>
<script
type="text/javascript"
href="http://maps.google.com/maps?
file=api&v=2&key=xxxxx">
function createMap() {
var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(37.44, -122.14), 14);
}
</script>
</head>
<body onload="createMap()" onunload="GUnload()">
Everything in Google Maps is based off of latitude (lat) and longitude (lng).
So to create a simple marker you will just create a GMarker with the lat and lng.
var where = new GLatLng(37.925243,-122.307358); //Lat and Lng for El Cerrito, CA
var marker = new GMarker(where); // Create marker (Pinhead thingy)
map.setCenter(where); // Center map on marker
map.addOverlay(marker); // Add marker to map
However if you don't want to look up the Lat and Lng for each city you can use Google's Geo Coder. Heres an example:
var address = "El Cerrito, CA";
var geocoder = new GClientGeocoder;
geocoder.getLatLng(address, function(point) {
if (point) {
map.clearOverlays(); // Clear all markers
map.addOverlay(new GMarker(point)); // Add marker to map
map.setCenter(point, 10); // Center and zoom map on marker
}
});
So I would just create an array of GLatLng's of every city from the GeoCoder and then draw them on the map.
Check out the Google Maps API Examples
They make it pretty simple and their API documentation is great.
Most of the examples are for doing all the code in JavaScript on the client side, but there are APIs for other languages available as well.
I guess more information would be needed to really give you an answer, but over at Django Pluggables there is a django-googlemap plugin that might be of help.
Edit: Adam has a much better answer. When it doubt look at the API examples.
Try this: http://www.google.com/uds/solutions/wizards/mapsearch.html
It's a google maps wizard which will generate the code for you. Not the best for your application; but a good place to "get your feet wet" ;)
Edit: (found the link), here's a good Google Maps API stepwise tutorial.
Good luck!
/mp
Here are some links but as with most things i have not got round to trying them yet.
http://gathadams.com/2007/08/21/add-google-maps-to-your-net-site-in-10-minutes/
http://www.mapbuilder.net/
Cheers
John