Skip to content

Commit

Permalink
Allow to use icon path from resource #43
Browse files Browse the repository at this point in the history
ShowUserLocation not working #31
  • Loading branch information
EddyVerbruggen committed Oct 26, 2016
1 parent 43f7576 commit 85a3156
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 52 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ Check out the usage details on the functions below.
lng: 4.8941680, // mandatory
title: 'Nice location', // recommended to pass in
subtitle: 'Really really nice location', // one line is available on iOS, multiple on Android
icon: 'res://cool_marker', // use either this preferred way (to grab a density-independent marker from app resources), or:
iconPath: 'res/markers/green_pin_marker.png', // anywhere in your app folder
onTap: function(marker) { console.log("This marker was tapped"); },
onCalloutTap: function(marker) { console.log("The callout of this marker was tapped"); }
Expand Down Expand Up @@ -220,6 +221,7 @@ instead of redrawing it (which is a lot slower and you loose the viewport positi
lng: 4.8891680, // mandatory
title: 'One-line title here', // no popup unless set
subtitle: 'Infamous subtitle!',
icon: 'res://cool_marker', // preferred way, otherwise use:
iconPath: 'res/markers/home_marker.png',
onTap: onTap,
onCalloutTap: onCalloutTap
Expand Down
81 changes: 42 additions & 39 deletions mapbox.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,37 +203,41 @@ mapbox.show = function(arg) {

if (settings.showUserLocation) {
if (mapbox._fineLocationPermissionGranted()) {
/*
mapbox.locationServices = com.mapbox.mapboxsdk.location.LocationServices.getLocationServices(application.android.context);
mapbox.locationServices.addLocationListener(new com.mapbox.mapboxsdk.location.LocationListener({
onLocationChanged: function(location) {
if (location !== null) {
if (mapbox._locationMarkerAdded) {
mapbox._removeMarkers([999999]);
} else {
mapbox._locationMarkerAdded = true;
}
mapbox._addMarkers([{
id: 999999,
// TODO find a nice marker resource and bundle with the plugin for now
icon: "res://location",
lat: location.getLatitude(),
lng: location.getLongitude()
}]);
}
}
})
);
*/
mapbox.locationServices = com.mapbox.mapboxsdk.location.LocationServices.getLocationServices(application.android.context);

mapbox.locationServices.addLocationListener(new com.mapbox.mapboxsdk.location.LocationListener({
onLocationChanged: function (location) {
if (location !== null) {
if (mapbox._locationMarkerAdded) {
mapbox._removeMarkers([999997, 999998]);
} else {
mapbox._locationMarkerAdded = true;
}
mapbox._addMarkers([
{
id: 999997,
icon: "res://ic_mylocationview_normal",
lat: location.getLatitude(),
lng: location.getLongitude()
},
{
id: 999998,
icon: "res://ic_mylocationview_background",
lat: location.getLatitude(),
lng: location.getLongitude()
}
]);
}
}
})
);
mapbox.mapboxMap.setMyLocationEnabled(true);

} else {
// devs should ask permission upfront, otherwise enabling location will crash the app on Android 6
console.log("Mapbox plugin: not showing the user location on this device because persmission was not requested/granted");
}
}

resolve();
}
})
Expand Down Expand Up @@ -344,7 +348,10 @@ mapbox._removeMarkers = function (ids, nativeMap) {
for (var m in mapbox._markers) {
var marker = mapbox._markers[m];
if (!ids || (marker.id && ids.indexOf(marker.id) > -1)) {
theMap.mapboxMap.removeAnnotation(marker.android);
// don't remove the location markers in case 'removeAll' was invoked
if (ids || (marker.id != 999997 && marker.id != 999998)) {
theMap.mapboxMap.removeAnnotation(marker.android);
}
}
}
};
Expand All @@ -371,6 +378,7 @@ mapbox._addMarkers = function(markers, nativeMap) {
return;
}
var theMap = nativeMap || mapbox;
var iconFactory = com.mapbox.mapboxsdk.annotations.IconFactory.getInstance(application.android.context);
for (var m in markers) {
var marker = markers[m];
mapbox._markers.push(marker);
Expand All @@ -385,26 +393,21 @@ mapbox._addMarkers = function(markers, nativeMap) {
var identifier = res.getIdentifier(resourcename, "drawable", utils.ad.getApplication().getPackageName());
console.log("-- loc identifier: " + identifier);

var iconDrawable = android.support.v4.content.ContextCompat.getDrawable(application.android.context, identifier);
console.log("-- loc iconDrawable: " + iconDrawable);

var iconFactory = com.mapbox.mapboxsdk.annotations.IconFactory.getInstance(application.android.context);
var icon = iconFactory.fromDrawable(iconDrawable);

markerOptions.setIcon(icon);
if (identifier === 0) {
console.log("No icon found for this device desity for icon " + marker.icon + ", using default");
} else {
var iconDrawable = android.support.v4.content.ContextCompat.getDrawable(application.android.context, identifier);
markerOptions.setIcon(iconFactory.fromDrawable(iconDrawable));
}
} else {
console.log("Please use res://resourcename, or iconPath to use a local path");
}
} else if (marker.iconPath) {
// TODO these bits can be cached
var iconFactory = com.mapbox.mapboxsdk.annotations.IconFactory.getInstance(application.android.context);
var appPath = fs.knownFolders.currentApp().path;
var iconFullPath = appPath + "/" + marker.iconPath;
var iconFullPath = fs.knownFolders.currentApp().path + "/" + marker.iconPath;
// if the file doesn't exist the app will crash, so checking it
if (fs.File.exists(iconFullPath)) {
var icon = iconFactory.fromPath(iconFullPath);
// TODO (future) width, height, retina, see https://github.com/Telerik-Verified-Plugins/Mapbox/pull/42/files?diff=unified&short_path=1c65267
markerOptions.setIcon(icon);
// could set width, height, retina, see https://github.com/Telerik-Verified-Plugins/Mapbox/pull/42/files?diff=unified&short_path=1c65267, but that's what the marker.icon param is for..
markerOptions.setIcon(iconFactory.fromPath(iconFullPath));
} else {
console.log("Marker icon not found, using the default instead. Requested full path: " + iconFullPath);
}
Expand Down
7 changes: 7 additions & 0 deletions mapbox.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ declare module "nativescript-mapbox" {
title?: string;
subtitle?: string;
/**
* Prefix with 'res://' and load a file from the resources folder.
* Details on how 'res://' is used can be found here: https://docs.nativescript.org/ui/images#load-images-from-resource
* Example: "res://iconfile"
*/
icon?: string;
/**
* The preferred way is using the 'icon' property, but you can still reference a local file directly.
* Example: "res/markers/green_pin_marker.png"
*/
iconPath?: string;
Expand Down
35 changes: 23 additions & 12 deletions mapbox.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,15 @@ mapbox.animateCamera = function (arg, nativeMap) {
return new Promise(function (resolve, reject) {
try {
var theMap = nativeMap || mapbox.mapView;

var target = arg.target;
if (target === undefined) {
reject("Please set the 'target' parameter");
return;
}

var cam = MGLMapCamera.camera();

cam.centerCoordinate = CLLocationCoordinate2DMake(target.lat, target.lng);

if (arg.altitude) {
Expand Down Expand Up @@ -495,7 +495,7 @@ mapbox.setViewport = function (arg, nativeMap) {
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 Down Expand Up @@ -701,17 +701,28 @@ var MGLMapViewDelegateImpl = (function (_super) {
// fired when the marker icon is about to be rendered - return null for the default icon
MGLMapViewDelegateImpl.prototype.mapViewImageForAnnotation = function(mapView, annotation) {
var cachedMarker = _getTappedMarkerDetails(annotation);
if (cachedMarker && cachedMarker.iconPath) {
if (cachedMarker) {
if (cachedMarker.reuseIdentifier) {
return mapView.dequeueReusableAnnotationImageWithIdentifier(cachedMarker.reuseIdentifier);
var reusedImage = mapView.dequeueReusableAnnotationImageWithIdentifier(cachedMarker.reuseIdentifier);
if (reusedImage) {
return reusedImage;
}
}
var appPath = fs.knownFolders.currentApp().path;
var iconFullPath = appPath + "/" + cachedMarker.iconPath;
if (fs.File.exists(iconFullPath)) {
var image = imgSrc.fromFile(iconFullPath).ios;
// TODO (future) add resize options for nice retina rendering
cachedMarker.reuseIdentifier = cachedMarker.iconPath;
return MGLAnnotationImage.annotationImageWithImageReuseIdentifier(image, cachedMarker.reuseIdentifier);

if (cachedMarker.icon) {
var resourcename = cachedMarker.icon.substring(6);
var imageSource = imgSrc.fromResource(resourcename);
cachedMarker.reuseIdentifier = cachedMarker.icon;
return MGLAnnotationImage.annotationImageWithImageReuseIdentifier(imageSource.ios, cachedMarker.reuseIdentifier);
} else if (cachedMarker.iconPath) {
var appPath = fs.knownFolders.currentApp().path;
var iconFullPath = appPath + "/" + cachedMarker.iconPath;
if (fs.File.exists(iconFullPath)) {
var image = imgSrc.fromFile(iconFullPath).ios;
// perhaps add resize options for nice retina rendering (although you can now use the 'icon' param instead)
cachedMarker.reuseIdentifier = cachedMarker.iconPath;
return MGLAnnotationImage.annotationImageWithImageReuseIdentifier(image, cachedMarker.reuseIdentifier);
}
}
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-mapbox",
"version": "2.1.1",
"version": "2.2.0",
"description": "Native Maps, by Mapbox.",
"main": "mapbox.js",
"typings": "mapbox.d.ts",
Expand Down

0 comments on commit 85a3156

Please sign in to comment.