How can I get/set the bonding box of a Google map? I know how to create map using center and zoom, but I need a way to save the view of map based on its boxing and the recreate the same view later using the map bonds (NE and SW of map)
Any idea how I can do this?
I am using MVC 3.
You want to use the map.getBounds() function which returns a LatLngBounds object. From this you can then use getNorthEast() and getSouthWest() to get the coordinates you want.
var bounds = map.getBounds();
var NE = bounds.getNorthEast();
var SW = bounds.getSouthWest();
If you need those LatLng objects to then be strings (for inserting to your DB or writing to a cookie or whatever), just use the toString() function on them.
strNE = NE.toString();
strSW = SW.toString();
So let's assume you write these to a cookie or use Ajax to write these to your DB. Once you get them out of the cookie/DB later, you can then just use those for setting the center of the map:
bounds = new google.maps.LatLngBounds(SW, NE);
map.fitBounds(bounds); // or maybe try panToBounds()
All these functions are documented here: https://developers.google.com/maps/documentation/javascript/reference
Related
I am using Azure Atlas Map in my Azure Web app. I want to move the symbols smoothly without refreshing the whole page.
I did some changes in existing code. I have set a time interval in ajax call and adding the new symbol layer in the map.
But facing issues.
I am getting error
map is undefined.
here is the code
function GetJsonMap(jsondata) {
if (typeof jsondata !== 'undefined') {
var gps_data = jsondata;
for (var i = 0; i < gps_data.length; i++) {
var point = new atlas.data.Point([gps_data[i][0], gps_data[i][1]]);
var feature = new atlas.data.Feature(point, { name: gps_data[i][2], description: '[' + gps_data[i][0] + ", " + gps_data[i][1] + ']' });
datasource.add(feature);
}
//Add a layer for rendering point data as symbols.
var symbolLayer = new atlas.layer.SymbolLayer(datasource, null, {
iconOptions: {
image: 'pin-red'
}
});
debugger;
// $("#iotmap")
map.layers.add(symbolLayer); -->getting error here
}
}
I am calling this GetJsonMap in ajax call.
If you are getting an error that map is null it means that its out of scope in your code. Is it a global or local variable?
Looking at your code I highly recommend that you create the symbol layer after the maps ready event has fired and only create it once. Majority of the code samples for Azure Maps do this. The way your code is now, it will add a new layer every the GetJsonMap function is called. As such, you will end up with several layers over time that are trying to render the same data on the map. Note that the data is managed by the data source, layers only render what's in the data source.
Another tip, you are looping through and adding each point in one by one to the data source. A slightly faster way is to add all the points to an array and then add the array to the data source. Every time data is added to the data source it tries to update the map. Additionally, if you want to overwrite all data on the map, instead of clearing the data source and then adding your data, use the setShapes function of the data source. This will do the clear and add in one function and only trigger a single render update on the map.
Is it currently possible to use GEOLOCATION to enter points or polygons to a map layer, and access that information in form of SHP file, GPX, or KML?
The only examples I currently see render an IMAGE which is pretty much useless from a 'mapping' standpoint.
Thanks
OpenLayers 3 provides everything you need to write such an application. To track a geolocation, you can do something like this:
var geolocation = new ol.Geolocation({
tracking: true
});
var track;
geolocation.on('change:position', function() {
var coordinate = geolocation.getPosition();
if (track) {
track.appendCoordinate(coordinate);
} else {
track = new ol.geom.LineString(coordinate);
}
});
To create a GPX from the track, you can do
var gpx = new ol.format.GPX().writeFeatures([new ol.Feature(track)]);
The gpx variable now holds a string of the serialized GPX document. You can write its content to a file or upload it to a server or do whatever you want to do with it.
I'm trying to reference WindowManger so I can get the default screen's dimensions but I can't seem to find reference to it. (this is API 8, froyo 2.2). I even tried:
dynamic wm = Android.App.Application.Context.GetSystemService(Android.Content.Context.WindowService);
Display display = wm.getDefaultDisplay();
But got an error indicating that Object doesn't respond to getDefaultDisplay.
Also I tried:
var wm = (IWindowManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.WindowService);
Display display = wm.DefaultDisplay;
But I get an invalid cast exception.
Saw this post, but I can't resolve WindowManager. Any ideas what's going on here?
WindowManager is the IWindowManager interface. Instead of the cast, try using the .JavaCast<T>() extension method:
var wm = context.GetSystemService(Android.Content.Context.WindowService)
.JavaCast<IWindowManager>();
var d = wm.DefaultDisplay;
You can also check out the AccelerometerPlay sample.
Another simpler way is
Display display = this.WindowManager.DefaultDisplay;
You can get the Width and Height properties from the display object.
using Android.Runtime; //for JavaCast
var windowManager = Android.App.Application.Context.GetSystemService(Android.Content.Context.WindowService).JavaCast<IWindowManager>();
i have pairs of coorinates:
GpsLatitude GpsLongitude
32.012919 34.547592,
32.012798 34.54763,
32.012827 34.547584,
32.012814 34.547608,
32.01273 34.54765,
32.012868 34.547631,
32.012834 34.547577,
and i have show it on google map in delphi application.
i find an example here a delphi code example
and it works. But when i trying to use it with my coordinates it point
to wrong location, but when i open it in browser, like this
"32 01.2834,34 54.7577"
it work good, unfortunatly it does not work in delphi.
what can be wrong??
if you look the GotoLatLng javascript function used in the article,
function GotoLatLng(Lat, Lang) {
var latlng = new google.maps.LatLng(Lat,Lang);
map.setCenter(latlng);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title:Lat+","+Lang
});
}
You can see a call to the google.maps.LatLng() function, and this needs to be given two numbers in decimal format. you are using a value like this 32 01.2919, 34 54.7592 which is in degrees and minutes format, so that format must be converted to decimal before be passed to the javascript function. Now the reason because that value works in the google maps page is because that page internally resolves the string passed as parameter and returns a colletion of markers as result.
I've never used actionscript before, and but I've just had to dive into it in order to get a map working.
I'm using the following code to add a map marker, replacing a previous one if one exists:
public var tracer:Array = new Array();
public var tracerLng:Number = 0;
for ( var i : Number=1 ; i<64000 ; i++)
{
//Check if there is already a marker, if so get rid of it
if(tracerLng > 0) {
map.removeOverlay(tracer[0]);
tracer[0] = null;
tracer.pop();
}
// Set up a marker
var trackMrk:Marker = new Marker(
new LatLng(_lat, _lng),
new MarkerOptions({
strokeStyle: new StrokeStyle({color: 0x987654}),
fillStyle: new FillStyle({color: 0x223344, alpha: 0.8}),
radius: 12,
hasShadow: true
})
);
//Add the marker to the array and show it on the map
tracerLng = tracer.push(trackMrk);
map.addOverlay(tracer[0]);
}
My first problem is that running this code (The 64000 repeats are for testing, the final application won't need to be run quite THAT many times). Either way, memory usage increases by about 4kB/s - how do I avoid that happening?
Secondly - could anyone advise me on how to make that program more graceful?
Thanks in advance for advice
This isn't a memory leak, it's probably the result of created events - enter frames, mouse events, custom events etc. Provided that your memory doesn't keep going up and up forever, it's nothing to be worried about - it'll get garbage collected in due course.
Some points on your code:
The tracer Array doesn't seem to do anything - you only seem to be holding one thing in there at a time, so an array makes no sense. If you need an Array, use Vector instead. It's smaller and faster. More so if you create one with a specific length.
Don't create a new Marker unless you need one. Reuse old objects. Learn about object pooling: http://help.adobe.com/en_US/as3/mobile/WS948100b6829bd5a6-19cd3c2412513c24bce-8000.html or http://lostinactionscript.com/2008/10/30/object-pooling-in-as3/
The LatLng and MarkerOptions (including the stroke and fill objects) don't seem to change (I'm assuming the LatLng object lets you set a new position). If that's the case, don't create new ones when you don't need to. If you need to create new ones, StrokeStyle and FillStyle seem good candidates for a "create once, use everywhere" policy.
Create a destroy() function or similar in your Marker class and explicitly call it when you need to delete one (just before setting it to null or popping it from the array). In the destroy() function, null out any parameters to non-base classes (int, Number, String etc). Garbage collection runs using a reference counting method and a mark and sweep method. Ideally, you want to run everything using reference counting as it's collected quicker and stops any stalls in your program.
I explain memory management in AS3 a bit more here: http://divillysausages.com/blog/tracking_memory_leaks_in_as3
Also included is a class that helps you track down memory leaks if there are any