How to check if a coordinate exists in GMSMutablePath before adding a coordinate - ios

I'm trying to draw a polyline on the Google map as a object travels, sometimes the coordinates sent can get duplicated. I want to prevent duplicate coordinate from being added to the GMSMutablePath. Anyway this be achieved?
Currently I use the following method to add the coordinate to the GMSMutablePath. It adds duplicate values as well!
self.path.addLatitude(coordinate.latitude, longitude: coordinate.longitude)

After doing some digging in GoogleMaps SDK, I arrived at this solution. It may not be the perfect one but you can give it a try.
You can iterate through all the coordinates of the path by using coordinate(at:index) method of GMSMutablePath
Iterating the GMSMutablePath coordinates.
//Here path is your GMSMutablePath
for i in 0..<path.count() {
let coordinate = path.coordinate(at: i)
//The coordinate received is a CLLocationCoordinate2D type from which you can get the latitude and longitude.
//Here check the coordinate latitude and longitude is same as your received coordinate, make a return else add to your path.
//You can also keep a flag variable and at the end of all iterations, you can check whether the coordinate is present or not.
print(coordinate)
}

Related

Find coordinates in MKCoordinateRegion

I have a list of places with their coordinates (latitude + longitude).
I want to get only the places that are in the region displayed on the screen.
I can get the current region displayed on my screen with:
MKCoordinateRegion(center: CLLocationCoordinate2D, span: MKCoordinateSpan)
However I don't know under which parameters I should filter the coordinates of my list to only get the places in this region.
MKCoordinateSpan isn't convertible into a distance mesure from the center.
Thanks for helping
I think the simplest way is to get the displayed region as an MKMapRect with visibleMapRect and convert your coordinates to MKMapPoint using the MKMapPoint(_:) initializer. That way you can just call MKMapRect contains(_:).

Current location issue in ArcGIS Maps in iOS

I'm developing an application in which I have a map. I'm using ArcGIS SDK for showing map. Here I'm trying to show my current location when the map loads. I have passed my latitude and longitude to the x and y parameters of the ArcGIS function. This is the code for that function,
let lat = gpsLocation.location?.coordinate.latitude
let lng = gpsLocation.location?.coordinate.longitude
print(lat)
print(lng)
//zoom to custom view point
self.mapView.setViewpointCenter(AGSPoint(x: lat!, y: lng!, spatialReference: AGSSpatialReference.webMercator()), scale: 4e7, completion: nil)
self.mapView.interactionOptions.isMagnifierEnabled = true
But when I run the app it shows the wrong location in the map. It points to the wrong location. I have printed the lat and lng also. They are correct but the display of location is wrong in the map. How can I get the map to load my current location?
This is what it shows location when loads,
You're using latitude and longitude, i.e. a number of degrees (probably in WGS 1984), but then you're telling ArcGIS that they are in Web Mercator, i.e. a number of meters. That will definitely cause the behavior you see.
To fix it, simply replace webMercator() with WGS84().
Also, you are mixing up latitude and longitude. Latitude is y and longitude is x, and you have it the other way around.
In summary, replace your setViewpointCenter call with this:
self.mapView.setViewpointCenter(
AGSPoint(x: lon!, y: lat!, spatialReference: AGSSpatialReference.WGS84()),
scale: 4e7,
completion: nil
)

Get all GMSMarker from mapview and remove all marker without using mapview.clear()

I have drawn path with marker in google map. So the path is static but marker needs to change their positions. How can I remove all markers without using mapview.clear(), because it will clear my path also from the map.
Any solution?
I guess you will have to keep all markers in an array(eg. allMarkers). Then,
for marker in allMarkers {
marker.map = nil
}

Assign a latitude/longitude point to a region

I have a map from jvectormap http://jvectormap.com/maps/countries/united-kingdom/ which displays country regions in SVG 'paths'.
I also have a set of objects with latitude and longitude co-ordinates.
Is it possible to assign each object to a particular region, given the co-ordinates and the SVG paths?
I found a way of doing it, but it's really hackish, and I'm sure there are better ways of doing it.
Anyhow, here's my solution to the problem:
jvm.Map.prototype.latLngToRegion = function (lat, lng) {
var pointOnMap = this.latLngToPoint(lat, lng),
pointOnPage = {
x: Math.round((pointOnMap.x + this.container.offset().left) - $(window).scrollLeft()),
y: Math.round((pointOnMap.y + this.container.offset().top) - $(window).scrollTop())
},
element = document.elementFromPoint(pointOnPage.x, pointOnPage.y),
region = $(element).attr('data-code');
return region; // note, this will only give you the region code, but from that you can read out most of what you need from jvectormaps itself.
}
Caveats are that the latitude and longitude you are looking for must be in view, for document.elementFromPoint to work. But you could maybe update the list on scroll and map pan etc.
Well, maybe you can take a look at how jvectormap matches your current mouse position to the region - you should be able to match your lat/lng coordinates in a similar way

Best practice for using lat/long within a UIView (not MKMapView)

Basically i have a list of POI's (name,lat,long) and i want to draw them on the UIView, relative to my current lat/long. I'm looking for some best practice for mapping these POI (lat/long) to a UIView.
I don't want to use MKMapView (no need for displaying map-data).
I was reading:
http://developer.apple.com/library/ios/#documentation/general/conceptual/Devpedia-CocoaApp/CoordinateSystem.html
But I'm clueless how i get from a CLLocation to a (x,y) on my UIView. I only want to draw those POI's around my current location. So, for example if my screen would represent a 20 by 30 KM region, how do i map my POI's to their corresponding (x,y) coordinates?
Thanks.
What you're doing is a little strange, but you can convert latitude/longitude to a CGPoint-like struct called an MKMapPoint. An MKMapPoint has an x and y value which correspond to points on a map. Imagine if you laid out a flat map of the world, and 0,0 was the top left. MKMapPoint is a point on that map using that coordinate system.
Use the function MKMapPointForCoordinate() to convert a CLLocationCoordinate2D to an MKMapPoint
MKMapPoint myMapPoint = MKMapPointForCoordinate(myLocationCoordinate);
When you get the list of points, you'll have to do something like finding the max and min x and y values, then fitting all the points into your view using those values, otherwise you'll end up with a load of very close points in one place in your view.
My guess is that, for a 20KM by 30KM region, you can consider the earth to be flat and there fore linearly extrapolate the coordinates. I am sure you can google and find out as to how much distance is a difference in 0.00001 in latitude and longitude.
So if you have 20Km to be represented on X axis, and your current location is 30.1234567 in latitude, and 0.0000001 is 1 km then you can put your coordinate in the center of the screen and 30.1234557 as the left most X coordinate and so on.
I am not trying to provide an answer here, but just trying to think out loud, because I wanted to do some thing similar as well and did it as an Internet based app (without display though), where given two coordinates, I had to find the distance between them.
There are many (many) different approaches to modelling the planet and translating 3D coordinates onto a 2D surface, and the errors introduced by the various methods vary depending on what part of the globe you are. This question seems to cover most of what you are after though:
Converting Longitude & Latitude to X Y on a map with Calibration points
I think its best way (correctly work for Mercator projection map):
extension UIView
{
func addLocation(coordinate: CLLocationCoordinate2D)
{
// max MKMapPoint values
let maxY = Double(267995781)
let maxX = Double(268435456)
let mapPoint = MKMapPointForCoordinate(coordinate)
let normalizatePointX = CGFloat(mapPoint.x / maxX)
let normalizatePointY = CGFloat(mapPoint.y / maxY)
let pointView = UIView(frame: CGRectMake(0, 0, 5, 5))
pointView.center = CGPointMake(normalizatePointX * frame.width, normalizatePointY * frame.height)
pointView.backgroundColor = UIColor.blueColor()
addSubview(pointView)
}
}
My simple project for adding coordinate on UIView: https://github.com/Glechik/MapCoordinateDrawer

Resources