maps displayed wrong on mobile (using JQuery mobile) - google-maps

I'm using jQuery mobile and I have to display some maps.
I'm using a function that create maps every time that I click on the specific link but after the generation of the first map, the others are displayed wrong.
Here an example,
First map:
Other maps:
I use a function like this:
function buildMap(div){
var coord = new google.maps.LatLng(lat, lng);
var options = {
zoom: 13,
scrollwheel: false,
scaleControl: true,
mapTypeControl: false,
center: coord,
mapTypeId: google.maps.MapTypeId.HYBRID
};
var map = new google.maps.Map(div, options);
return map;
}
Can you help me?
Thank you!!

Problem solved, you need to create the map on the "pageshow" event (an event of jQuery mobile):
$('#map_page').live('pageshow',function(event, ui){
buildResultMap('#map_canvas');
});

The solution is to trigger the resize event OR not to use Ajax. Phill has guided you to an example how it's done with the jQuery Mobile Google maps plugin which uses Ajax (and calls $('#map_canvas').gmap('refresh'); on pageload. The plugin is very easy to use (especially if you already know Google maps syntax).

Related

GoogleMap with a lot of different places and restricted zoom

I have to put a Map from Google (Google Maps or GoogleMyMaps) in a website. I need the map to show a lot of different places all over my country (about 200 places). I already have a GoogleMyMaps with all the places.
But I need the map to have a zoom limitation/restriction : I want people NOT TO KNOW exactly where the places are, which means the zoom must stop before it gets accurate enough.
I know you can control the zoom with GoogleMaps JS API with something like
var opt = { minZoom: 6, maxZoom: 9 };
map.setOptions(opt);
But I dont know how to add places on a Google Maps (still I have them all on a GoogleMyMaps).
Thank you for your time
Thank you everyone for your help.
So most of the time, GoogleMyMaps uses .KML files (as import or export), and it is possible to upload the data from a .KML file into Google Maps.
Here is an example :
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: {lat: 1, lng: 1}
});
var kmlLayer = new google.maps.KmlLayer({
url: 'THE_KML_FILE_URL',
suppressInfoWindows: true,
map: map
});
kmlLayer.addListener('click', function(kmlEvent) {
var text = kmlEvent.featureData.description;
showInContentWindow(text);
});
function showInContentWindow(text) {
var sidediv = document.getElementById('content-window');
sidediv.innerHTML = text;
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY_HERE&callback=initMap"
async defer></script>
But note that the KML file has to be online, it does not work on local (Google's API needs to have access to your file).
Then you can customize your Google Map using Google Map Javascript API (https://developers.google.com/maps/documentation/javascript/tutorial).
Thank you everyone for your time

Google Map not displaying on website. Controls show, but the map itself does not render

The Google map on my site no longer shows up. I can see the zoom in/out slider, map/satellite buttons, etc... but the map itself no longer shows up. I read somewhere that the map should be in a div tag labeled "map" with both height and width stated in the CSS. The map has always had height/width CSS properties listed, but the ID was labeled differently. I changed the ID to map and corresponding CSS, but the map still will not show up. I read elsewhere that the Google Analytics by Yoast plugin was also causing similar errors. I have disabled all plugins and then one at a time I have reactivated them to see if another plugin might be the issue. However, it seems that none of the plugins running have any effect on the map displaying.
I am a web designer with some HTML/CSS knowledge. I designed this site and had hired a guy to code it into a custom Wordpress theme for me. Like I said, the map had been showing just fine, but no longer does and I have no idea why. I could really use some help to figure this out. I'm trying to learn more about coding so in the future I can debug and fix things like this on my own. A little help would be greatly appreciated!
Here is a link to the page with the map: http://seegerlaw.com/contact-us/
Please let me know if you need any other information from me.
You're using undocumented fields:
function initialize() {
geocoder = new google.maps.Geocoder();
codeAddr(function(lat, lng) {
myLatlng = new google.maps.LatLng(lat,lng);
var mapOptions = {
zoom: 15,
center: myLatlng,
disableDefaultUI: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map
});
});
}
function codeAddr(callback) {
if (geocoder) {
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results[0].geometry);
maps_lat = results[0].geometry.location["kb"];
maps_lng = results[0].geometry.location["lb"];
callback(maps_lat, maps_lng);
}
else {
console.log("Geocoding failed: " + status);
}
});
}
}
Use the lat() and lng() properties of LatLng to get their values.
Even better, just pass in the LatLng directly from the Geocoder to the MapOptions object:
codeAddr(function(latLng) {
var mapOptions = {
center: latLng,
...
};
...
});
callback(results[0].geometry.location);
Even better – it appears that you always geocode the same address – just hardcode the LatLng into your code.

Google Maps API V3 - Messy Controllers

I'm trying to run the first example in the tutorial, and it works fine, except that my controller (zoom, etc..) are all messy.
I'm using V3.
Please take a look in the image:
The code I'm using is:
<div id="map_canvas" style="width:800px; height:600px;"></div>
<script type="text/javascript">
jQuery(function() {
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
});
</script>
Thoughts?
Thanks
Solution:
If this ever happens to you, take a look at your css.
In my case, I was adding some padding to the divs, and it also affected divs inside google maps area.
first of all check on another browser (if you didn't yet). If other browser displays it right try to set position of UI by yourself like here
If problem still exist nothing more is comming to my head.
Mariusz

Reload/Refresh Google map

I want to reload google map with new markers when the user select an option from the select field.Please help me how to refresh/reload google map with new markers.
Right now the store is getting reloaded from the change option the the select field. But the markers are not getting refreshed and are still showing the old results.
I'm trying :
mapToRefresh = Ext.getCmp("mapaSearch");
mapToRefresh.update(mapPositions[0]);//mapPosition is the new lat,long
mapToRefresh.rendered = false;
mapToRefresh.render();
But it's not working.i'm getting this error:
Uncaught TypeError: Cannot read property 'ownerDocument' of null
Thanks
I had the same issue with the map. It would be much more simpler and efficient to clear the markers and update the map with the new markers on select change.
If you need to render the map the best way to achieve this is to destroy the map and recreate it again on select change.
Hope this helps.
This problem occurs when the site is connected to two google maps.
And on another page of one of the map is not available. You must add it and hide...
Example:
var mapOptions = {
zoom: 17,
center: new google.maps.LatLng(59.938863, 30.311556),
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP,'map_style']
}
};
var mapOptionsNew = {
zoom: 17,
center: new google.maps.LatLng(59.939119, 30.317952),
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP,'map_style']
}
};
map = new google.maps.Map(document.getElementById('id-map'),apOptions);
mapNew = new google.maps.Map(document.getElementById('id-map1'),mapOptionsNew);

Creating a Interface for Insering Google Maps

I am trying to create a site that has option to add maps to the site using google maps api. I found hundreds of tutorial and blog posts on embeding map in the site. But what I actually want is a way to give the user choose a place on the map and add marker. Then get the co-ordinates of the marker and store it in the database of retrieving and showing in the front-end. I have seen this option given wordpress plugins like mappress. But now I'm trying to achieve this in codeigniter. I can't find any thing to start on. Can any one point out some one tell me where to start?
This is really easy the 3rd version of the Google maps API. Versions before that where much more complicated, and most of the tutorials floating around are for those versions. Have a look through the API documentation and examples.
Adding a marker is as simple as:
var marker=new google.maps.Marker({/* ... see API */});
Adding an event (eg click) to a marker is as simple as:
var marker_click=new google.maps.event.addListener(
marker,
'click',
function() {/*...*/});
Sounds like you'd want a click event for the map, which you'd translate into a latlong, then (a) generate a marker on the map with JS, and (b) post that back to your server using AJAX, or by storing the values in a hidden form field to be submitted after.
Update:
Mostly from the API documentation # http://code.google.com/apis/maps/documentation/javascript/events.html:
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var myOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
}
function placeMarker(location) {
var clickedLocation = new google.maps.LatLng(location);
var marker = new google.maps.Marker({
position: location,
map: map
});
map.setCenter(location);
/*Do your processing here:
* eg. ajax.post('addMarkerDB&lat='+location.lat+'&long='+location.long);
*/
}
Note: There is no ajax.post function by default, but there could be :)