I have a Google Map with a set of markers on. I'm trying to add MarkerClusterer to it to simplify the view. Overall, doing so should be pretty easy from what I've seen online but when I try it, my map doesn't show any markers, clusters or anything. My code is below:
function calculateCenter() {
center = map.getCenter()
}
let center
let worldCenter = new window.google.maps.LatLng(32.249974, 5.800781)
let mapOptions = {
center: worldCenter,
zoom: 3,
scrollwheel: false
}
let map = new google.maps.Map(document.querySelector('.banner--map--parks'), mapOptions)
let infoWindow = new google.maps.InfoWindow()
let markers = []
map.addListener('idle', _ => {
calculateCenter()
})
fetch('/api/parks.json')
.then(response => response.json())
.then(data => {
data.forEach(park => {
if (park.lat && park.lng) {
let title = park.name
let position = new google.maps.LatLng(park.lat, park.lng)
let icon = 'https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_blue.png'
let marker = new google.maps.Marker({
position: position
title: title,
icon: icon,
// map: map
})
let parkIWContent = '<div class="iw">' + park.name + '</div>'
marker.addListener('click', (function(marker, parkIWContent) {
return function() {
infoWindow.setContent(parkIWContent);
infoWindow.open(map, marker);
// map.panTo(new google.maps.LatLng(park.lat, park.lng))
};
})(marker, parkIWContent))
markers.push(marker)
}
})
})
console.log(markers)
let markerCluster = new MarkerClusterer(map, markers)
map.addListener("click", _ => {
infoWindow.close()
})
As you can see, at one point I did output the array of markers and they do all show up in the console but still fail to show on the map.
Can anyone spot any issues with the code that may be causing this?
Thanks
I've actually managed to solve this. I needed to move the Marker Clusterer inside the .then in order for it to work.
Related
I am currently working on ionic native google maps building weather map . Basically i want to get LatLngBounds and zoom level while camera move then pass those data to url even shows data on map .
example every time when move map and zoom level, the url dynamically updates .
http://www.xxxxxxxx.net/map#lat=-25.87899&lng=-42.45117&zoom=4
My code :
getData(){
////Here should pass LatLngBounds in url ////
this.http.get('xxxxxxx/MetarJSON.php?bbox='','','',''' ,{},{}).then(data=>{
let DataJson = JSON.parse(data.data)
for (let datas of DataJson['features']) {
this.points.push({ lng: datas.geometry.coordinates[0], lat: datas.geometry.coordinates[1] });
}
for (let i = 0; i < DataJson.features.length; i++) {
let coords = DataJson.features[i].geometry.coordinates;
this.latLng = new LatLng(coords[1], coords[0]);
this.icoa = DataJson.features[i].properties.id
this.rawOb = DataJson.features[i].properties.rawOb
let fltcat = DataJson.features[i].properties.fltcat
let marker: Marker = this.map.addMarkerSync({
position: this.latLng,
title:fltcat,
icon: icons
})
}
})
}
loadMap() {
let mapOptions: GoogleMapOptions = {
camera: {
target: {
lat: 33.263,
lng: 44.232
},
zoom: 6,
}
};
this.map = GoogleMaps.create('map_canvas', mapOptions);
//// Here l get LatLngBounds but when l move map the lat and lng aren't update
let chec1= this.map.getVisibleRegion().farLeft.lat
let chec2= this.map.getVisibleRegion().farLeft.lng
console.log(chec1,chec2)
}
Ionic Google map native plugin Doc
l found solution while move map to update LatLngBounds . used GoogleMapsEvent constants
depending on doc , and he is working fine
this.map.on(GoogleMapsEvent.CAMERA_MOVE).subscribe(()=>{
let left= this.map.getVisibleRegion().northeast.lat
let leftbottom= this.map.getVisibleRegion().northeast.lng
let right= this.map.getVisibleRegion().southwest.lat
let rightbottom= this.map.getVisibleRegion().southwest.lng
console.log(left,leftbottom , right ,rightbottom )
})
I am following the Josh morony's tutorial and
trying to get the bounds information of google map.
But the outcome of getBounds() is "undefined"
How can I get the value of getBounds()?
My developing environment is
"Ionic2 (2.0.0-rc.4) + typescript"
And my code is like below
initMap(): Promise<any> {
this.mapInitialised = true;
return new Promise((resolve) => {
let latLng = new google.maps.LatLng(37.481223, 126.952701);
let mapOptions = {
center: latLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
this.map = new google.maps.Map(this.mapElement, mapOptions);
let bounds = this.map.getBounds();
console.log(bounds); <-- output "undefined"
resolve(true);
});
}
Thanks in advance ! :)
Try this one :)
this.map.getBounds().getSouthWest().lat(),
this.map.getBounds().getNorthEast().lat(),
this.map.getBounds().getSouthWest().lng(),
this.map.getBounds().getNorthEast().lng()
Ensure that your map is initialized and tiles loaded by listening for the tilesloaded event. This code example would output Latitude and Longitude values for and map bounds change such as panning or zooming.
google.maps.event.addListener(this.map, 'tilesloaded', () => {
console.log(this.map.getBounds().getNorthEast().lat());
console.log(this.map.getBounds().getNorthEast().lng());
console.log(this.map.getBounds().getSouthWest().lat());
console.log(this.map.getBounds().getSouthWest().lng());
});
I have this set of codes and it works, however everytime I click on my button to get my current location, it will position my map to center. Is it possible to get my location WHILE not moving my map?
google.maps.event.addDomListener(controlText, 'click', function(self)
{
getLocation(self);
});
function getLocation(self)
{
console.log("in get location", self);
let locationOptions =
{
timeout : 60000,
enableHighAccuracy : true
};
navigator.geolocation.getCurrentPosition
(
(position) =>
{
self.curLat = position.coords.latitude;
self.curLong = position.coords.longitude;
console.log("updated position coord:"+self.curLat+","+self.curLong);
self.local = new Storage(LocalStorage);
self.local.set("currLong", self.curLong);
self.local.set("currLat", self.curLat);
map.setCenter(new google.maps.LatLng(self.curLat, self.curLong));
markers = removeMarker(markers);
addMarker(map, markers);
},
(error) => {console.log(error);}, locationOptions
);
};
myLocationMarker = new google.maps.Marker
({
map : map,
icon : image,
animation : google.maps.Animation.DROP,
position : map.getCenter(),
id : 'myLocationMarker'
});
markers.push(myLocationMarker);
I have solve my issue by changing
map.setCenter(new google.maps.LatLng(self.curLat, self.curLong));
to
var latLng = new google.maps.LatLng(self.curLat, self.curLong);
and passing the data to addMarker to use it for position.
Remove this line:
map.setCenter(new google.maps.LatLng(self.curLat, self.curLong));
So am I am using gmaps.js and the gmaps.js along with marker clusterer. In my case I may have multiple markers with the exact same location, but in reality represent different data. To overcome this I am trying to implement the solution found here on SO - in particular the solution by Nathan Colgate.
The idea is when max zoom has reached on a cluster it will execute the multiChoice function. I have this part working. What I cannot get to work is showing an infoWindow with that function.
My goal is to show an infoWindow on this cluster click to display information about each marker (particularly each marker's infoWindow content (this will have additional details specific to it).
JS :
//create the map
var map = new GMaps({
el: '#map_canvas_main',
lat: response.results[0].lat,
lng: response.results[0].lng,
zoom: 5,
maxZoom: 15,
panControl: false,
markerClusterer: function(map) {
markerCluster = new MarkerClusterer(map, [], {
title: 'Location Cluster',
maxZoom: 15
});
// onClick OVERRIDE
markerCluster.onClick = function(clickedClusterIcon) {
return multiChoice(clickedClusterIcon.cluster_);
}
return markerCluster;
}
});
//loop through array
for(var i = 0; i < response.results.length; i++)
{
//create marker image
var markerLoc = {
url: '/custom/plugins/gmaps/images/marker-red.png',
size: new google.maps.Size(24, 30), //size
origin: new google.maps.Point(0, 0), //origin point
anchor: google.maps.Point(9, 30) // offset point
};
//add marker
map.addMarker({
lat: response.results[i].lat,
lng: response.results[i].lng,
icon: markerLoc,
title: response.results[i].ip_address,
infoWindow: {
content: '<p>'+response.results[i].ip_address+'</p>'
//add more details later
}
});
}
//cluster function to do stuff
function multiChoice(clickedCluster)
{
//clusters markers
var markers = clickedCluster.getMarkers();
//console check
console.log(clickedCluster);
console.log(markers);
if (markers.length > 1)
{
//content of info window
var infowindow = new google.maps.InfoWindow({
content: ''+
'<p>'+markers.length+' = length</p>'+
'<p>testing blah blah</p>'
});
//show the window
//infowindow.open(??????????);
return false;
}
return true;
};
Finally figured this out playing around with it some more... it makes sense now, but didn't before. Here is the new 'display' function to be replaced by the one in the OP. Of course, there are a few other change needed yet... showing all clustered marker data in the new info window for example, but this is the gist of getting the window to work.
//cluster function to do stuff
function multiChoice(clickedCluster)
{
//clusters markers
var markers = clickedCluster.getMarkers();
if (markers.length > 1)
{
//create the info window
var infoWindow = new google.maps.InfoWindow({
content: ''+
'<p>'+markers.length+' = length</p>'+
'<p>testing blah blah</p>',
position: clickedCluster.center_
});
//display the infowindow
infoWindow.open(clickedCluster.map_);
return false;
}
return true;
};
I'm learning sencha touch and trying put markers on the google map. I've looked at most of the examples online but I'm unable to get the marker and the infoWindow to work. Below is my code:
myApp.views.MapCard = Ext.extend(Ext.Panel, {
id: "mapcard",
layout: 'card',
initComponent: function() {
this.map = new Ext.Map({
useCurrentLocation: true,
mapOptions: {
zoom: 12,
},
});
this.panel = new Ext.Panel({
layout: 'fit',
items: this.map,
});
this.items = this.panel;
myApp.views.MapCard.superclass.initComponent.call(this);
refresh = function(theMap) {
var geoTag = {
lat: '47.584863',
longi: '-122.147026',
text: 'Hello World',
}
addMarker(geoTag, theMap);
}
addMarker = function(geoTag, theMap) {
var latLng = new google.maps.LatLng(geoTag.lat, geoTag.longi);
var marker = new google.maps.Marker({
map: theMap.map,
position: latLng
});
google.maps.event.addListener(marker, "click", function() {
geoTagBubble.setContent(tweet.text);
geoTagBubble.open(theMap.map, marker);
});
};
geoTagBubble = new google.maps.InfoWindow();
refresh(this.map);
},
});
Ext.reg('mapcard', myApp.views.MapCard);
I'm also unable to get the current location of the user, I'm pretty sure that the map is not loaded during the initComponent. I would be calling a json service to pull the lat/lon and loop through later. If there is a better implementation, please let me know!
Thanks a ton!
Here's a pretty minimal example application that demonstrates this:
http://septa.mobi
You can find the source code under view-source or on GitHub:
https://github.com/mjumbewu/Septnuts/
Here's panel where marker are added to a map:
https://github.com/mjumbewu/Septnuts/blob/master/src/Septnuts/MapPanel.js
WARNING: there is a bug in Sencha Touch 1.x that prevents embedded Google Maps from receiving ANY click events
You need to include this patch after including sencha-touch.js in order for any click listener on the map to work:
https://github.com/mjumbewu/Septnuts/blob/master/src/sencha-maptouch-patch.js