How can delete a polygon on google maps Ionic 3 - google-maps

I am new with the Native Google Maps API with Cordova using Ionic 3, and I would like to know the way to delete polygons, markers, circles, etc. with a button, for example.
Actually, I have in a method called loadMap() the parts to create a polygon and it works. But I would like to be able from another method, like removePolygon(), remove the polygon.
My loadMap()code is the follows:
loadMap() {
let mapOptions: GoogleMapOptions = {
camera: {
target: {
lat: 39.695263,
lng: 3.017571
},
zoom: 8,
}
};
this.map = GoogleMaps.create('map_canvas', mapOptions);
let myPolygonCoord: ILatLng[] = [
{
lat: 39.81952717082459,
lng: 3.1078016219598794
},
{
lat: 39.7794339492445,
lng: 3.0212842879755044
},
{
lat: 39.66007130788319,
lng: 3.1476270614130044
},
{
lat: 39.72030660365558,
lng: 3.2739698348505044
}
];
let myPolygon: PolylineOptions = {
'points': myPolygonCoord,
'strokeColor': '#89c3eb',
'fillColor': '#89c3eb',
'strokeWidth': 2
};
this.map.addPolygon(myPolygon).then((polygon: Polygon) => {});
}
By the other hand, on the method removePolygon():
removePolygon(){
//how to call the map and remove the polygon?
}
Any help would be appreciated it.

Well, after trying different stuff, I could find a way to solve this.
Declare a variable the one stores the value returned after create the polygon, and after that, just simply use the method remove:
generalPolygon : any;
this.map.addPolygon(myPolygon).then((polygon: Polygon) => {this.generalPolygon = polygon});
removePolygon(){
this.generalPolygon.remove();
}
And that's it.

Related

How to change active marker icon in vue by clicking

I'm using vue2-google-maps and it would be perfect - to change the marker icon by click on it. I try to write some code but it doesn't work correctly.
Template:
<gmap-map
ref="mainMap"
:center="startLocation"
:zoom="6"
map-type-id="roadmap"
style="width: 100%; height: 100%"
:options="{
zoomControl: false,
scaleControl: false,
mapTypeControl: false,
fullscreenControl: false,
streetViewControl: false,
disableDefaultUi: false
}"
>
<gmap-marker
v-for="(item, key) in coordinates"
:key="key"
:position="getPosition(item)"
:clickable="true"
:icon="markerOptions"
#click="toggleInfo(item, key)"
></gmap-marker>
</gmap-map>
Script:
...
let mapMarker = require('../images/ic-map-marker.svg'),
mapMarkerActive = require('../images/ic-map-marker-active.svg');
...
startLocation: {
lat: 49.0384,
lng: 33.4513
},
coordinates: {
0: {
city: City 0,
lat: '50.4021702',
lng: '30.3926088'
},
1: {
city: City 1,
lat: '49.9543502',
lng: '36.1241697'
}
},
infoOpened: false,
infoCurrentKey = null,
markerOptions: {
url: mapMarker
}
...
toggleInfo: function (marker, key) {
if (this.infoCurrentKey == key) {
if (this.infoOpened) {
this.infoOpened = false;
this.markerOptions = this.mapMarker;
} else {
this.infoOpened = true;
this.markerOptions = this.mapMarkerActive;
}
} else {
this.infoOpened = true;
this.infoCurrentKey = key;
this.markerOptions = this.mapMarkerActive;
}
}
All the markers are changed to active icon by click. Could someone help me please?
Thanks in advance!
Whole code here: https://codesandbox.io/s/map-vue-template-mv33z
all the map markers are referencing the same marker options object. When you change that object then it affects all the map markers.
you can have a data property called
selectedKey: null
have a method like:
getMarkers(key) {
if (this.selectedKey === key) return this.mapMarkerActive;
return this.mapMarker;
},
then you can have
:icon="getMarkers(key)"
or whatever logic you want to identify the selected marker (personally i'd put an id on the marker coordinate object and then have a selectedMarker data property)
in my example i have modified your example and the icons now display as you require.
https://codesandbox.io/s/map-vue-template-q67u0

how to add google map as a ui component in magento 2

i need to display a coordinates field , coming from database, currently am displaying it as an input like this:
i need to display it as map like this:
i googled a LOT and lot of answers on how to load the map using requireJS , but non is working with Magento 2.1..
currently am at this stage:
requirejs.config({
paths: {
googlemaps: 'googlemaps',
async: 'requirejs-plugins/src/async'
},
googlemaps: {
params: {
key: 'xxxxxxxxxxxxxx'
}
}
});
define([
'Magento_Ui/js/form/element/abstract',
'jquery',
'googlemaps!'
],function(Abstract,$,gmaps) {
return Abstract.extend({
initialize: function () {
return this._super();
},
initMap: function() {
console.log(this.value());
console.log("-------------");
var uluru = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
},
});
});
now , question is how to call the initMap function after page is loaded ?
help pleaaaaase

Map/polygon conversion from KML/JSON/VARIOUS mapit source to Lat/Long polygon?

I'd like to change the map zone I have in my custom backend at work.
The current inputs result in a map that is not correct.
The format this backend uses looks like
51.258548 -0.187498
51.302355 -0.30708
51.30872 -0.393738
51.328764 -0.469456
51.401552 -0.527588
51.500036 -0.489758
51.563533 -0.530822
etc etc
But I need to have the map file hosted at https://mapit.mysociety.org/area/2247.html instead.
My issue is they seem incompatible and I've not been able to Google search a tool that will do this for me? (There's not of converters, but nothing that covered the task at hand)
Apologies if it's a 'dumb' question, even finding out the name of the mapping syntax in my backend would help enormously
Theses are a (partial) list of co-ordinates which represent points withing a polygon area.
My issue is they seem incompatible and I've not been able to Google search a tool that will do this for me?
Just type them in for a simple solution. Depending on technology uses pass the to the javascript as an array and read them plotting them on the map.
But I need to have the map file hosted at https://mapit.mysociety.org/area/2247.html instead.
With the co-ordinates you gave you can achieve the following with Google-maps-api:
Link to JSFiddle: https://jsfiddle.net/y6f9fre6/
// This example creates a simple polygon representing the Bermuda Triangle.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: {
lat: 51.401552,
lng: -0.527588
},
mapTypeId: 'roadmap'
});
//your co-ordinates go here
var triangleCoords = [{
lat: 51.258548,
lng: -0.187498
}, {
lat: 51.302355,
lng: -0.30708
}, {
lat: 51.30872,
lng: -0.393738
},
{
lat: 51.328764,
lng: -0.469456
},
{
lat: 51.401552,
lng: -0.527588
},
{
lat: 51.500036,
lng: -0.489758
},
{
lat: 51.563533,
lng: -0.530822
},
];
// Construct the polygon.
var coordinates = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35
});
coordinates.setMap(map);
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>

Having problems reading in coordinates for markers from mongo for google maps

I have a mongoose schema that contains the lat and long for markers in google maps.
I GET the information fine and I can display what is stored in the html template using angular2 so the information is arriving. But, what I want to do, is loop through the callback array that was returned and use the lat and long values to add markers to the map.
I think I have the logic correct after messing around with the component but now I dont know how to feed the coordinates into the code for setting up the markers.
Does anyone know how to go about this correctly using typescript?
When I do a console log of the stores after the callback its undefined,
when I log the length its 0 but this is confusing me as the data is appearing in the html with angular and its there when I find it in the mongo shell.
export class MapsComponent implements OnInit {
stores: Store[] = [];
constructor(private storeService: StoreService) {}
ngOnInit() {
this.storeService.getStores()
.subscribe(
stores => this.stores = stores,
error => console.error(error)
);
console.log(stores);
var ireLatLng = {lat: 53.1424, lng: -7.6921};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: ireLatLng
});
stores.forEach(onAddMarker(stores.firstName,stores.lat,stores.long) {
console.log(stores.firstName + "added");
});
}
onAddMarker(name: string,lt: string,ln: string) {
var myLatLng = {lat: lt, lng: ln};
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: name,
});
}
}
First, if the output of
console.log(stores);
Is empty, it's because you're retrieving the mongo results asynchronously. You should try and create the markers when you actually receive them.
Second, even if you had a store array, inside your onAddMarker method you don't have access to the local variable map you declared in method ngOnInit, so you either pass it as a parameter to onAddMarker or declare it as an instance property.
Third, a latLngLiteral object should have numbers as its values, not strings.
All in all, your ngOnInit method should look like
ngOnInit() {
var ireLatLng = {lat: 53.1424, lng: -7.6921};
this.map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: ireLatLng
});
this.storeService.getStores()
.subscribe(
stores => {
this.stores = stores;
console.log(stores);
stores.forEach(store => {
onAddMarker(store.firstName, store.lat, store.long);
console.log(store.firstName + "added");
}
},
error => console.error(error)
);
}
your onAddMarker method should look like
onAddMarker(name: string,lt: string,ln: string) {
var myLatLng = {lat: 1*lt, lng: 1*ln},
map = this.map,
marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: name,
});
}

Gmap3 applying a new center postion without destroying the whole map

I am hacking around with gmap3 managing to get the first task working. Showing the map using a lat long.
My javascript look likes
$('#map_canvas').gmap3(
{
action: 'init',
options: {
center: [x,y],
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControlOptions: {
mapTypeIds: []
}
}
},
{
action: 'addMarkers',
radius: 100,
markers: GetMarkers(),
clusters:
{
0:
{
content: '<div class="cluster cluster-3">CLUSTER_COUNT <div class="cluster-3text">Stops</div> </div>',
width: 66,
height: 65
}
},
marker:
{
options:
{
icon: new google.maps.MarkerImage('../img/marker.png', size, origin, null, null)
},
events:
{
mouseover: function (marker, event, data) {
getArrivalsAndStop(marker, event, data);
},
mouseout: function () {
$(this).gmap3({ action: 'clear', name: 'overlay' });
}
}
}
});
This loads the map how I want. My next step is to be able apply a new lat & long. How can I do this without destroying the whole map and recreating it everytime?
I am using jquery gmap3 plugin.
If you want to call setCenter after having initialized the map and without reloading the whole thing you can do this like this :
$('#mymap').gmap3('get').setCenter(new google.maps.LatLng(48.706685,-3.486694899999975))
$('#mymap').gmap3('get') is the magic thing here, it give you the real google map object I think.
It was hard for me to find this out so I hope it can help someone else.
From the documentation:
This example executes map.setCenter with the result of the address resolution, target is not specified because this is the map.
$('#test').gmap3(
{
action: 'getLatLng',
address: '2 bis rue saint antoine, eguilles',
callback: function(result){
if (result){
$(this).gmap3({action: 'setCenter', args:[ result[0].geometry.location ]});
} else {
alert('Bad address 2 !');
}
}
});
So it looks like if you know the coordinates and don't need to use the geocoder, it would be:
$(this).gmap3({action: 'setCenter', args:[ new google.maps.LatLng(latitude, longitude) ]});
Kind of late in the game, but I am using a list and map(google maps type). hope it helps
Here is how I achieved it
$(document).delegate('#inner_list li', 'click', function () {
var id = $(this).attr('data-id');
$("#map-canvas").gmap3({
get: {
name:"marker",
id: id,
all: true,
callback: function(objs){
$.each(objs, function(i, obj){
obj.setIcon("http://www.markericon.com");
var newLL = obj.getPosition()
$('#map-canvas').gmap3('get').panTo(newLL)
});
}
}
});
});