I need to simulate the position tracking for my 'IoT thing' in my IoT platform Thingworx. For eg: If I have a car as a Thing, and it has two modes moving and stopped. I need to simulate the changing latitude and longitude for the car when it is in 'moving' state and store the generated coordinates so that they can be later displayed on the Google map widget in the mashup. Similarly, if the car is stopped then it should display the location where it was last stopped. I have the following questions:
How do I achieve this? As in, should I create and use a service or a subscription (with speed values as the trigger)?
What sort of code snippet will be required for such a functionality?
Should I employ the Google api?
Also how do you store the changing values of the lat/long in the variable which has a datatype 'Location'?
Thanks!
A part of the logic to simulate movement, which can be as complex as you want or a simple as adding Randomly movements to the Latitude / Longitude.
You should use a Timer Thing which for instance triggers every second ( or any other frequency ) and you subscribe to "Timer" event and there you write the code to move the car.
You need to have a property on the Car Thing that we can call it "location" which of baseType LOCATION, and this property should be set as "logged" then you just need to update location property with the new location ( if it moved since last location - on your simulator code - ). Of course you also must set car Thing's ValueStream property in order to get storage for location changes.
You can use TW Marketplace Google Maps extension in order to show Location and Paths on Google Maps from the recorded simulation.
Setting a location property in TW it's something like:
me.location = {
latittude: latitude_value,
longitude: longitude_value,
elevation: elevation_value,
units: "WGS84"
}
Hope it helps.
What you ask for is not easy to explain in a few words! The best way is to show you an example that uses the Google Map API and may interest you also as it uses CSS transitions to smooth the trip of you car (or Hansel and Gretel in my case).
You can see the fully documented code for the Brotkrumen Web App here. It includes a aaa_readme.txt
Note that knowing when someone has stopped is not straight forward and has to be obtained heuristically. I provide a number of secs parameter that must elapse sans interesting movement-change to work out you've stop and for how long. Try running Brotkrumen in your car and it should work out when you were at the lights (At least in Perth they take forever) and will show you when you press the Map Trip or Replay button.
Related
I'm using Leaflet library in my ReactJS app and I wonder if there is a simple way to recognize if object clicked by user is a building.
Idea that came up to my mind is to check map colour under clicked position.
Does it make sense?
I appreciate your help.
Colleagues in comments advised to give some use-case:
App I'm working on is meant to mark antique buildings with elevation in bad shape so city architecture management had simpler job of searching for them.
Every user of this App can mark such building. To prevent hooligans from corrupting data with senseless points on map I wanted to validate as a first step if clicked point is a building.
I hope it will clarify problem a little bit.
I wonder if there is a simple way to recognize if object clicked by user is a building.
No.
You basically want to run arbitrary point-in-polygon queries against OSM's building dataset, and I will presume that you don't want to host that dataset yourself.
The simplest way to do this is to perform queries to an Overpass API server, passing a is_in query and filtering by the building tag key. The OSM website's query feature functionality uses such a technique.
With this technique you won't have to worry about hosting the data, just about creating the right Overpass API query. Please bear in mind that the Overpass API servers are run by volunteers and their resources are limited.
The second simplest way would be to download a OSM extract of you area of interest, and run the point-in-polygon queries yourself, by whatever means you like (PostGIS' ST_Intersect, turf.js, etc etc).
If you will be using Leaflet, another approach would be to use vector tiles, and set it up in such a way that the buildings thematic layer is interactive. This will require you to be aware of the limitations of the vector tile servers.
Idea that came up to my mind is to check map colour under clicked position.
That is unreliable. Think about labels on top of buildings, or the colour of the edge of the building area, or buildings that don't render with the standard colour (e.g. places of worship, monuments).
Thanks for reading this. I've just implemented the Google Maps API in my Android APP and it works perfectly. I've added markers of specific points of interest in the onCreate which also works fine. The next thing I need to do is give the user of the app the ability to add a marker or overlay if they see something on the route an this should be visible to all the other users using the app. The App is used to help people spot animals in a nature reserve by checking the app to see where animals was spotted by other users of the app. If possible i need these markers to disappear after let's say 2 hours as the animal will probably not be there anymore. My "setMyLocation is set to true and the app shows my current possition. Please help point me in the right direction as I'm not sure where to start or what to use for this. Your help is appreciated.
Thanks
Janno
What I would do:
Save the positions in a database. Add a colum timestamp to the table. In this timestamp you save the current time + 2 hours.
To get the right positions, Create a query which selects the positions where the timestamp is bigger than the current time.
It might be a nice idea to save what animal it is.
Good luck
I'm working on a Google Maps project where we want the customer to type in an Autocomplete address and then be taken to that place on the map.
My manager wants a consistent "viewport" result when compared to the viewport achieved when searching for the same place on maps.google.com.
I found an inconsistent result for a certain location, in particular for the Australian city of Sydney.
The result I get back from Places Autocomplete for Sydney looks quite zoomed in (I used the response's viewport data and call map.fitBounds(...). I'm a first time Stackoverflow user, so I can't post images yet - instead, the effect can be seen by entering 'Sydney, New South Wales, Australia' at the following page: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete
Alternatively, if I use the Geocoding API instead and take the viewport data response and use that to call map.fitBounds(...), I get a zoomed out view, similar to what I would see when I look for Sydney on maps.googles.com.
I would be very grateful for any suggestions that would provide a solution that gives my customers a viewing result that is 100% consistent with maps.google.com. For example, should I be using a different version of the Autocomplete API, or pass a special parameter? Ideally I would prefer to not be calling the Geocoding to retrieve and override the Autocomplete viewport response, since that would double up my API traffic and double the hit on Google's servers.
I look forward to any advice anybody can offer, thank you!!!
Are you wanting the map zoom level to be consistent after the search ?
I do android with google map. And just by looking at the code from the provided link above.
change that map.setZoom() method to the level you want might help.
I haven't done javascript google map api yet. Just taking a wide guess.
If I am answering the wrong question, please correct me.
I am using JavaScript code to show the multiple pin points on Google Maps using Brownfield. I set the first clicked point as the center point, and when the user clicks on any other point (which is not the center point) I want to display a route mapped from the center point to that clicked point.
I would appreciate help of any sort, including ideas besides using JavaScript.
Google has a number of services available to people who program using their Maps. Go to https://developers.google.com/maps/documentation/javascript/reference and check out the directions services. I'm not very familiar with them, but I'm guessing it's similar to their other services. You make a directions request object supplied with point a and point b, and it will send a message to Google asking for the appropriate directions, which will be returned in some sort of result object that you can use to show the way. Update point b each time the user clicks and resend the google request, and it should update the path. Check out the API and it shouldn't be too hard to get it working. As for alternatives to javascript, Google Maps is written all in javascript, so there really is no other way. But I know from experience, most of their supplied code works really well, so I bet you can get it working!
Following link will show the route between two points:
J2ME/Android/BlackBerry - driving directions, route between two locations
I'm developing a store locator web-application for mobile (iPhone). The idea is standard, the device uses its geolocation feature to determince the location and should then display the 5-10 closest stores on a map. My problem is essentially that I have too many stores (10.000+) and haven't yet been able to create a nice user eperience.
I realize this may be a bit much to ask but I'd love to hear your thoughts on this..
PROBLEM SCOPE:
i) I figure I can only load some of the markers at once rather than all of them which I found that a lot of the <100 stores-finder apps do.
ii) It should be bad connection friendly: i.e. google maps should for example not be made to zoom in and out unecessearily causing new tiles to load (and a empty screen to show if the connection is bad)
iii) it has to somehow cope with the fact that the device gps/tower triangulation can be slow to get an sufficiently accurate fix on your position (often a minute or two if indoors or in an rural area)
SOLUTIONS I've tried:
I) Use getCurrentPositition and from that lat/lng load 5 closest markers. Issue: the snapshot positioning is inaccurate and often the 'wrong' 5 stores are loaded.
II) Use watchPosition until accuracy reaches <100m and then load closest 5 markers. Issue: often takes way to long to get the position (often >1 minute), not a good UX.
III) Same as II only markers are loaded contionously while it finding exact position. Issues: It gets real 'jumpy' using fitbounds() or panTo() every time there is an update. I.e. if the user tries to say click a marker while the map is still in 'find position'-mode they will be intrerupted by the map changing bounds and loading more markers.
IV) Fit bounds to an approximate position using watchPosition and a fixed zoom and use center of the map to get the closest stores. Continously load markers as watchPosition finds a better approximate center or the user himself pans the map. Issues: Again, a bit jumpy since watchPosition updates alot. No 'your position' marker.
BACKGROUND INFO:
I'm using much of the standard google method of creating a store finder, shown here: http://code.google.com/intl/sv-SE/apis/maps/articles/phpsqlsearch.html
Google Maps v3 API, MySQL database, Haversine formula, JSONP asychronous loading of marker-data, no libraries (jQuery or such). Loading 5-10 new markers with this method usually takes less than 1s.
I don't know what watchCurrentPosition is, I'm going to assume you meant to say watchPosition; which is defined in the HTML5 Geolocation spec.
I wouldn't use watchPosition for this. It's technically defined to track changes in position, which don't necessarily translate to increases in accuracy. While I'm not an expert on mobile device power consumption, I'd bet that it uses a lot more power to fire up the GPS and keep it on than to get 1 really good position fix, which is what getCurrentPosition is designed for.
I'd be surprised if this was actually a GPS / sensor fix-time problem. Lots of websites used getCurrentPosition to find the user's current position and most mobile devices can do it within a few seconds, not much different from an AJAX call. I'd play with the PositionOptions settings to set a reasonable timeout, maxAge, and play with the enableHighAccuracy value.
If your 10K stores are within a 2-5 mile radius then you definitely have a hard problem on your hands :-D, but otherwise the data you're getting back from the Geolocation API should be able to narrow it down a handful of stores within a reasonable accuracy. The Geolocation API might not return a lat/lng representing your exact street address, but it should get you a location within the correct city, if not cluster of streets.
I'd decompose this into two problems: 1 - Getting an acceptably accurate position (lat, lng) via the Geolocation API. 2 - Finding stores near position (lat, lng). It'll be much easier to ask focused questions if you're having troubles with part 1 or part 2