/**
 * This function used to show map on front end side.
 * @author Flipper Code <*****@*****.**>
 * @version 1.0.0
 * @package Maps
 */
function wpgmp_show_location_in_map($atts, $content = null)
{
    ob_start();
    wpgmp_google_map_load();
    global $wpdb;
    extract(shortcode_atts(array('zoom' => get_option('wpgmp_zoomlevel'), 'width' => get_option('wpgmp_mapwidth'), 'height' => get_option('wpgmp_mapheight'), 'title' => 'WP Google Map', 'class' => 'map', 'center_latitude' => get_option('wpgmp_centerlatitude'), 'center_longitude' => get_option('wpgmp_centerlongitude'), 'container_id' => 'map', 'id' => ''), $atts));
    $icon = isset($atts['icon']) ? $atts['icon'] : '';
    include_once dirname(__FILE__) . '/class-google-map.php';
    $map = new Wpgmp_Google_Map();
    $map_data = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "create_map where map_id=%d", $atts['id']));
    $unserialize_map_street_view_setting = unserialize($map_data->map_street_view_setting);
    $unserialize_map_control_setting = unserialize($map_data->map_all_control);
    $unserialize_map_info_window_setting = unserialize($map_data->map_info_window_setting);
    $unserialize_map_layer_setting = unserialize($map_data->map_layer_setting);
    if (!empty($map_data)) {
        $un_loc_add = unserialize($map_data->map_locations);
        $loc_data = $wpdb->get_row($wpdb->prepare("SELECT location_address,location_latitude,location_longitude FROM " . $wpdb->prefix . "map_locations where location_id=%d", $un_loc_add[0]));
        if (!empty($center_latitude)) {
            $map->center_lat = $center_latitude;
        } else {
            $map->center_lat = $loc_data->location_latitude;
        }
        if (!empty($center_longitude)) {
            $map->center_lng = $center_longitude;
        } else {
            $map->center_lng = $loc_data->location_longitude;
        }
        if (!empty($unserialize_map_street_view_setting['street_control'])) {
            $map->street_control = $unserialize_map_street_view_setting['street_control'];
            $map->street_view_close_button = $unserialize_map_street_view_setting['street_view_close_button'];
            $map->links_control = $unserialize_map_street_view_setting['links_control'];
            $map->street_view_pan_control = $unserialize_map_street_view_setting['street_view_pan_control'];
        }
        $map->map_type = $map_data->map_type;
        if (empty($map_data->map_width)) {
            $map->map_width = $width;
        } else {
            $map->map_width = $map_data->map_width;
        }
        if (empty($map_data->map_height)) {
            $map->map_height = $height;
        } else {
            $map->map_height = $map_data->map_height;
        }
        $map->map_scrolling_wheel = $map_data->map_scrolling_wheel;
        $map->map_pan_control = $unserialize_map_control_setting['pan_control'];
        $map->map_zoom_control = $unserialize_map_control_setting['zoom_control'];
        $map->map_type_control = $unserialize_map_control_setting['map_type_control'];
        $map->map_scale_control = $unserialize_map_control_setting['scale_control'];
        $map->map_street_view_control = $unserialize_map_control_setting['street_view_control'];
        $map->map_overview_control = $unserialize_map_control_setting['overview_map_control'];
        $map->visualrefresh = $map_data->map_visual_refresh;
        $map->map_layers = $unserialize_map_layer_setting['choose_layer'];
        if (empty($map_data->map_zoom_level)) {
            $map->zoom = $zoom;
        } else {
            $map->zoom = $map_data->map_zoom_level;
        }
    }
    if (!empty($atts['id'])) {
        $map_locations = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "create_map where map_id=%d", $atts['id']));
        $un_info_window_setting = unserialize($map_locations->map_info_window_setting);
        $map_address = unserialize($map_locations->map_locations);
        if ($map_address != '') {
            $address[] = array();
            foreach ($map_address as $map_ad) {
                $map_locations_records = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "map_locations where location_id=%d", $map_ad));
                $group_marker = $wpdb->get_row($wpdb->prepare("SELECT group_marker FROM " . $wpdb->prefix . "group_map where group_map_id=%d", $map_locations_records->location_group_map));
                $wpgm_marker = get_option('wpgmp_default_marker');
                $unmess_info_message = unserialize(base64_decode($map_locations_records->location_messages));
                $loc_image_src = '';
                if (!empty($group_marker->group_marker)) {
                    $loc_image_src = $group_marker->group_marker;
                } elseif (!empty($map_locations_records->location_marker_image)) {
                    $loc_image_src = $map_locations_records->location_marker_image;
                } elseif (!empty($wpgm_marker)) {
                    $loc_image_src = $wpgm_marker;
                }
                $latitude = $map_locations_records->location_latitude;
                $longitude = $map_locations_records->location_longitude;
                $title = $map_locations_records->location_title;
                $dragg = $map_locations_records->location_draggable;
                $address['first']['message'] = $unmess_info_message['googlemap_infowindow_message_one'];
                $address = array_filter($address);
                if ($address['first']['message'] != '') {
                    $map->addMarker($latitude, $longitude, $un_info_window_setting['info_window'], $title, $address, $loc_image_src, '', $dragg);
                } else {
                    wp_print_scripts('wpgmp_map');
                    $new_loc_adds = array();
                    $new_loc_adds = $map_locations_records->location_address;
                    $address_coordinates = wpgmp_get_address_coordinates($new_loc_adds);
                    $map->addMarker($latitude, $longitude, $un_info_window_setting['info_window'], $title, $new_loc_adds, $loc_image_src, '', $dragg);
                }
            }
        }
    } elseif ($content) {
        wp_print_scripts('wpgmp_map');
        if (empty($zoom) || empty($width) || empty($height) || empty($title)) {
            $map->zoom = 14;
            $map->width = '600';
            $map->height = '400';
            $map->title = 'WP Google Map Plugin';
        } else {
            $map->zoom = $zoom;
            $map->width = $width;
            $map->height = $height;
            $map->title = $title;
        }
        $address = '';
        $coordinates = wpgmp_get_coordinates($content);
        $address = '<h3>' . $coordinates["address"] . '</h3>';
        $address .= '<p>Latitude=' . $coordinates["lat"] . '</p>';
        $address .= '<p>Longitude=' . $coordinates["lng"] . '</p>';
        $map->center_lat = $coordinates['lat'];
        $map->center_lng = $coordinates['lng'];
        $map->addMarker($map->center_lat, $map->center_lng, 'true', $map->title, $address);
        if (!is_array($coordinates)) {
            return;
        }
    } else {
        return "Thank you for using this plugin. Please <a href='" . admin_url('admin.php?page=wpgmp_add_location') . "'>Add your locations</a> or set plugin <a href='" . admin_url('admin.php?page=wpgmp_google_settings') . "'>Settings</a>.";
    }
    echo $map->showmap();
    $content = ob_get_contents();
    ob_clean();
    return $content;
}
function wpgmp_display_map($atts, $content = null)
{
    ob_start();
    global $wpdb;
    include_once dirname(__FILE__) . '/class-google-map.php';
    $map = new Wpgmp_Google_Map();
    $marker_array = array();
    $address_array = array();
    foreach ($atts as $key => $value) {
        if (strpos($key, 'marker') === 0) {
            $marker_array[$key] = $value;
            $first_marker = current($marker_array);
            $explode_marker = explode('|', $first_marker);
            $map->center_lat = $explode_marker[0];
            $map->center_lng = $explode_marker[1];
        }
        if (strpos($key, 'address') === 0) {
            $address_array[$key] = $value;
            $first_address = current($address_array);
            $rm_space_ads = str_replace(' ', '+', $first_address);
            $geocode = wp_remote_get('http://maps.google.com/maps/api/geocode/json?address=' . $rm_space_ads . '&sensor=false');
            $output = json_decode($geocode['body']);
            $map->center_lat = $output->results[0]->geometry->location->lat;
            $map->center_lng = $output->results[0]->geometry->location->lng;
        }
    }
    $map->map_width = $atts['width'];
    $map->map_height = $atts['height'];
    $map->zoom = $atts['zoom'];
    if (is_array($marker_array)) {
        foreach ($marker_array as $marker) {
            $explode_marker = explode('|', $marker);
            if ($explode_marker[6] != '') {
                $icon = $wpdb->get_row($wpdb->prepare('SELECT group_marker FROM ' . $wpdb->prefix . 'group_map WHERE group_map_id=%d', $explode_marker[6]));
                $icon = $icon->group_marker;
            } else {
                $icon = '';
            }
            if (empty($explode_marker[4])) {
                $clickable = 'false';
            } else {
                $clickable = $explode_marker[4];
            }
            if (empty($explode_marker[5])) {
                $draggable = 'false';
            } else {
                $draggable = $explode_marker[5];
            }
            $map->addMarker($explode_marker[0], $explode_marker[1], $clickable, $explode_marker[2], $explode_marker[3], $icon, '', $draggable, '', $group_id = '');
        }
    }
    if (is_array($address_array)) {
        foreach ($address_array as $address) {
            $explode_address = explode('|', $address);
            $rm_space_ads = str_replace(' ', '+', $explode_address[0]);
            $geocode = wp_remote_get('http://maps.google.com/maps/api/geocode/json?address=' . $rm_space_ads . '&sensor=false');
            $output = json_decode($geocode['body']);
            $lat = $output->results[0]->geometry->location->lat;
            $lng = $output->results[0]->geometry->location->lng;
            if ($explode_address[5] != '') {
                $icon_image = $wpdb->get_row($wpdb->prepare('SELECT group_marker FROM ' . $wpdb->prefix . 'group_map WHERE group_map_id=%d', $explode_address[5]));
                $icon = $icon_image->group_marker;
            } else {
                $icon = '';
            }
            if (empty($explode_address[3])) {
                $clickable = 'false';
            } else {
                $clickable = $explode_address[3];
            }
            if (empty($explode_address[4])) {
                $draggable = 'false';
            } else {
                $draggable = $explode_address[4];
            }
            $map->addMarker($lat, $lng, $clickable, $explode_address[1], $explode_address[2], $icon, '', $draggable, '', $group_id = '');
        }
    }
    echo $map->showmap();
    $content = ob_get_contents();
    ob_clean();
    return $content;
}