google map sdk how to get current location in xamarin ios - ios

I have a code like below but "CLLocation loc = mapView.MyLocation;" assigne null to loc object, so i cannot take cuurent location.
How can get my current location in xamarin for ios with google map sdk for ios.
//and many other using
using Google.Maps;
public partial class MapNavigationController : UIViewController
{
MapView mapView;
public override void LoadView ()
{
base.LoadView ();
var camera = CameraPosition.FromCamera (latitude: 7.2935273,
longitude: 80.6387523,
zoom: 13);
mapView = MapView.FromCamera (RectangleF.Empty, camera);
mapView.MyLocationEnabled = true;
mapView.MapType = MapViewType.Hybrid;
CLLocation loc = mapView.MyLocation;
Console.WriteLine("tapped at " + loc.Coordinate.Latitude + "," +loc.Coordinate.Longitude);
}
}

I am not very familiar with Google Maps in iOS.
But I don't think its possible to get you current location from the MapView.
You can maybe show it (by enabling an option (showOwnLocation or something similar)).
If you want to do it the right way you have to do it with the CLLocationManager from the iOS SDK. I don't have a sample available right now but if you google it you will find some.
Hopefully this helps you.

You need to get that info from CCLocationManager, like so:
CameraPosition camera = CameraPosition.FromCamera(latitude: App.MyLatitude,
longitude: App.MyLongitude,
zoom:50);
mapView = MapView.FromCamera(CGRect.Empty, camera);
mapView.Settings.ScrollGestures = true;
mapView.Settings.ZoomGestures = true;
mapView.MyLocationEnabled = true;
mapView.MapType = MapViewType.Satellite;
CLLocation loc = iPhoneLocationManager.Location;
// My position
var Marker = new Marker()
{
Title = App.PinTitle,
Snippet = App.PinLabel,
Position = new CLLocationCoordinate2D(latitude: loc.Coordinate.Latitude, longitude: loc.Coordinate.Longitude),
Map = mapView
};

Related

Flutter PositionStream isn't updating map location

I have a Flutter app with a map that shows user location
On the simulator, when I change the coords the marker moves.
However, on a physical device it doesn't move at all.
I have a Geolocator Stream to update the marker position
Here is my code
Position? currentPosition;
StreamSubscription<Position>? positionStream;
void listenToLocationChanges() {
LocationIndicator locationIndicator = LocationIndicator();
locationIndicator.locationIndicatorStyle = LocationIndicatorIndicatorStyle.pedestrian;
final LocationSettings locationSettings = LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 100,
);
positionStream = Geolocator.getPositionStream(locationSettings: locationSettings).listen(
(Position? position) {
print(position==null? 'Unknown' : '$position');
currentPosition = position;
GeoCoordinates geoCoordinates = GeoCoordinates (position!.latitude, position.longitude);
bearing = position.heading;
currentlat = position.latitude;
currentlong = position.longitude;
Location location = Location.withCoordinates(geoCoordinates);
location.time = DateTime.now();
location.bearingInDegrees = bearing;
locationIndicator.updateLocation(location);
_hereMapController.addLifecycleListener(locationIndicator);
_locationIndicatorList.add(locationIndicator);
},
);
}
Is there any reason why it would work in my simulator when I change the custom coords, it updates fine, but on a device it stays still at the location when opened?
Thanks in advance
I changed distance params and is now accurate

(iOS Swift)Google Maps animateWithCameraUpdate not working

I am working with Google maps in iOS. I wanted to animate my Camera such that it shows all the required Lat, Lngs. I am storing those locations in the array(as CLLocationCoordinate2D instances) and using GMSCoordinateBounds class to add these locations. This is my code:
let bounds = GMSCoordinateBounds()
bounds.includingCoordinate(firstLocation)
bounds.includingCoordinate(lastLocation)
let camUpdate = GMSCameraUpdate.fit(bounds, withPadding: 60)
mMapView.camera = GMSCameraPosition.camera(withLatitude: firstLocation.latitude, longitude: firstLocation.longitude, zoom: 18)
mMapView.animate(with: camUpdate)
firstLocation and lastLocation have correct values but the camera is not animating. Am I missing something?
Very silly mistake. includingCoordinates function returns GMSCoordinateBounds object. I had to set to itself again for it to work like so:
bounds = bounds.includingCoordinate(firstLocation)
bounds = bounds.includingCoordinate(lastLocation)
If i set like above, it is working fine.

How can I show & scroll particular bound of view for google map? other part of map we can't see and scroll

I found a method for android called setLatLngBoundsForCameraTarget. But could not find for ios. Has any one idea about it? I want to scroll into particular bound. User can't scroll out of the bound which is already defined
The iOS equivalent method is setCameraTargetBounds. You can use it as follows:
GMSCoordinateBounds *gmsBounds = [[GMSCoordinateBounds alloc]
initWithCoordinate:bounds.northWest
coordinate:bounds.southEast];
[self.mapView setCameraTargetBounds:bounds];
Additionally, you may want to set a minimum zoom level, by doing this:
[self.mapView setMinZoom:15 maxZoom:23];
Try this :
let camera = GMSCameraPosition.camera(withLatitude: -33.8683,
longitude: 151.2086,
zoom: 16)
let mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
More
I used cameraTargetBounds but not useful for me. Then I used
func mapView(mapView: GMSMapView, didChangeCameraPosition position: GMSCameraPosition) {
var latitude = position.target.latitude;
var longitude = position.target.longitude;
if (position.target.latitude > bounds.northEast.latitude) {
latitude = bounds.northEast.latitude;
}
if (position.target.latitude < bounds.southWest.latitude) {
latitude = bounds.southWest.latitude;
}
if (position.target.longitude > bounds.northEast.longitude) {
longitude = bounds.northEast.longitude;
}
if (position.target.longitude < bounds.southWest.longitude) {
longitude = bounds.southWest.longitude;
}
if (latitude != position.target.latitude || longitude != position.target.longitude) {
var l = CLLocationCoordinate2D();
l.latitude = latitude;
l.longitude = longitude;
mapView.animateToLocation(l);
}
}
And it works.

Marker not showing up on GMSPanoramaView

I have a drop dead simple test app in which i execute the following code
let marker = GMSMarker(position:CLLocationCoordinate2DMake(33.987884, -118.474434))
marker.icon = GMSMarker.markerImageWithColor(UIColor.redColor())
marker.panoramaView = panoramaView;
marker.snippet = "I am Here!"
panoramaView.moveNearCoordinate(marker.position)
If i move the coordinate to some other street off the beach i can see the marker just fine. But from the code above I don't see anything. Does anybody know why I can get an area to show up fine in a GMSPanoramaView however placing a marker there does not work?
I think some implementation was not done. Not sure what are those implementation but I found some tutorials regarding your issue. Or according to the document "Note that if the marker's position is too far away from the panoramaView's current panorama location, it will not be displayed as it will be too small".
Checking the Street View locations and point-of-view (POV):
GMSPanoramaView *panoView_;
panoView_.camera = [GMSPanoramaCamera cameraWithHeading:180
pitch:-10
zoom:1];
Markers within Street View
// Create a marker at the Eiffel Tower
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(48.858,2.294);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
// Add the marker to a GMSPanoramaView object named panoView_
marker.panoramaView = panoView_;
// Add the marker to a GMSMapView object named mapView_
marker.map = mapView_;
That's the first option, another option is Google Maps JavaScript API inside a WebView. https://developers.google.com/maps/documentation/javascript/examples/streetview-overlays
var panorama;
function initMap() {
var astorPlace = {lat: 40.729884, lng: -73.990988};
// Set up the map
var map = new google.maps.Map(document.getElementById('map'), {
center: astorPlace,
zoom: 18,
streetViewControl: false
});
// Set up the markers on the map
var cafeMarker = new google.maps.Marker({
position: {lat: 40.730031, lng: -73.991428},
map: map,
icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
title: 'Cafe'
});
var bankMarker = new google.maps.Marker({
position: {lat: 40.729681, lng: -73.991138},
map: map,
icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=dollar|FFFF00',
title: 'Bank'
});
var busMarker = new google.maps.Marker({
position: {lat: 40.729559, lng: -73.990741},
map: map,
icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=bus|FFFF00',
title: 'Bus Stop'
});
// We get the map's default panorama and set up some defaults.
// Note that we don't yet set it visible.
panorama = map.getStreetView();
panorama.setPosition(astorPlace);
panorama.setPov(/** #type {google.maps.StreetViewPov} */({
heading: 265,
pitch: 0
}));
}
function toggleStreetView() {
var toggle = panorama.getVisible();
if (toggle == false) {
panorama.setVisible(true);
} else {
panorama.setVisible(false);
}
}
Here are links for tutorials and repo of the codes
http://googlegeodevelopers.blogspot.com/2013/09/sun-surveyor-shines-with-street-view.html
https://github.com/ratana/streetview-panorama-demo
I hope this helps.

How to set Monotouch map between two points

How can I center a map between two points? Sort of like when the native map application generates directions between location A and location B. I'm got a start coordinate and an end coordinate and I'll like to show two pins. I can place the pins in place, but I'm not sure how to set the center of the map.
Do I need to find the math to work out the exact distance from the points and set the map to that location? Is there a built in function for this?
this.currentMapView.SetCenterCoordinate (annotation.Coordinate, true);
Calculating the midpoint between two coordinates needs a simple formula. For example, let's say you have two coordinates: (x1,y1) and (x2,y2).
Their midpoint coordinate is: ( (x1+x2)/2, (y1+y2)/2 ).
So for example, in map coordinates, let's say you have the following start/end points:
a. long: 40, lat: 39
b. long: 41, lat: 38
Their midpoint coordinate is: ( (40+41)/2, (39+38)/2 ) = (40.5, 38.5)
So you set the map view's center coordinate to the outcome of this formula.
I am not aware of a built-in function for calculating this.
Taken from: http://codisllc.com/blog/zoom-mkmapview-to-fit-annotations/
BasicMapAnnotation is inherit class from MKAnnotation
private void GetRegion(MKMapView mapView)
{
var userWasVisible = mapView.ShowsUserLocation;
mapView.ShowsUserLocation = false; // ignoring the blue blip
// start with the widest possible viewport
var tl = new CLLocationCoordinate2D(-90, 180); // top left
var br = new CLLocationCoordinate2D(90, -180); // bottom right
foreach (var an in mapView.Annotations)
{
// narrow the viewport bit-by-bit
CLLocationCoordinate2D coordinate = ((BasicMapAnnotation) an).Coordinate;
tl.Longitude = Math.Min(tl.Longitude, coordinate.Longitude);
tl.Latitude = Math.Max(tl.Latitude, coordinate.Latitude);
br.Longitude = Math.Max(br.Longitude, coordinate.Longitude);
br.Latitude = Math.Min(br.Latitude, coordinate.Latitude);
}
var center = new CLLocationCoordinate2D
{
// divide the range by two to get the center
Latitude = tl.Latitude - (tl.Latitude - br.Latitude)*0.5
,
Longitude = tl.Longitude + (br.Longitude - tl.Longitude)*0.5
};
var span = new MKCoordinateSpan
{
// calculate the span, with 20% margin so pins aren’t on the edge
LatitudeDelta = Math.Abs(tl.Latitude - br.Latitude)*1.2
,
LongitudeDelta = Math.Abs(br.Longitude - tl.Longitude)*1.2
};
var region = new MKCoordinateRegion {Center = center, Span = span};
region = mapView.RegionThatFits(region); // adjusts zoom level too
mapView.SetRegion(region, true); // animated transition
mapView.ShowsUserLocation =
userWasVisible;
}
} ``

Resources