WordPress.org

Ready to get started?Download WordPress

Forums

Geo Mashup
Center map on user (custom.js not working) (10 posts)

  1. mistercyril
    Member
    Posted 3 years ago #

    Hello Dylan,

    I'd like my map to render locations in alphabetical order, so centering the map on last published item is not ideal.

    I would like the map to be centered on user location but I can't get your custom.js method to work. I picked up the code you suggest in a number of posts:

    GeoMashup.addAction( 'loadedMap', function( properties, map ) {
      if ( google.loader.ClientLocation ) {
        map.setCenter( new google.maps.LatLng(
    google.loader.ClientLocation.latitude,
    google.loader.ClientLocation.longitude ) );
      }
    } );

    But it doesn't seem to make a difference.

    Could you help me out with this. I think quite a few people would like to be able to implement this because it makes Geotaging way more useful for visitors.

    Thank you,

    Cyril

  2. Dylan Kuhn
    Member
    Plugin Author

    Posted 3 years ago #

    Google is now recommending a different method for getting user location that prompts the user for permission. Here's some custom.js code adapted from their example:

    /*
    * Center maps on user location when available.
    */
    GeoMashup.addAction( 'loadedMap', function( properties, map ) {
    	var initialLocation;
    	var siberia = new google.maps.LatLng(60, 105);
    	var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
    	var browserSupportFlag =  new Boolean();
    
    	if ( 'global' != properties.map_content ) {
    		// Don'ts center non-global maps on user
    		return;
    	}
    
    	// Try W3C Geolocation (Preferred)
    	if(navigator.geolocation) {
    		browserSupportFlag = true;
    		navigator.geolocation.getCurrentPosition(function(position) {
    			initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
    			map.setCenter(initialLocation, 11 );
    		}, function() {
    			handleNoGeolocation(browserSupportFlag);
    		});
    	// Try Google Gears Geolocation
    	} else if (google.gears) {
    		browserSupportFlag = true;
    		var geo = google.gears.factory.create('beta.geolocation');
    		geo.getCurrentPosition(function(position) {
    			initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
    			map.setCenter(initialLocation, 11 );
    		}, function() {
    			handleNoGeoLocation(browserSupportFlag);
    		});
    	// Browser doesn't support Geolocation
    	} else {
    		browserSupportFlag = false;
    		handleNoGeolocation(browserSupportFlag);
    	}
    
    	function handleNoGeolocation(errorFlag) {
    		if (errorFlag == true) {
    			initialLocation = newyork;
    		} else {
    			initialLocation = siberia;
    		}
    		map.setCenter(initialLocation);
    	}
    } );
  3. mistercyril
    Member
    Posted 3 years ago #

    Thanks Dylan,

    I ported this to the custom.js file but i'm not getting any kind of prompt. I know the custom.js file is working because I added the "maximize info window" button removal scripts and they worked straight away.

    Thanks,

    Cyril

  4. Dylan Kuhn
    Member
    Plugin Author

    Posted 3 years ago #

    I can't tell what might be wrong without access to the site. I've used that code elsewhere successfully.

    In Firefox at least the prompt is not very eye-catching - a bar at the top of the page - but you would see it if you're looking.

  5. mistercyril
    Member
    Posted 3 years ago #

    Can this be done automatically by using a wider localization range?

    I don't need to center on the user's actual street address. The city would be enough.

    Thanks,
    C.

  6. Dylan Kuhn
    Member
    Plugin Author

    Posted 3 years ago #

    The issue on your site is that you're using a contextual map and the code above is set up for global maps only. Set the map_content parameter to global if you want a global there, or change 'global' to 'contextual' in the code if not.

    An extra step could be added to look up the nearest city center to the user location, and center there. PM me again if you'd like me to implement that for you.

  7. mistercyril
    Member
    Posted 3 years ago #

    Hello Dylan,

    You're right! I added the map_content parameter and set it to global and the location tracking prompt appeared straight away.

    That next step sounds pretty cool, but i'm not sure that is the most efficient solution for my use. I don't have thousands of entries so more often than not, the map will center on a city with no posts.

    So I wonder if we could have it center on the closest city that does have entries. Do you think thats possible?

  8. Dylan Kuhn
    Member
    Plugin Author

    Posted 3 years ago #

    Sure - just a few more steps needed. We'd get the user location, find the nearest entry, look up the city there, and center on that.

  9. niteroi
    Member
    Posted 3 years ago #

    To center the GeoMashup Global Map on the user location:
    1) Install de PHP-Exec plugin (there are some basic settings to adjust in WP).
    2) In the content editor of your Global Map page, put the following code:
    <?php $data = array(); $data = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])); $latitude = $data['geoplugin_latitude']; $longitude = $data['geoplugin_longitude']; ?>
    [geo_mashup_map_context="global" center_lat=<?php echo $latitude; ?> center_lng=<?php echo $longitude; ?>]

    No need of Custom plugin, no need of browser toolbar, etc.

  10. Dylan Kuhn
    Member
    Plugin Author

    Posted 3 years ago #

    No need for PHP-Exec if you use a template - that's what I recommend.

    Glad to hear about geoplugin.net. Seems like you might just get a nearby city as is common for IP locators, but the terms look good.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic