Search Placemarks Google Earth API - google-earth

I know in the Google Earth client you can search for a placemark is there a way to implement this type of feature for the API. We have different placemarks that users would like to search then fly to.
Thanks

Yes, there are lots of ways to search for placemarks, really it would depend on how you have defined or created them.
For example, are they defined in a KML file, or created via the API? Also, do they have any abstract view defined, or are they simply based on point geometry?
One of the easiest ways would be to use the various accessors to find the placemarks. For example if the placemark has an ID you can use getElementById() and presuming the placemarks have an abstract view you can use that to look at, or fly to, it.
var placemark = ge.getElementById('MyPlacemark');
if (placemark.getAbstractView()) {
ge.getView().setAbstractView(placemark.getAbstractView());
}
Or else if you are loading KML by its URL, e.g.
// loaded via KML
var placemark = ge.getElementByUrl('http://yoursite.com/foo.kml#MyPlacemark');
if (placemark.getAbstractView()) {
ge.getView().setAbstractView(placemark.getAbstractView());
}
If the placemark doesn't have an abstract view you can still use the accessors to find the correct placemark and then extract the geometry from it to create the look at.
var placemark = ge.getElementByUrl('http://yoursite.com/foo.kml#MyPlacemark');
var point = placemark.getGeometry();
var lat = point.getLatitude();
var lng = point.getLongitude();
Without more information on how you have defined your placemarks it is pretty hard to be more specific.

what about KmlObjectList GEPlugin.getElementsByType?

Related

Is there any way to retrieve coordinates from searched location in mapbox?

I am using mapbox geocoder to search for a location in one of my mapbox project. I am able to implement the search functionality successfully but I am having hard time in extracting the selected address by the user from the list of searched/suggested addresses.
Is there any way to get the coordinates of the searched place?
I went through mapbox docs but only found an event 'moveend' which is fired when we fly to some location. So I am listening for that event and once fired, I am calling getBounds() function on the map. It somehow works but it doesn't give me the exact coordinates since getBounds() gives the South West and North East coordinates of the box. Can we get the exact coordinates of the selected place?
Below is the code I am using to search for a place.
initMapSearch() {
var _this = this;
var geocoder = new MapboxGeocoder({ accessToken: environment.mapbox.accessToken, mapboxgl: mapboxgl });
this.locationObj = geocoder.onAdd(_this.map)
document.getElementById('geocoder').appendChild(this.locationObj);
}
And this is my moveend event.
this.map.on('moveend', function() {
_this.lat = _this.map.getBounds()["_ne"]["lng"];
_this.long = _this.map.getBounds()["_ne"]["lat"];
});
Thanks in advance to the community.
You should listen to geocoder.on('result') which is passed the feature (and its location).

Get Mapbox point for queryRenderedFeatures based on longitude and latitude

I found an address with the Mapbox Geocoding API. Then I want to highlight that building on the map.
It works in native, but does not in web. I want to have the point in the mapBox view for the renderedFeatures.
Something like this
pointInView = await map.getPointInView(foundAddress.center)
foundBuildings = await map.queryRenderedFeaturesAtPoint(pointInView)
How can I convert long,lat to the a point in mapbox-gl-js
Update
Found something helpful
How can I query the feature that's closest to the geocode result in Mapbox?
Found it, it's called project(LngLat)
pointInView = await map.project(foundAddress.center)
See: https://docs.mapbox.com/mapbox-gl-js/api/#map#project

Google Map in tableView

I am facing an issue while displaying google maps in a table view cell with swift. I want to display check In (Latitude and Longitude) based on this I want to display google map in table view. I will latitude and longitude from server if no location available means I will get null. so, In one scenario I am getting correct, but while reloading the top-level I am getting map when and where their latitude and longitude is null also. Please guide me.
A map view is an expensive view to instantiate. Even when using dequeueReusableCellWithIdentifier it will still be resource-heavy with a large number of items.
I believe using a way to generate a static map image from your longitude/latitude combination is your best bet.
You can take a look here and you can see how to easily construct an API call to many popular map providers such as Google for a static image map like for example:
http://maps.googleapis.com/maps/api/staticmap?center=40.714728,-73.998672&zoom=13&scale=false&size=600x300&maptype=roadmap&format=png&visual_refresh=true
Also worth mentioning that some of those providers might have limitations (Like Google might need an API key in case of high traffic). So choose wisely after some research.
Looks like you are facing two issues
1) Already mentioned one "Map appears even when data is nil"
2) Performance issue (It is not mentioned here though)
The first one is due to dequeueing of cell. When you reload table , the cell will be reused(Not created again).
When you return your cell for row in
tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
the cell with map view may get return, that is why you get unwanted map there. You should make it nil to prevent this.
2:
When came to performance, its a not the right approach to show mapview in every cell. An alternate approach is to make image out of your map url
use this
let mapUrl: String = NSURL(string:"YourMapURL")
let mapImage = UIImage.imageWithData(NSData.dataWithContentsOfURL(mapUrl))
let latitude:Double = 17.3850
let longitude:Double = 78.4867
let imageURL = NSURL(string: "http://maps.googleapis.com/maps/api/staticmap?center=\(latitude),\(longitude)&zoom=12&scale=false&size=600x300&maptype=roadmap&format=png&visual_refresh=true")
let imagedData = NSData(contentsOfURL: imageURL!)!
staticMapImgView.image = UIImage(data: imagedData)

Displaying google direction api response on Map

I am using google api V3 in my iOS App,
I requested to google api direction service, by http GET method, I got a very large json response, I got many alternate routes from origin to destination, now I want to show the route of each alternative on map, how should I done this ? is there any need to integrate google sdk in iOS, or can i use webView only, please help me, and suggest the simplest way.
Thanks in advance
you do not need to include any google SDK to draw the route over map. Look at the following classes.
MKPolyline
MKPolyLineview
The google map api Direction service will give you the legs(coordinates) to draw the route between two specific points.
As you already have the json response from the Direction api now you have to parse the json and get all the legs point from the json to create Coordinate array.
Jsonkit for parsing
These points may be or mostly encripted. How to decode the Google Directions API polylines field into lat long points in objective-C for iPhone?
If you have the coordinates array then first you have to create a CLLocationCoordinate2D array like following:
CLLocationCoordinate2D *pointArr = malloc(sizeof(CLLocationCoordinate2D) * [your_CoordinateArray count]);
for(int i = 0; i < [your_CoordinateArray count]; i++){
pointArr[i] = [your_CoordinateArray objectAtIndex:i];
}
Then you have to add the polyline to your map
MKPolyline *line = [MKPolyline polylineWithCoordinates:pointArr count:[your_CoordinateArray count]];
if (nil != line)
{
[your_Map addOverlay:line];
}
Then you have to implement the following map delegate:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView* overlayView = nil;
lineView = [[MKPolylineView alloc] initWithPolyline:overlay];// lineView is an ivar, object of MKPolylineView
lineView.fillColor = [UIColor greenColor];
lineView.strokeColor = [UIColor greenColor];
lineView.lineWidth = 10.0;
lineView.alpha = 0.9;
overlayView = lineView;
return overlayView;
}
Now, if you want to show two different routes between two points then you have to create two different array of coordinates and apply the above method for both Arrays
Hope this will help you.
There is no need per say to integrate google sdk and you can simply use webView since I believe the webView gives you alternative views, but the user interface is much cleaner in the SDK than online. But at this moment, I don't believe the SDK allows other apps to show the alternative routes from what I've used of the new SDK. The code in maps.google.com is now considerably different from that in
the API and has access to different services. In particular, it has a large external module, mod_suggest.js, that doesn't exist in the API. But you may be able to implement it by
1) Throw the request at maps.google.com, and let it do the suggestions,
i.e. open a new browser window and pass it something like
http://www.google.com/maps?f=d&source=s_d&saddr=Jurong+West,+Singapore&da
ddr=Ang+Mo+Kio,+Singapore&hl=en
2) Just display the normal route and the avoid highways route.
3) Try to guess sensible waypoints to add in the middle of he route that
might lead to reasonable alternatives. That's not easy unless you have a
database of highways or highway intersections, and can look for such an
intersection that's somewhere between your start and end points (for
some approximate value of "between").
There may be problems using lat/lng coordinates for the waypoints,
particularly if there's a dual carriageway involved. If the coordinates
are for a point on the wrong carriageway, GDirections will drive a
considerable distance out of your way so that you visit the wrong side
of the road. But even using street names you may well get strange kinks
near the waypoints, like this:
from: Jurong West, Singapore to: Ayer Rajah Expy singapore to: Ang Mo
Kio, Singapore
Perhaps the only way to really deal with that is to include points on
both sides of a dual carriageway, and well clear of roads that cross
underneath or overhead, and then try to filter out the ones that are
silly.
Consider these examples
from: Jurong West, Singapore to: 1.32558,103.740764 to: Ang Mo Kio,
Singapore
from: Jurong West, Singapore to: 1.32582,103.740764 to: Ang Mo Kio,
Singapore
One of those adds 7 minutes to the trip by a complicated excursion to
visit the wrong side of the road.
Writing code to filter out silly routes isn't easy. As a first
approximation, you could discard routes that have estimated times that
are more than, say, 10% longer than the initial route. And you can also
compare the durations for pairs of points on opposite sides of a dual
carriageway and always discard the slower one.
Writing code to discard duplicate routes isn't easy either. For example,
a point on Bukit Timah Expy or Kranji Expy might create a route that's a
duplicate of Google's Seletar Expy suggestion.
You might want to look at: http://www.geocodezip.com/example_geo2.asp?waypts=yes&addr1=Jurong+West,+Singapore&addr2=Ang+Mo+Kio,+Singapore&waypt=Ayer+Rajah+Expy+singapore&geocode=1&geocode=2
But that may not be a legal implementation since the API does not allow it. Hope this helps.

How to simulate a user driving a route in a MKMapView?

I need to simulate how my application will look when a user is driving around for a demo. I have a MKMapView, how can I simulate the look of a user driving around which will use the map.userLocation functionality, which obviously will not be available in the demo.
Thanks!
No way to simulate in iPhone simulator. You'll need to load it onto your device and move around.
Well I got something going, I just did essentially this
- (void)moveIcon:(MKAnnotationView*)locationView toLocation:(CLLocation*)newLoc
{
LocationAnnotation* annotation = [[[LocationAnnotation alloc] initWithCoordinate:newLoc.coordinate] autorelease];
[locationView setAnnotation:annotation];
[map setCenterCoordinate:newLoc.coordinate animated:YES];
}
Then I call this guy in a loop between all of my vertices with a slight delay. Works quite qell.
I'm not an iPhone dev expert, but how does the map view receive the coordinates? If it's through a function that calls the CoreLocation API, could you possibly just write a function that randomly generates longitude and latitude values at a certain time interval and have your map view pull the coordinates from there instead? Just a thought.
You could also check out iSimulate which claims to be able to simulate several features only available on the iPhone in the iPhone simulator include CoreLocation. I have not tried this myself so your mileage may vary.
In order to simulate driving you'll need to establish 2 basic functionalities:
Reading CLLocations from an archive (which you'd log during the drive test with a device). Ideally you'll do this based on the timestamps on the locations, i.e. reproducing the exact same location updates which were received during the drive test.
Updating your MKAnnotationView's position on the map based on the locations read from log.
For part 1, take a look at CLLocationDispatch, a handy class which provides archiving/unarchiving of CLLocations and dispatches them to one or more listeners (using CLLocationManagerDelegate protocol).
For part 2, take a look at Moving-MKAnnotationView.
I found a better way would be to subclass MKUserLocation:
class SimulatedUserLocation: MKUserLocation {
private var simulatedCoordinate = CLLocationCoordinate2D(latitude: 39, longitude: -76)
override dynamic var coordinate: CLLocationCoordinate2D {
get {
return simulatedCoordinate
}
set {
simulatedCoordinate = newValue
}
}
}
Then add it as an annotation mapView.addAnnotation(SimulatedUserLocation()). (You might also want to hide the real location first mapView.showsUserLocation = false)
iOS would render the annotation exactly like the real user location.
dynamic is used on the property so that changing coordinate triggers KVO and moves it on the map.
The answer is NO. Then, how about adding an abstraction layer between your code and MKMapKit? You can do xUnit tests for your objective.