Skip to content

Commit

Permalink
#21 Implement FitToBounds - now supported for maps declared in XML as…
Browse files Browse the repository at this point in the history
… well
  • Loading branch information
EddyVerbruggen committed Oct 15, 2016
1 parent b75ade1 commit c4899c5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,29 @@ function onMapReady(args) {
exports.onMapReady = onMapReady;
```

Note that at the moment this is the only one of the methods listed below
### .. or want to set the viewport bounds?

```js
var mapbox = require("nativescript-mapbox");

function onMapReady(args) {
args.map.setViewport([
{
bounds: {
north: 52.4820,
east: 5.1087,
south: 52.2581,
west: 4.6816
},
animated: true
}
);
}

exports.onMapReady = onMapReady;
```

Note that at the moment these are the only two of the methods listed below
you can use with the XML-rendered map API. It's very easy to add other methods though,
so please open an issue on GitHub if you need to fi get the zoom level in a similar way.

Expand Down Expand Up @@ -218,6 +240,34 @@ or remove specific marker id's (which you specified previously).
mapbox.removeMarkers([1, 2]);
```

### setViewport
If you want to for instance make the viewport contain all markers you
can set the bounds to the lat/lng of the outermost markers using this function.

```js
mapbox.setViewport(
{
bounds: {
north: 52.4820,
east: 5.1087,
south: 52.2581,
west: 4.6816
},
animated: true // default true
}
)
```

### getViewport
```js
mapbox.getViewport().then(
function(result) {
console.log("Mapbox getViewport done, result: " + JSON.stringify(result));
}
)
```


### setCenter
```js
mapbox.setCenter(
Expand Down
7 changes: 6 additions & 1 deletion mapbox-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,16 @@ var Mapbox = (function (_super) {
});
};

// TODO add more of the API methods so they can be used from the XML map's "mapReady" event
// functions that can be called from the XML map's "mapReady" event
Mapbox.prototype.addMarkers = function (args) {
mapbox.addMarkers(args, this.native);
};

Mapbox.prototype.setViewport = function (args) {
mapbox.setViewport(args, this.native);
};

// properties that can be set from XML
Object.defineProperty(Mapbox.prototype, "accessToken", {
set: function (value) {
this.config.accessToken = value;
Expand Down
8 changes: 5 additions & 3 deletions mapbox.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,12 @@ mapbox.getViewport = function (arg) {
});
};

mapbox.setViewport = function (arg) {
mapbox.setViewport = function (arg, nativeMap) {
return new Promise(function (resolve, reject) {
try {
if (!mapbox.mapboxMap) {
var theMap = nativeMap || mapbox;

if (!theMap) {
reject("No map has been loaded");
return;
}
Expand All @@ -603,7 +605,7 @@ mapbox.setViewport = function (arg) {
var animated = arg.animated === undefined || arg.animated;
var padding = 25;

mapbox.mapboxMap.easeCamera(
theMap.mapboxMap.easeCamera(
com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newLatLngBounds(bounds, padding),
animated ? 1000 : 0);

Expand Down
10 changes: 6 additions & 4 deletions mapbox.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,16 @@ mapbox.getViewport = function (arg) {
});
};

mapbox.setViewport = function (arg) {
mapbox.setViewport = function (arg, nativeMap) {
return new Promise(function (resolve, reject) {
try {
if (!mapbox.mapView) {
var theMap = nativeMap || mapbox.mapView;

if (!theMap) {
reject("No map has been loaded");
return;
}

var swCoordinate = CLLocationCoordinate2DMake(arg.bounds.south, arg.bounds.west);
var neCoordinate = CLLocationCoordinate2DMake(arg.bounds.north, arg.bounds.east);
var bounds = MGLCoordinateBounds;
Expand All @@ -455,7 +457,7 @@ mapbox.setViewport = function (arg) {
var animated = arg.animated === undefined || arg.animated;
var padding = UIEdgeInsetsMake(25, 25, 25, 25);

mapbox.mapView.setVisibleCoordinateBoundsEdgePaddingAnimated(bounds, padding, animated);
theMap.setVisibleCoordinateBoundsEdgePaddingAnimated(bounds, padding, animated);
resolve();
} catch (ex) {
console.log("Error in mapbox.setViewport: " + ex);
Expand Down

0 comments on commit c4899c5

Please sign in to comment.