IONIC 3 google map is ready but nothing showing in the map - google-maps

I am new on ionic and integrating google map one of my app. I have everything installing google map plugin using apikey and writing some code here it is:
#ViewChild('map') mapElement: ElementRef;
map: GoogleMap;
spaArray:any = []
setupLocation(){
this.location = new LatLng(42.346903, -71.135101);
//Add cluster locations
this.locations.push({position: {lat: 42.346903, lng: -71.135101}});
this.locations.push({position: {lat: 42.342525, lng: -71.145943}});
this.locations.push({position: {lat: 42.345792, lng: -71.138167}});
this.locations.push({position: {lat: 42.320684, lng: -71.182951}});
this.locations.push({position: {lat: 42.359076, lng: -71.0645484}});
this.locations.push({position: {lat: 42.36, lng: -71.1}});
}
ionViewDidLoad() {
this.provider.presentLoadingDefault()
this.platform.ready().then(() => {
// let element = this.mapElement.nativeElement;
let element: HTMLElement = document.getElementById(this.mapElement.nativeElement);
this.map = this.googleMaps.create(this.mapElement.nativeElement);
// this.map.clear();
this.map.one(GoogleMapsEvent.MAP_READY).then(() => {
this.provider.hideLoader()
let options = {
target: this.location,
zoom: 8
};
this.map.moveCamera(options);
setTimeout(() => {this.addCluster()}, 500);
});
});
}
html is here
<ion-content>
<div #map style="height: 100%" id="map"></div>
</ion-content>
but when I am running the app into my mobile its only showing blank map with google logo bottom of this.
Help me into this please.

Here you have a working example:
import {
GoogleMaps,
GoogleMap,
GoogleMapsEvent,
GoogleMapOptions,
CameraPosition,
MarkerOptions,
Marker,
MarkerCluster,
LatLng,
HtmlInfoWindow,
MyLocation
} from '#ionic-native/google-maps';
inside your class:
private mapElement: HTMLElement;
private map: GoogleMap;
inside your platform.ready().then()
this.mapElement = document.getElementById('map');
// DEFINE YOUR OWN MAP SETTINGS
let mapOptions: GoogleMapOptions = {
controls: {
indoorPicker: true
},
gestures: {
rotate: true,
scroll: true,
tilt: true,
zoom: true
},
preferences: {
building: true,
zoom: {
maxZoom: 20,
minZoom: 1
}
}
};
this.map = GoogleMaps.create(this.mapElement, mapOptions);
// Wait the MAP_READY before using any methods.
this.map.one(GoogleMapsEvent.MAP_READY)
.then(() => {
// DO MAP STUFF HERE
});
this should work fine.

Related

map.setCenter undefined

I'm using GoogleMaps and want to centre it when the orientation changes, but when I call
map.setCenter({...})
I'm receiving an undefined error for setCenter.
Here's my code:
let GoogleMap = {
lat : "",
lng : "",
map : {},
marker : {},
point : {},
init() {
return this
},
create(lat, lng) {
this.lat = lat
this.lng = lng
this.point = {lat: lat,lng: lng};
this.map = new google.maps.Map(document.getElementById('map'), {
center: this.point,
zoom: 17
});
this.marker = new google.maps.Marker({
map: this.map,
position: this.point
});
window.addEventListener('orientationchange', this.doOnOrientationChange);
},
doOnOrientationChange() {
this.map.setCenter({lat: this.lat, lng: this.lng})
}
}
export default GoogleMap
Docs: https://developers.google.com/maps/documentation/javascript/reference#Map
And I grab the google maps api from: https://maps.googleapis.com/maps/api/js?key=KEY&libraries=geometry,places
Any ideas?
Thanks to geocodezip, the issue was scoping, and this wasn't in the correct scope in the orientation event handler.
The fix was to use the bind method
window.addEventListener('orientationchange', this.orientationChange.bind(this));
Full working code:
let GoogleMap = {
lat : "",
lng : "",
map : {},
marker : {},
point : {},
init() {
return this
},
create(lat, lng) {
this.lat = lat
this.lng = lng
this.point = {lat: lat,lng: lng};
this.map = new google.maps.Map(document.getElementById('map'), {
center: this.point,
zoom: 17
});
this.marker = new google.maps.Marker({
map: this.map,
position: this.point
});
window.addEventListener('orientationchange', this.orientationChange.bind(this));
},
orientationChange() {
this.map.setCenter({lat: this.lat, lng: this.lng})
}
}
export default GoogleMap
The map now centres itself as expected when the orientation changes.

SetPosition: not a LatLng or LatLngLiteral: in property lat: not a number using React and Google Maps

I am trying to display multiple markers using google maps and react but keep getting the error mentioned above. The idea is to have multiple markers displayed from lat and long cords in an array. Any thoughts?
Codepen: http://codepen.io/anon/pen/evOoNP?editors=0010
class GMap extends React.Component {
state = { zoom: 10 };
static propTypes() {
initialCenter: React.PropTypes.objectOf(React.PropTypes.number).isRequired
}
render() {
return <div className="GMap">
<div className='UpdatedText'>
<p>Current Zoom: { this.state.zoom }</p>
</div>
<div className='GMap-canvas' ref="mapCanvas">
</div>
</div>
}
componentDidMount() {
// create the map, marker and infoWindow after the component has
// been rendered because we need to manipulate the DOM for Google =(
this.map = this.createMap()
this.marker = this.createMarker()
this.infoWindow = this.createInfoWindow()
// have to define google maps event listeners here too
// because we can't add listeners on the map until its created
google.maps.event.addListener(this.map, 'zoom_changed', ()=> this.handleZoomChange())
}
// clean up event listeners when component unmounts
componentDidUnMount() {
google.maps.event.clearListeners(map, 'zoom_changed')
}
createMap() {
let mapOptions = {
zoom: this.state.zoom,
center: new google.maps.LatLng(38, -78)
}
return new google.maps.Map(this.refs.mapCanvas, mapOptions)
}
mapCenters() {
return new google.maps.LatLng(
this.props.initialCenter.lat,
this.props.initialCenter.lng
)
}
mapCenter() {
const navLinks = [
{location: "Bondi Beach", lat: -33.890542, long: 151.274856},
{location: "Coogee Beach", lat: -33.923036, long: 151.259052},
{location: "Cronulla Beach", lat: -34.028249, long: 151.157507}
];
return navLinks.map((b, i) => {
console.log(b)
return new google.maps.LatLng(b.lat, b.long)
})
}
createMarker() {
return new google.maps.Marker({
position: this.mapCenter(),
map: this.map
})
}
createInfoWindow() {
let contentString = "<div class='InfoWindow'>I'm a Window that contains Info Yay</div>"
return new google.maps.InfoWindow({
map: this.map,
anchor: this.marker,
content: contentString
})
}
handleZoomChange() {
this.setState({
zoom: this.map.getZoom()
})
}
}
var initialCenter = { lng: -90.1056957, lat: 29.9717272 }
ReactDOM.render(<GMap initialCenter={initialCenter} />, document.getElementById('container'));
new google.maps.Marker({
position: this.mapCenter(), // here is the problem
map: this.map
})
Your mapCenter() method returns an array of coordinates, but new google.maps.Marker({}) expects a single LatLng to be passed as position.
You need to update your createMarker() method to create the markers in a loop, for example:
createMarker() {
return this.mapCenter().map( point => {
return new google.maps.Marker({
position: point,
map: this.map
})
});
}
http://codepen.io/anon/pen/yMLYBa?editors=0010

Display Multiple Markers with Google Maps and React Js ES6

I am able to display one marker but when I map create new google.maps.Marker inside my loop nothing is returned. Has anyone encoutnered this before? Code Below and codepen attached.
Codepen: http://codepen.io/anon/pen/pezqKq?editors=0010
class GMap extends React.Component {
state = { zoom: 10 };
static propTypes() {
initialCenter: React.PropTypes.objectOf(React.PropTypes.number).isRequired
}
render() {
return <div className="GMap">
<div className='UpdatedText'>
<p>Current Zoom: { this.state.zoom }</p>
</div>
<div className='GMap-canvas' ref="mapCanvas">
</div>
</div>
}
componentDidMount() {
// create the map, marker and infoWindow after the component has
// been rendered because we need to manipulate the DOM for Google =(
this.map = this.createMap()
this.marker = this.createMarker()
this.infoWindow = this.createInfoWindow()
// have to define google maps event listeners here too
// because we can't add listeners on the map until its created
google.maps.event.addListener(this.map, 'zoom_changed', ()=> this.handleZoomChange())
}
// clean up event listeners when component unmounts
componentDidUnMount() {
google.maps.event.clearListeners(map, 'zoom_changed')
}
createMap() {
let mapOptions = {
zoom: this.state.zoom,
center: this.mapCenter()
}
return new google.maps.Map(this.refs.mapCanvas, mapOptions)
}
mapCenter() {
return new google.maps.LatLng(
this.props.initialCenter.lat,
this.props.initialCenter.lng
)
}
createMarker() {
const navLinks = [
{location: 'Bondi Beach', lat: -33.890542, long: 151.274856},
{location: 'Coogee Beach', lat: -33.923036, long: 151.259052},
{location: 'Cronulla Beach', lat: -34.028249, long: 151.157507},
{location: 'Manly Beach', lat: -33.80010128657071, long: 151.28747820854187}
];
navLinks.map((b, i) => {
return new google.maps.Marker({
position: new google.maps.LatLng(b.lat, b.long),
map: this.map
})
console.log(b.long)
})
}
createInfoWindow() {
let contentString = "<div class='InfoWindow'>I'm a Window that contains Info Yay</div>"
return new google.maps.InfoWindow({
map: this.map,
anchor: this.marker,
content: contentString
})
}
handleZoomChange() {
this.setState({
zoom: this.map.getZoom()
})
}
}
var initialCenter = { lng: -90.1056957, lat: 29.9717272 }
ReactDOM.render(<GMap initialCenter={initialCenter} />, document.getElementById('container'));
In createMarker you aren't actually returning anything. You need to return the navLinks.map. You also aren't seeing that console.log because you have it below the return statement in the map.
return navLinks.map((b, i) => {
console.log(b.long)
return new google.maps.Marker({
position: new google.maps.LatLng(b.lat, b.long),
map: this.map
})
})

Google maps, changing the map on click - outside the map

I want to change the map (lat and lag) when a div (which is outside the map) is clicked.
I have got it working when the #map div is clicked, however any event outside the map does not get picked up.
This is what I have so far.
var map;
function initMap() {
var mapDiv = document.getElementById('map');
var map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: 52.9722222,
lng: -3.1622222
},
disableDefaultUI: true,
zoom: 17,
mapTypeControlOptions: {
mapTypeIds: ['roadmap', 'satellite', 'hybrid', 'terrain',
'styled_map'
]
}
});
google.maps.event.addDomListener(mapDiv, 'click', function() {
window.alert('Map was clicked!');
});
var image = 'img/mapmarker.png';
//Associate the styled map with the MapTypeId and set it to display.
map.mapTypes.set('styled_map', styledMapType);
map.setMapTypeId('styled_map');
var marker = new google.maps.Marker({
position: {
lat: 52.9722222,
lng: -3.1622222
},
map: map,
icon: image,
});
}
The map variable that is being initialized is local to the initMap function. You need to initialize the global version:
var map;
function initMap() {
var mapDiv = document.getElementById('map');
// remove the var before map in this line:
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 52.9722222, lng: -3.1622222},
proof of concept fiddle
code snippet:
var map;
function initMap() {
var mapDiv = document.getElementById('map');
map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: 52.9722222,
lng: -3.1622222
},
disableDefaultUI: true,
zoom: 17,
mapTypeControlOptions: {
mapTypeIds: ['roadmap', 'satellite', 'hybrid', 'terrain',
'styled_map'
]
}
});
google.maps.event.addDomListener(mapDiv, 'click', function() {
window.alert('Map was clicked!');
});
var marker = new google.maps.Marker({
position: {
lat: 52.9722222,
lng: -3.1622222
},
map: map
});
}
google.maps.event.addDomListener(window, "load", initMap);
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<input type="button" value="Click Me!" onclick="map.setCenter({lat:52.97,lng:-3.16});map.setZoom(15);" />
<div id="map"></div>

Google Maps API - Show multiple marker tool tips

I have a map with markers on it, but I want to be able to show multiple tool tips.
It seems when I do:
marker.openInfoWindowHtml(strToolTip);
...each time it is called, it closes the previous tool tip.
Any ideas how I show multiple marker tool tips on the same map?
Thanks
Have you tried creating a new infowindow object on marker click event and opening it?
var infowindow = new google.maps.InfoWindow({ content: 'Hello world' });
infowindow.open(map, marker);
You can try this:
var markers = [
{ lat: 28.7051, lng: 77.1125 },
{ lat: 28.7081, lng: 77.1075 },
{ lat: 28.7021, lng: 77.1315 }
]
var index=0;
markers.forEach(function (marker) {
var self=this;
(function (marker) {
let mark = new google.maps.Marker({ position: new google.maps.LatLng(marker.lat, marker.lng) });
var infowindow = new google.maps.InfoWindow({ content: index });
infowindow.open(self.map, marker);
mark.setMap(self.map);
index++;
})(marker)
})
Note: The sequence of open() & setMap() must be like above/below code.
Ex:
infowindow.open(self.map, marker);
mark.setMap(self.map);
Snapshot are below:
If you are using angular2/4/5 then have a look on the complete code:
map.component.ts:
import { Component, ViewChild } from '#angular/core';
import { } from '#types/googlemaps'; // You need to install #types/googlemaps, To know how hit this URL- https://medium.com/#svsh227/integrate-google-api-map-in-your-angular-2-4-5-app-472bf08fdac
#Component({
selector: 'map-component',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent {
#ViewChild('map') gmapElement: any;
map: google.maps.Map;
ngOnInit() {
var markers = [
{ lat: 28.4685, lng: 77.0056, toolTip: 'Here too' },
{ lat: 28.4795, lng: 77.0276, toolTip: 'Here too' },
{ lat: 28.4605, lng: 77.0546, toolTip: 'Here too' }
]
// For center
var mapProp = {
center: new google.maps.LatLng(28.4595, 77.0266),
zoom: 13,
mapTypeId: google.maps.MapTypeId.HYBRID // also use ROADMAP,SATELLITE or TERRAIN
};
this.map = new google.maps.Map(this.gmapElement.nativeElement, mapProp);
var marker = new google.maps.Marker({ position: mapProp.center });
marker.setMap(this.map);
var infowindow = new google.maps.InfoWindow({ content: "Hey !! Here we are" });
infowindow.open(this.map, marker);
this.setMultipleMarker(markers, this);
}
setMultipleMarker(markers, self) {
markers.forEach(function (marker) {
(function (marker) {
let mark = new google.maps.Marker({ position: new google.maps.LatLng(marker.lat, marker.lng) });
let infowindow = new google.maps.InfoWindow({ content: marker.toolTip });
infowindow.open(self.map, mark);
mark.setMap(self.map);
})(marker)
})
}
}
map.component.html:
<div>
<br />
<h1>
<span class="heading-text">Here We Are</span>
</h1>
<div class="row" class="card-details rcorners3 card-height">
<div class="card" class="subAbout tech-stack">
<div class="card-header card-header-us">
<div id="map" #map></div>
</div>
</div>
</div>
</div>
And here is the output/Snapshot: