NativeScript: addMarkers outside of onMapReady - mapbox

I'm new in NativeScript, and I'm playing with maps, using Mapbox.
I want add markers, programmatically from a function when tap a buttom, to map.
XML
` <Button text="GET" tap="getRequest" /> <<<-- BUTTON!
<ContentView>
<map:MapboxView
accessToken= token
mapStyle="streets"
zoomLevel="13"
showUserLocation="false"
disableRotation= "true"
disableTilt="false"
mapReady="onMapReady">
</map:MapboxView>
</ContentView>`
JS
`function onMapReady(args) {
args.map.addMarkers([
{
id: 1,
lat: -35.30505050,
lng: -47.56263254,
title: 'Company 1', // no popup unless set
subtitle: 'Subt 1',
iconPath: 'markers/green_pin_marker.png',
onTap: function () { console.log("'Nice location' marker tapped"); },
onCalloutTap: function () {
console.log("'Nice location' marker callout tapped");
console.log(lati + long);
}
}
]).then(
function (result) {
console.log("Mapbox addMarkers done");
},
function (error) {
console.log("mapbox addMarkers error: " + error);
})
}
exports.onMapReady = onMapReady;`
That code works fine, the marker ID 1 appears on map.
My question is: how can add others markers from a function that responde to tap button:
exports.getRequest = function () {
console.log("BUTTON TAPPED!");
args.map.addMarkers([
{
id: 2,
lat: -35.30586500,
lng: -47.56218500,
title: 'Company 2', // no popup unless set
subtitle: 'Subt 2',
iconPath: 'markers/green_pin_marker.png',
onTap: function () { console.log(" marker tapped"); },
onCalloutTap: function () {
console.log("marker callout tapped");
console.log(lati + long);
}
}
]).then(
function (result) {
console.log("Mapbox addMarkers done");
},
function (error) {
console.log("mapbox addMarkers error: " + error);
})
}
When tap button, console show message BUTTON TAPPED!, but no new mapker ID 2 on map.
I'm doing bad or forgeting something?

Well, it's in the readme of the plugin repo: https://github.com/EddyVerbruggen/nativescript-mapbox/tree/26019957e4e3af3e737d7a44c845f5d5b1bfb808#addmarkers
So here's a JavaScript example, but that repo also has a TypeScript-based demo app with an 'add markers' button that you can check out:
var mapbox = require("nativescript-mapbox");
var onTap = function(marker) {
console.log("Marker tapped with title: '" + marker.title + "'");
};
var onCalloutTap = function(marker) {
alert("Marker callout tapped with title: '" + marker.title + "'");
};
mapbox.addMarkers([
{
id: 2, // can be user in 'removeMarkers()'
lat: 52.3602160, // mandatory
lng: 4.8891680, // mandatory
title: 'One-line title here', // no popup unless set
subtitle: 'Infamous subtitle!',
// icon: 'res://cool_marker', // preferred way, otherwise use:
icon: 'http(s)://website/coolimage.png', // from the internet (see the note at the bottom of this readme), or:
iconPath: 'res/markers/home_marker.png',
selected: true, // makes the callout show immediately when the marker is added (note: only 1 marker can be selected at a time)
onTap: onTap,
onCalloutTap: onCalloutTap
},
{
// more markers..
}
])

I also cannot use Mapbox as a const/var... or do anything programmatically. I get undefined is not a function, yet Mapbox to log yields the module and its objects. I can see the appropriate functions under prototype:Mapbox etc.
Only declaring the map in XML and utilizing the MapOnReady function works for me.
Update:
I stumbled upon this thread from {N} discourse that helped me understand:
https://discourse.nativescript.org/t/adding-mapbox-to-layout-container/4679/11
Basically the programatic way of building the map still does not allow interaction with the map after it has been rendered. You just declare all the map options as shown in the git example and then still use onMapReady as your function to add markers, polylines etc... you can still setup map listeners of course.

Related

leaflet routing.control update when marker is moved

I am using leaflet and routing.control to show a route. I have it working fine, but I would like one of the markers to move with the users location using watch.position. But for now I a just trying to move the marker when I click a button. Again this works fine but when the marker moves I would like the route to update automatically. Its possible if you drag the marker so surely its possible when marker is moved in a different way? I can it if I remove the control and add a new one but this flickers too much. Any advice?
The code for the routing.control is
myroute = L.Routing.control({
waypoints: [
L.latLng(window.my_lat, window.my_lng),
L.latLng(window.job_p_lat, window.job_p_lng)
],show: true, units: 'imperial',
router: L.Routing.mapbox('API KEY HERE'),
createMarker: function(i, wp, nWps) {
if (i === 0 || i === nWps + 1) {
return mymarker = L.marker(wp.latLng, {
icon: redIcon
});
} else {
return job_start = L.marker(wp.latLng, {
icon: greenIcon
});
}
}
}).addTo(map);
and the code for moving the marker is
function movemarker() {
var lat = "52.410490";
var lng = "-1.575950";
var newLatLng = new L.LatLng(lat, lng);
mymarker.setLatLng(newLatLng);
// I assume I call something here?
}
Sorted, I did it with this, which removes first point and replaces it with new data
myroutewithout.spliceWaypoints(0, 1, newLatLng);

add leave or stay on the page alert in ionic 3?

I'm trying to add a popup that will be shown on a page when the user starts to populate data in the form and then he decided to go somewhere else in the app.
This popup will show this message: 'Do you want to leave this page and save your changes?'
three buttons are available: Stay, Leave and Save before leaving.
I'm new to ionic logic and I couldn't figure out how to do this.
I started by adding a button in the page that shows the popup (Still don't know how to trigger the event when the user clicks on any link of the sidebar for example). when the user clicks on that button the popup is shown with the three buttons.
The problem is that I don't know how to implement the handlers of these buttons.
This is what I have in the ts file :
leaveOrStayModal() {
let e = event || window.event;
e.stopPropagation();
this.alertMixin.presentAlert(
'Do you want to leave this site?\n',
"You haven't saved your changes!",
'Stay',
'Leave',
'Save',
null,
() => {
console.log('leave handler')
// this.navCtrl.push() Here I don't know how to get the exact link clicked from the sidebar ? to go to
},
() => {
console.log('Save handler')
//here I want to save the form ?
}
)
}
the popup code:
presentAlert(title: string, message: string, btnOneText: string, btnTowText: string, btnThreeText: string,
btnOneHandler?: () => void, btnTowHandler?: () => void, btnThreeHandler?: () => void,
present: boolean = true) {
let confirm = this.alertCtrl.create({
title: title,
message: message,
buttons: [
{
text: btnOneText,
handler: () => {
if (btnOneHandler) {
btnOneHandler();
}
}
},
{
text: btnTowText,
handler: () => {
if (btnTowHandler) {
btnTowHandler();
}
}
},
{
text: btnThreeText,
handler: () => {
if (btnThreeHandler) {
btnThreeHandler();
}
}
}
]
});
if (present) {
confirm.present().then();
}
return confirm;
}
And this is the button that shows the popup (to be removed )
<button ion-button icon-left item-right type="button" (click)="leaveOrStayModal()"> Click to show modal </button>
You should leverage life cycle hook ionViewCanLeave for that. Some basic documentation here: https://ionicframework.com/docs/api/navigation/NavController/
For your context I just drafted the way I would do it (its a bit dirty):
userCanLeave = false;
ionViewCanLeave() {
// here you can use other vars to see if there are reasons we want to keep user in this page:
if (!this.userCanLeave) {
return new Promise((resolve, reject) => {
let alert = this.alertCtrl.create({
title: 'Are you sure?',
message: 'The form data may be lost',
buttons: [
{
text: 'Stay',
role: 'cancel',
handler: () => {
console.log('User stayed');
this.userCanLeave = false;
reject();
}
},
{
text: 'Leave',
handler: () => {
console.log('User leaves');
this.userCanLeave = true;
resolve();
}
},
{
text: 'Save',
handler: () => {
console.log('User saved data');
// do saving logic
this.userCanLeave = true;
resolve();
}
}
]
});
alert.present();
});
} else { return true }
}
userCanLeave - here is just example of a var that defines if the page has the state where we would not want a user to leave "freely".
then we use promise to ensure that user can not leave without answering dialogue options, we wait for their answers to define whether life cycle hook gets true/false flag to proceed.
Please note also that this life cycle hook only "kicks in" when a view (page) gets off the stack (pops) if you would push in a new view - it won't guard that. But in this case new pushed in page won't destroy user's data in the form anyway and user can safely return to it once you dismiss that newly pushed in page.
Hope this helps.

Nativescript mapbox custom marker tooltip

I am using mapbox in nativescript, how we can add a custom view (HTML or XML) to the marker tooltip.
Markers show only title and subtitle, I need to view my own layout instead
map.addMarkers([{
lat: 52.3602160,
lng: 4.8891680,
title: 'marker title',
subtitle: 'marker subtitle',
image: 'https://farm9.staticflickr.com/8571/15844010757_63b093d527_n.jpg'
}]);
According to the plugin README you can add you own icons for the markers via icon and iconPath
var onTap = function(marker) {
console.log("Marker tapped with title: '" + marker.title + "'");
};
var onCalloutTap = function(marker) {
alert("Marker callout tapped with title: '" + marker.title + "'");
};
mapbox.addMarkers([
{
id: 2, // can be user in 'removeMarkers()'
lat: 52.3602160, // mandatory
lng: 4.8891680, // mandatory
title: 'One-line title here', // no popup unless set
subtitle: 'Infamous subtitle!',
icon: 'res://cool_marker', // preferred way, otherwise use:
icon: 'http(s)://website/coolimage.png', // from the internet (see the note at the bottom of this readme), or:
iconPath: 'res/markers/home_marker.png',
onTap: onTap,
onCalloutTap: onCalloutTap
},
{
..
}
])
There is also the official demo which demonstrates how to use the icons for markers

Google Map Makers Sprite in OSX not displaying

I can't seem to get the my map markers to display when I use an image sprite on the iphone. They appear when I use the standard google map markers on iphone and when viewing the site in the desktop the sprite icons work fine.
Here is the code I use to create the markers, I am using Zepto but JQuery could as easily apply.
$.ajax({
dataType: 'jsonp',
url: myLocations.LocatorUrl,
timeout: 8000,
success: function(data) {
var infoWindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
$.each(data, function(index, item){
var data = item, pincolor,
latLng = new google.maps.LatLng(data.lat, data.lng);
var d = 'http://blah';
var pinImage = new google.maps.MarkerImage(d+"/assets/img/sprite.locator.png",
new google.maps.Size(24, 36),
new google.maps.Point(0,25),
new google.maps.Point(10, 34));
// Creating a marker and putting it on the map
var marker = new google.maps.Marker({
position: latLng,
map: map,
title: data.type,
icon: pinImage
});
bounds.extend(latLng); // Extend the Latlng bound method
var bubbleHtml = '<div class="bubble"><h2>'+item.type+'</h2><p>'+item.address+'</p></div>'; // Custom HTML for the bubble
(function(marker, data) {
// Attaching a click event to the current marker
google.maps.event.addListener(marker, "click", function(e) {
infoWindow.setContent(bubbleHtml);
infoWindow.open(map, marker);
});
markers.push(marker); // Push markers into an array so they can be removed
})(marker, data);
});
map.fitBounds(bounds); // Center based on values added to bounds
}, error: function(x, t, m) {
console.log('errors')
if(t==="timeout") {
alert("got timeout");
} else {
alert(t);
}
}
});
Got it. Turns out the images I was referencing were on localhost, when I swapped this to the actual IP address of my local machine it worked.

iPhone showing a blank infoWindow fro google maps

The info window shows ok on desktop, and if I set an alert to show the content it will show the correct html code. However, on the iPhone it will just pop a blank info window (No text within it).
Here is my code:
function showPOICategory(category) {
// Icons { ID, Image }
// Entry { Latitude, Longitude, Name, Description, iconID);
$.getJSON('ajax/poi.php?key=' + jQGMSettings.apiKey + '&c=' + category , function(data) {
$.each(data.poi, function(key, val) {
// Set current position marker
var $image = new google.maps.MarkerImage('/images/pois/'+data.icons[val.image],
// This marker is 20 pixels wide by 32 pixels tall.
new google.maps.Size(32, 37),
// The origin for this image is 0,0.
new google.maps.Point(0,0),
// The anchor for this image is the base of the flagpole at 0,32.
new google.maps.Point(16, 37)
);
var $marker = new google.maps.Marker({
title: val.title,
icon: $image,
clickable: true,
draggable: false,
position: new google.maps.LatLng(val.latitude, val.longitude),
animation: google.maps.Animation.DROP,
map: map
});
// Info Window
if( val.info == null ) {
var $infowindow = new google.maps.InfoWindow({
content: '<div><h1>' + val.title + '</h1>Prueba</div>'
});
} else {
var $infowindow = new google.maps.InfoWindow({
content: '<div style="color:#000000;"><h1 style="font-size:14px; font-family:Helvetica; padding:0px; margin:0px;">' + val.title + '</h1>' + val.info + 'Prueba</div>',
maxWidth: 200
});
}
var $listener = google.maps.event.addListener($marker, 'click', function() {
if( infoWindow != null ) {
infoWindow.close();
}
infoWindow = $infowindow;
infoWindow.open(map,$marker);
});
// Keep track of the marker to remove it ;)
pois.push({
marker: $marker,
listener: $listener
});
});
});
}
Anyone had this problem before? I'm going nutts to find out where the problem could be located.
OK I finally was able to solve my issue but not sure if it will help you. Are you using loadHTMLString method? if so and you aren't declaring a baseURL try declaring in the call
baseURL:[NSURL URLWithString:#"/"]