Auto-zoom to entity specified by id param

This commit is contained in:
John Firebaugh
2013-04-18 14:24:01 -07:00
parent c6200931e1
commit c8dcba31ad
5 changed files with 81 additions and 0 deletions

View File

@@ -41,6 +41,12 @@ iD.behavior.Hash = function(context) {
// do so before any features are loaded. thus wait for the feature to
// be loaded and then select
function willselect(id) {
context.connection().loadEntity(id, function(error, entity) {
if (entity) {
context.map().zoomTo(entity);
}
});
context.map().on('drawn.hash', function() {
if (!context.entity(id)) return;
selectoff();

View File

@@ -40,6 +40,18 @@ iD.Connection = function() {
return d3.xml(url).get().on('load', done);
};
connection.loadEntity = function(id, callback) {
var type = iD.Entity.id.type(id),
osmID = iD.Entity.id.toOSM(id);
connection.loadFromURL(
url + '/api/0.6/' + type + '/' + osmID + (type !== 'node' ? '/full' : ''),
function(err, entities) {
event.load(err, entities);
if (callback) callback(err, entities && entities[id]);
});
};
function authenticating() {
event.authenticating();
}

View File

@@ -25,6 +25,10 @@ iD.Entity.id.toOSM = function(id) {
return id.slice(1);
};
iD.Entity.id.type = function(id) {
return {'n': 'node', 'w': 'way', 'r': 'relation'}[id[0]];
};
// A function suitable for use as the second argument to d3.selection#data().
iD.Entity.key = function(entity) {
return entity.id;

View File

@@ -322,6 +322,12 @@ iD.Map = function(context) {
return redraw();
};
map.zoomTo = function(entity) {
var extent = entity.extent(context.graph()),
zoom = map.extentZoom(extent);
map.centerZoom(extent.center(), zoom);
};
map.centerZoom = function(loc, z) {
var centered = setCenter(loc),
zoomed = setZoom(z);

View File

@@ -79,6 +79,59 @@ describe('iD.Connection', function () {
});
});
describe('#loadEntity', function () {
var server,
nodeXML = '<?xml version="1.0" encoding="UTF-8"?><osm><node id="1" version="1" changeset="1" lat="0" lon="0" visible="true" timestamp="2009-03-07T03:26:33Z"></node></osm>',
wayXML = '<?xml version="1.0" encoding="UTF-8"?><osm>' +
'<node id="1" version="1" changeset="2817006" lat="0" lon="0" visible="true" timestamp="2009-10-11T18:03:23Z"/>' +
'<way id="1" visible="true" timestamp="2008-01-03T05:24:43Z" version="1" changeset="522559"><nd ref="1"/></way>' +
'</osm>';
beforeEach(function() {
server = sinon.fakeServer.create();
});
afterEach(function() {
server.restore();
});
it('loads a node', function(done) {
c.loadEntity('n1', function(error, entity) {
expect(entity).to.be.an.instanceOf(iD.Node);
expect(entity.id).to.eql('n1');
done();
});
server.respondWith("GET", "http://www.openstreetmap.org/api/0.6/node/1",
[200, { "Content-Type": "text/xml" }, nodeXML]);
server.respond();
});
it('loads a way', function(done) {
c.loadEntity('w1', function(error, entity) {
expect(entity).to.be.an.instanceOf(iD.Way);
expect(entity.id).to.eql('w1');
done();
});
server.respondWith("GET", "http://www.openstreetmap.org/api/0.6/way/1/full",
[200, { "Content-Type": "text/xml" }, wayXML]);
server.respond();
});
it('emits a load event', function(done) {
c.loadEntity('n1');
c.on('load', function(error, result) {
expect(result.n1).to.be.an.instanceOf(iD.Node);
done();
});
server.respondWith("GET", "http://www.openstreetmap.org/api/0.6/node/1",
[200, { "Content-Type": "text/xml" }, nodeXML]);
server.respond();
});
});
describe('#osmChangeJXON', function() {
it('converts change data to JXON', function() {
var jxon = c.osmChangeJXON('jfire', '1234', {created: [], modified: [], deleted: []});