jedwan
Forum Replies Created
-
AuthorPosts
-
Yes, I imported locations without listings. Please see my private answer above.
This reply has been marked as private.2.7 locations. I added few indexes on these tables. I didn’t see any improvement. Can you please detail what table exactly needs to be indexed. I think joint queries are the problem. I just don’t know what tables to index.
Back to the original question on how slow Geodirectory location manager on a large database.
The main reason for this is the structure of the database, any query has to sort through 2.7 million entries in my case. Splitting that table into 3 indexed tables for country, region, and city, will reduce the query load on location table. Here is a proposed table structure for location table, a similar approach can be implemented for place details tables.Countries table
Column Type Description
id SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT Primary Key for a Country
name VARCHAR(255) NOT NULL Country name
code VARCHAR(10) NOT NULL Country abbreviationRegions table (regions/provinces/states)
Column Type Description
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT Primary Key for a Region
name VARCHAR(255) NOT NULL Region name
code VARCHAR(10) NOT NULL For USA: State/Territory abbreviation
country_id SMALLINT(5) UNSIGNED NOT NULL Foreign key to Countries tableCities table
Column Type Description
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT Primary Key for a City
name VARCHAR(255) NOT NULL City name
region_id INT(11) UNSIGNED NOT NULL Foreign key to Regions table (States for USA)
country_id SMALLINT(5) UNSIGNED NOT NULL Foreign key to Countries table
latitude DECIMAL(10,8) NOT NULL City Latitude
longitude DECIMAL(11,8) NOT NULL City LongitudeNo worries, I figured it out. Here is my code if anyone needs its.
<?php /** * Plugin Name: Test Widget Plugin * Plugin URI: https://test.com * Description: Adds an example widget that displays the site title and tagline in a widget area. * Version: 1.0 * Author: test * Author URI: https://www.test.com */ class jpen_Example_Widget extends WP_Widget { // Set up the widget name and description. public function __construct() { $widget_options = array( 'classname' => 'example_widget', 'description' => 'This is an Example Widget' ); parent::__construct( 'example_widget', 'Example Widget', $widget_options ); } // Create the widget output. public function widget( $args, $instance ) { $title = apply_filters( 'widget_title', $instance[ 'title' ] ); $blog_title = get_bloginfo( 'name' ); $tagline = get_bloginfo( 'description' ); echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?> <p><strong>Site Name:</strong> <?php echo $blog_title ?></p> <p><strong>Tagline:</strong> <?php echo $tagline ?></p> <?php global $gd_session; function _gd_custom_address_lat_lng( $country, $region = '', $city = '', $street = '' ) { $lat = ''; $lng = ''; $address = array(); if ( ! empty( $street ) ) { $address[] = $street; } if ( ! empty( $city ) ) { $city = geodir_ucwords( preg_replace( '/[_-]/', ' ', preg_replace( '/-(\d+)$/', '', $city ) ) ); $address[] = $city; } if ( ! empty( $region ) ) { $region = geodir_ucwords( preg_replace( '/[_-]/', ' ', preg_replace( '/-(\d+)$/', '', $region ) ) ); $address[] = $region; } if ( ! empty( $country ) ) { $country = geodir_ucwords( preg_replace( '/[_-]/', ' ', preg_replace( '/-(\d+)$/', '', $country ) ) ); $address[] = $country; } if ( ! empty( $address ) ) { $url = 'https://maps.googleapis.com/maps/api/geocode/json?sensor=false&key=' . geodir_get_map_api_key() . '&address=' . urlencode(implode(',', $address)); $url = 'https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=' . urlencode(implode(',', $address)); $response = wp_remote_get( $url , array( 'timeout' => 120, 'httpversion' => '1.1' ) ); $response_code = wp_remote_retrieve_response_code( $response ); $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( ! is_wp_error( $response ) && 200 == $response_code && $response_body['status'] == 'OK' ) { $latitude = $response_body['results'][0]['geometry']['location']['lat']; $lng = $response_body['results'][0]['geometry']['location']['lng']; } } $return = array( 'latitude' => $latitude, 'longitude' => $longitude, 'country' => $country, 'region' => $region, 'city' => $city, ); return $return; } $location = array(); if ( ! empty( $gd_session ) && (( $gd_country = $gd_session->get( 'gd_country' ) ) || ( $gd_region = $gd_session->get( 'gd_region' ) ) || ( $gd_city = $gd_session->get( 'gd_city' ) )) && $gd_session->get( 'gd_multi_location' ) ) { $lat_lng = _gd_custom_address_lat_lng( $gd_session->get( 'gd_country' ), $gd_session->get( 'gd_region' ), $gd_session->get( 'gd_city' ) ); $latitude = $lat_lng['latitude']; $longitude = $lat_lng['longitude']; $country = $lat_lng['country']; $region = $lat_lng['region']; $city = $lat_lng['city']; global $wpdb; $location = $wpdb->get_row( $wpdb->prepare( "SELECT country, region, city, city_latitude, city_longitude FROM " . POST_LOCATION_TABLE . " WHERE country_slug = %s AND region_slug = %s AND city_slug = %s", array( $gd_country, $gd_region, $gd_city ) ) ); } if ( ! empty( $location ) ) { $latitude = $location->city_latitude; $longitude = $location->city_longitude; $country = $location->country; $region = $location->region; $city = $location->city; /** if ( ! empty( $gd_session ) && ( ( $gd_country = $gd_session->get( 'gd_country' ) ) || ( $gd_region = $gd_session->get( 'gd_region' ) ) || ( $gd_city = $gd_session->get( 'gd_city' ) ) ) && $gd_session->get( 'gd_multi_location' ) ) { $lat_lng = _gd_custom_address_lat_lng( $gd_session->get( 'gd_country' ), $gd_session->get( 'gd_region' ), $gd_session->get( 'gd_city' ) ); $latitude = $lat_lng['latitude']; $longitude = $lat_lng['longitude']; $country = $lat_lng['country']; $region = $lat_lng['region']; $city = $lat_lng['city']; */ } else { /////////////////////////////////////// ////////////////////////// //////////////////////// if (is_front_page()) { // this code will run // what if this was code to take action when $mystring is undefined? // // // // $remoteIP = $_SERVER['REMOTE_ADDR']; function url_get_contents($url, $useragent='cURL', $headers=false, $follow_redirects=true, $debug=false) { // initialise the CURL library $ch = curl_init(); // specify the URL to be retrieved curl_setopt($ch, CURLOPT_URL,$url); // we want to get the contents of the URL and store it in a variable curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // specify the useragent: this is a d courtesy to site owners curl_setopt($ch, CURLOPT_USERAGENT, $useragent); // ignore SSL errors curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // return headers as requested if ($headers==true){ curl_setopt($ch, CURLOPT_HEADER,1); } // only return headers if ($headers=='headers only') { curl_setopt($ch, CURLOPT_NOBODY ,1); } // follow redirects - note this is disabled by default in most PHP installs from 4.4.4 up if ($follow_redirects==true) { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); } // if debugging, return an array with CURL's debug info and the URL contents if ($debug==true) { $result['contents']=curl_exec($ch); $result['info']=curl_getinfo($ch); } // otherwise just return the contents as a variable else $result=curl_exec($ch); // free resources curl_close($ch); // send back the data return $result; } $location2 = url_get_contents( "https://freegeoip.net/json/$remoteIP" ); //Secondary Service $location2 = json_decode( $location2 ); $location = new stdClass(); $location->country_name = $location2->country_name; $location->country_code = $location2->country_code; $location->city = $location2->city; $location->state = $location2->region_name; $location->latitude = $location2->latitude; $location->longitude = $location2->longitude; $latitude = $location->longitude; $longitude = $location->longitude; $country = $location->country_name; $region = $location->state; $city = $location->city; // // // // } /////////////////////////////////////////////// } echo '<br>latitude : ' . $latitude; echo '<br>longitude : ' . $longitude; echo '<br>country : ' . $country; echo '<br>region : ' . $region; echo '<br>city : ' . $city; ?> <?php echo $args['after_widget']; } // Create the admin area widget settings form. public function form( $instance ) { $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; ?> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label> <input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" /> </p><?php } // Apply settings to the widget instance. public function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] ); return $instance; } } // Register the widget. function jpen_register_example_widget() { register_widget( 'jpen_Example_Widget' ); } add_action( 'widgets_init', 'jpen_register_example_widget' ); ?>
Thank you Kiran for your help, the code is what I want to do. However, I this code causes excessive use of google maps api which is limited to 2500 calls per day.
Can I use a combination of the snippets that you provided to call latitude and longitude info from my database at the city level, and make requesting region and country level info from google maps.
something like this but I can’t make the logical operators to work correctly.global $gd_session; function _gd_custom_address_lat_lng( $country, $region = '', $city = '', $street = '' ) { $latitude = ''; $longitude = ''; $address = array(); if ( ! empty( $street ) ) { $address[] = $street; } if ( ! empty( $city ) ) { $city = geodir_ucwords( preg_replace( '/[_-]/', ' ', preg_replace( '/-(\d+)$/', '', $city ) ) ); $address[] = $city; } if ( ! empty( $region ) ) { $region = geodir_ucwords( preg_replace( '/[_-]/', ' ', preg_replace( '/-(\d+)$/', '', $region ) ) ); $address[] = $region; } if ( ! empty( $country ) ) { $country = geodir_ucwords( preg_replace( '/[_-]/', ' ', preg_replace( '/-(\d+)$/', '', $country ) ) ); $address[] = $country; } if ( ! empty( $address ) ) { $url = 'https://maps.googleapis.com/maps/api/geocode/json?sensor=false&key=' . geodir_get_map_api_key() . '&address=' . urlencode(implode(',', $address)); $url = 'https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=' . urlencode(implode(',', $address)); $response = wp_remote_get( $url , array( 'timeout' => 120, 'httpversion' => '1.1' ) ); $response_code = wp_remote_retrieve_response_code( $response ); $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( ! is_wp_error( $response ) && 200 == $response_code && $response_body['status'] == 'OK' ) { $latitude = $response_body['results'][0]['geometry']['location']['lat']; $longitude = $response_body['results'][0]['geometry']['location']['lng']; } } $return = array( 'latitude' => $latitude, 'longitude' => $longitude, 'country' => $country, 'region' => $region, 'city' => $city, ); return $return; } $location = array(); if ( ! empty( $gd_session ) && ( $gd_country = $gd_session->get( 'gd_country' ) ) && ( $gd_region = $gd_session->get( 'gd_region' ) ) && ( $gd_city = $gd_session->get( 'gd_city' ) ) && $gd_session->get( 'gd_multi_location' ) ) { global $wpdb; $location = $wpdb->get_row( $wpdb->prepare( "SELECT country, region, city, city_latitude, city_longitude FROM " . POST_LOCATION_TABLE . " WHERE country_slug = %s AND region_slug = %s AND city_slug = %s", array( $gd_country, $gd_region, $gd_city ) ) ); $lat_lng = _gd_custom_address_lat_lng( $gd_session->get( 'gd_country' ), $gd_session->get( 'gd_region' ), $gd_session->get( 'gd_city' ) ); $latitude = $lat_lng['latitude']; $longitude = $lat_lng['longitude']; $country = $lat_lng['country']; $region = $lat_lng['region']; $city = $lat_lng['city']; } if ( ! empty( $location ) ) { $latitude = $location->city_latitude; $longitude = $location->city_longitude; $country = $location->country; $region = $location->region; $city = $location->city; } else { $latitude = ''; $longitude = ''; $country = ''; $region = ''; $city = ''; } echo '<br>latitude : ' . $latitude; echo '<br>longitude : ' . $longitude; echo '<br>country : ' . $country; echo '<br>region : ' . $region; echo '<br>city : ' . $city;
This reply has been marked as private.Thank you very much for the code snippet. I’m wondering if there anyway to also retrieve lat and long values at the state and country level. I know region and country don’t have lat and long values but is there anyway to get a lat and long values that are center of the region or country.
I want to retrieve lat and lng for this page https://try.esalah.com/location/united-states/md/laurel/
thank you.
Thank you. I will wait for V2. I’d be more than happy if you add me to your beta testing.
I’m trying to pass latitude and longitude values to my plugin.
I was able to do that for listings, I’m having difficult time figuring out how location page is created to retrieve to lat and long values location page.
Here is my code for listing page the else statement is for the location page needs to be completed.// find lat and long on listing page
if ( is_single() ) {
global $post;
if ( isset( $post->post_latitude ) ) {
$lat = $post->post_latitude;
$lng = $post->post_longitude;
$pcountry = $post->post_country;
$pregion = $post->post_region;
$pcity = $post->post_city;
$paddress = $post->post_address;
}
// find lat and long on location page
} else {
$lat = ;
$lng = ;
$pcountry = ;
$pregion = ;
$pcity = ;}
This reply has been marked as private.I imported that data according to this structure into geodir_location table via mysql statements
location_id
,
country
,
region
,
city
,
country_slug
,
region_slug
,
city_slug
,
city_latitude
,
city_longitude
,
is_default
,
city_meta
,
city_desc
and this structure into location_seo table.
seo_id
,
location_type
,
country_slug
,
region_slug
,
city_slug
,
seo_meta_title
,
seo_meta_desc
,
seo_title
,
seo_desc
,
seo_image
,
seo_image_tagline
,
date_created
,
date_updated
I believe these are the only two tables that are created by location manager.
I created a fresh wordpress installation with only geodirectory and location manager plugins activated.
activation of location manager basically kills the website, location manager plugin becomes inaccessible from the back-end. Places page in the back-end takes around 5 minutes to load.basically the location manager plugin can’t handle 2 million lines of data.
OK. In Location Manager>Location Settings>Enabled selected Cities. That page is trying to load all the cities that I have. My browser can’t handle 2 million lines of select menu.
That function needs to be ajaxed to prevent such a problem.This is the specs of my server 64G E5-1650 SoftRaid 3×2 TB Server
Can you please let me know if I can use mysql dump directly in the location table or I need to update the table via the import&export csv?
The number of entries that I can import in one CSV files is limited to 50000. If I try to import more than 50000 cities from one CSV file I get red bar displaying http error.
-
AuthorPosts