The Mobile Galaxy The New Mobile Era Has Come | Mobile – A Time To Remember

31Mar/140

How to fix WordPress 3.8.1 admin page not found issues?

After upgrade to WordPress 3.8.1, admin area some links got " Sorry, but you are looking for something that isn't here."

or got Error 500 on WP-admin

It caused by php memory limit .

so edit a file php.ini

put below line to it:

max_execution_time = 30 ; Maximum execution time of each script, in seconds
max_input_time = 60 ; Maximum amount of time each script may spend parsing request data
memory_limit = 512M ; Maximum amount of memory a script may consume (32MB)

and upload this file to wp-admin folder

it will fix the issues

 

Share
Filed under: HTML5, PHP No Comments
4Oct/112

Send Google Static map with direction in mail

From: http://www.think-1st.com/send-google-static-map-with-direction-in-mail/

 

I was working one with project in which client needed to send Google Static Map image with direction to email, After search for few hours i found one script which showed how to get static map for origin to destination but script did not have option for having multiple address as destinations. So i started digging more about Google Direction API, which is fantastic.

Google Direction API – http://code.google.com/apis/maps/documentation/directions/

Google Direction API can give you direction between 2 points (Only if there no sea in between. ;) If there is sea in between, we need to find AIR Direction API) in asked formats. Supported formats ar JSON, XML.

Detail about how to use Google Direction API is shown at Google.

After getting response from Google Direction API in JSON, we can parse it and extract direction steps and polyline points which helps draw line in Google Map.

For showing Static Map please refer to Google Static Map API –http://code.google.com/apis/maps/documentation/staticmaps/

 

<?php

getmap("Vadodara", array("Vallabh Vidya Nagar, Anand, Gujarat, India", "bareja, gujarat, India"));

/**
 * Implementation of hook_form().
 *
 */
function getmap($strStartAddress, $arrAddressInOrder) {
    global $base_url;

    $markers = array();

    $arrLatLon = getLatLong($strStartAddress);
    $markers[] = $arrLatLon['lat'].",".$arrLatLon['long'];

    foreach($arrAddressInOrder AS $address) {

        $arrLatLon = getLatLong($address);
        $markers[] = $arrLatLon['lat'].",".$arrLatLon['long'];
    }

    /* Get the driving directions from google api */
    $directions = retrieveDirections($strStartAddress, $arrAddressInOrder);

    /* Create the map image url with the directions coordinates */
    $staticMap = buildStaticMap($markers, 640, 640, $directions);

    $img = "<img src='$staticMap' align='left'/>";

    $instruction = "<p>Driving instruction displayed below.</p>";
    $legIndex = 0;
    foreach($directions['routes'][0]['legs'] AS $leg) {
        $instruction .= "<p><strong>".$leg['start_address']."</strong><p>";
        foreach($leg AS $steps) {
            if(!is_array($steps)) continue;
            foreach($steps AS $step) {
                if(!is_array($step)) continue;
                $instruction .= $step['html_instructions']."<br />";
            }
        }
        if($legIndex == (count($directions['routes'][0]['legs']) - 1))
            $instruction .= "<p><strong>".$leg['end_address']."</strong><p>";
        $legIndex++;
    }

    echo '<p>'.$img.$instruction.'</p>';

}

function getLatLong($address){
    if (!is_string($address))die("All Addresses must be passed as a string");
    $_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address));
    $_result = false;
    if($_result = file_get_contents($_url)) {
        if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false;
        preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match);
        $_coords['lat'] = $_match[1];
        $_coords['long'] = $_match[2];
    }
    return $_coords;
}

function buildStaticMap($markers=array(), $width=400, $height=400, $directions=null) {
    $strMarkers = "";
    foreach($markers as $marker) {
        if (!empty($strMarkers)) $strMarkers .= '|';
        $strMarkers .= urlencode($marker);
    }

    if (!empty($directions)) {
        $steps .= $directions['routes'][0]['overview_polyline']['points'];
    }

    if($strMarkers != "")
        $strMarkers = "&markers=".$strMarkers;

    $staticMap = 'http://maps.google.com/maps/api/staticmap?size='.$width.'x'.$height.'&maptype=roadmap&sensor=false' .$strMarkers. '&path=weight:3|color:blue|enc:'.$steps;
    return $staticMap;
}

function retrieveDirections ($from, $arrTo) {

    $q = "origin=".urlencode($from)."&";

    $q .= "destination=".urlencode($arrTo[count($arrTo) - 1])."";

    $index = 0;
    foreach($arrTo AS $index => $value) {
        if($index == (count($arrTo) - 1)) break;
        $via[] = urlencode($value);
        $index++;
    }

    if(count($via) > 0)
        $q = $q."&waypoints=".(implode("|", $via));

    $url = "http://maps.googleapis.com/maps/api/directions/json?sensor=false&". $q;
    $result = file_get_contents($url);
    $result = json_decode($result, true);

    return $result;
}

Running DEMO

Share
Tagged as: , 2 Comments
26Sep/110

Fetching tiles for Offline Map

BE WARE: If you are trying to cache the tiles that Google serves, that may be a violation of Google's Terms of Service.
Google Maps API Premier License Agreement

3.4 Cache Restrictions. Customer may not pre-fetch, retrieve, cache, index, or store any Content, or portion of the Services with the exception being Customer may store limited amounts of Content solely to improve the performance of the Customer Implementation due to network latency, and only if Customer does so temporarily, securely, and in a manner that (a) does not permit use of the Content outside of the Services; (b) is session-based only (once the browser is closed, any additional storage is prohibited); (c) does not manipulate or aggregate any Content or portion of the Services; (d) does not prevent Google from accurately tracking Page Views; and (e) does not modify or adjust attribution in any way.

using System.Collections.Generic;
using System.Text;
namespace MapTileURL
{
    class Program
    {
        private static Dictionary<string, string> MapTypeURLs = new Dictionary<string, string>
        {
            { "GoogleSatURL", "<a href="http://khm{GOOG_DIGIT}.google.com/kh/v=58&x={X}&y={Y}&z={ZOOM}&s={GALILEO">http://khm{GOOG_DIGIT}.google.com/kh/v=58&x={X}&y={Y}&z={ZOOM}&s={GALILEO</a>}" },
            { "GoogleMapURL", "<a href="http://mt{GOOG_DIGIT}.google.com/vt/lyrs=m@121&hl=en&x={X}&y={Y}&z={ZOOM}&s={GALILEO">http://mt{GOOG_DIGIT}.google.com/vt/lyrs=m@121&hl=en&x={X}&y={Y}&z={ZOOM}&s={GALILEO</a>}" },
            { "GoogleHybURL", "<a href="http://mt{GOOG_DIGIT}.google.com/vt/lyrs=h@121&hl=en&x={X}&y={Y}&z={ZOOM}&s={GALILEO">http://mt{GOOG_DIGIT}.google.com/vt/lyrs=h@121&hl=en&x={X}&y={Y}&z={ZOOM}&s={GALILEO</a>}" },
            { "GoogleTerURL", "<a href="http://mt{GOOG_DIGIT}.google.com/vt/lyrs=t@108,r@121&hl=en&x={X}&y={Y}&z={ZOOM}&s={GALILEO">http://mt{GOOG_DIGIT}.google.com/vt/lyrs=t@108,r@121&hl=en&x={X}&y={Y}&z={ZOOM}&s={GALILEO</a>}" },
            { "GoogleChinaURL", "<a href="http://mt{GOOG_DIGIT}.google.cn/vt/lyrs=m@121&hl=en&gl=cn&x={X}&y={Y}&z={ZOOM}&s={GALILEO">http://mt{GOOG_DIGIT}.google.cn/vt/lyrs=m@121&hl=en&gl=cn&x={X}&y={Y}&z={ZOOM}&s={GALILEO</a>}" },
            { "YahooMapURL", "<a href="http://maps{Y_DIGIT}.yimg.com/hx/tl?v=4.3&.intl=en&x={X}&y={YAHOO_Y}&z={YAHOO_ZOOM}&r=1">http://maps{Y_DIGIT}.yimg.com/hx/tl?v=4.3&.intl=en&x={X}&y={YAHOO_Y}&z={YAHOO_ZOOM}&r=1</a>" },
            { "YahooSatURL", "<a href="http://maps{Y_DIGIT}.yimg.com/ae/ximg?v=1.9&t=a&s=256&.intl=en&x={X}&y={YAHOO_Y}&z={YAHOO_ZOOM}&r=1">http://maps{Y_DIGIT}.yimg.com/ae/ximg?v=1.9&t=a&s=256&.intl=en&x={X}&y={YAHOO_Y}&z={YAHOO_ZOOM}&r=1</a>" },
            { "YahooInMapURL", "<a href="http://maps.yimg.com/hw/tile?locale=en&imgtype=png&yimgv=1.2&v=4.1&x={X}&y={YAHOO_Y}&z={YAHOO_ZOOM_2">http://maps.yimg.com/hw/tile?locale=en&imgtype=png&yimgv=1.2&v=4.1&x={X}&y={YAHOO_Y}&z={YAHOO_ZOOM_2</a>}" },
            { "YahooInHybURL", "<a href="http://maps.yimg.com/hw/tile?imgtype=png&yimgv=0.95&t=h&x={X}&y={YAHOO_Y}&z={YAHOO_ZOOM_2">http://maps.yimg.com/hw/tile?imgtype=png&yimgv=0.95&t=h&x={X}&y={YAHOO_Y}&z={YAHOO_ZOOM_2</a>}" },
            { "YahooHybURL", "<a href="http://maps{Y_DIGIT}.yimg.com/hx/tl?v=4.3&t=h&.intl=en&x={X}&y={YAHOO_Y}&z={YAHOO_ZOOM}&r=1">http://maps{Y_DIGIT}.yimg.com/hx/tl?v=4.3&t=h&.intl=en&x={X}&y={YAHOO_Y}&z={YAHOO_ZOOM}&r=1</a>" },
            { "MicrosoftBrMapURL", "<a href="http://imakm{MS_DIGITBR}.maplink3.com.br/maps.ashx?v={QUAD}|t&call=2.2.4">http://imakm{MS_DIGITBR}.maplink3.com.br/maps.ashx?v={QUAD}|t&call=2.2.4</a>" },
            { "MicrosoftHybURL", "<a href="http://ecn.t{MS_DIGIT}.tiles.virtualearth.net/tiles/h{QUAD}.png?g=441&mkt=en-us&n=z">http://ecn.t{MS_DIGIT}.tiles.virtualearth.net/tiles/h{QUAD}.png?g=441&mkt=en-us&n=z</a>" },
            { "MicrosoftSatURL", "<a href="http://ecn.t{MS_DIGIT}.tiles.virtualearth.net/tiles/a{QUAD}.png?g=441&mkt=en-us&n=z">http://ecn.t{MS_DIGIT}.tiles.virtualearth.net/tiles/a{QUAD}.png?g=441&mkt=en-us&n=z</a>" },
            { "MicrosoftMapURL", "<a href="http://ecn.t{MS_DIGIT}.tiles.virtualearth.net/tiles/r{QUAD}.png?g=441&mkt=en-us&n=z">http://ecn.t{MS_DIGIT}.tiles.virtualearth.net/tiles/r{QUAD}.png?g=441&mkt=en-us&n=z</a>" },
            { "MicrosoftTerURL", "<a href="http://ecn.t{MS_DIGIT}.tiles.virtualearth.net/tiles/r{QUAD}.png?g=441&mkt=en-us&shading=hill&n=z">http://ecn.t{MS_DIGIT}.tiles.virtualearth.net/tiles/r{QUAD}.png?g=441&mkt=en-us&shading=hill&n=z</a>" },
            { "OpenStreetMapURL", "<a href="http://tile.openstreetmap.org/{ZOOM}/{X}/{Y}.png">http://tile.openstreetmap.org/{ZOOM}/{X}/{Y}.png</a>" },
            { "OSMARenderURL", "<a href="http://tah.openstreetmap.org/Tiles/tile/{ZOOM}/{X}/{Y}.png">http://tah.openstreetmap.org/Tiles/tile/{ZOOM}/{X}/{Y}.png</a>" },
            { "OpenAerialMapURL", "<a href="http://tile.openaerialmap.org/tiles/?v=mgm&layer=openaerialmap-900913&x={X}&y={Y}&zoom={OAM_ZOOM">http://tile.openaerialmap.org/tiles/?v=mgm&layer=openaerialmap-900913&x={X}&y={Y}&zoom={OAM_ZOOM</a>}" },
            { "OpenCycleMapURL", "<a href="http://andy.sandbox.cloudmade.com/tiles/cycle/{ZOOM}/{X}/{Y}.png">http://andy.sandbox.cloudmade.com/tiles/cycle/{ZOOM}/{X}/{Y}.png</a>" }
        };
        readonly static char[] Lookup = new[] { 'q', 't', 'r', 's' };
        public static string GetQuadtreeFromXYZoom(int x, int y, int zoom)
        {
            var quad = new StringBuilder();
            for (int i = 0; i < zoom; i++)
            {
                int rx = x % 2;
                int ry = y % 2;
                x /= 2;
                y /= 2;
                quad.Insert(0, Lookup[rx * 2 + ry]);
            }
            quad.Insert(0, 't');
            return quad.ToString();
        }
        public static string BuildUrl(string tileType, int x,int y,int zoom)
        {

            string url = "";
            // Edited by Shustrik - compacted the switch statement
            switch (tileType)
            {
                case "GoogleSat":
                case "GoogleSatH":
                    url = MapTypeURLs["GoogleSatURL"];
                    break;
                case "GoogleMap":
                    url = MapTypeURLs["GoogleMapURL"];
                    break;
                case "GoogleHyb":
                    url = MapTypeURLs["GoogleHybURL"];
                    break;
                case "GoogleTer":
                    url = MapTypeURLs["GoogleTerURL"];
                    break;
                case "GoogleChina":
                    url = MapTypeURLs["GoogleChinaURL"];
                    break;
                case "MicrosoftMap":
                    url = MapTypeURLs["MicrosoftMapURL"];
                    break;
                case "MicrosoftSat":
                    url = MapTypeURLs["MicrosoftSatURL"];
                    break;
                case "MicrosoftHyb":
                    url = MapTypeURLs["MicrosoftHybURL"];
                    break;
                case "MicrosoftTer":
                    url = MapTypeURLs["MicrosoftTerURL"];
                    break;
                case "MicrosoftBrMap":
                    if (zoom <= 10)
                        url =  MapTypeURLs["MicrosoftMapURL"];
                    else
                        url = MapTypeURLs["MicrosoftBrMapURL"];
                    break;
                case "YahooMap":
                    url = MapTypeURLs["YahooMapURL"];
                    break;
                case "YahooSat":
                case "YahooSatH":
                case "YahooSatH2":
                    url = MapTypeURLs["YahooSatURL"];
                    break;
                case "YahooHyb":
                    url = MapTypeURLs["YahooHybURL"];
                    break;
                case "YahooInMap":
                    url = MapTypeURLs["YahooInMapURL"];
                    break;
                case "YahooInHyb":
                    url = MapTypeURLs["YahooInHybURL"];
                    break;
                case "OpenStreetMap":
                    url = MapTypeURLs["OpenStreetMapURL"];
                    break;
                case "OSMARender":
                    url = MapTypeURLs["OSMARenderURL"];
                    break;
                case "OpenAerialMap":
                    url = MapTypeURLs["OpenAerialMapURL"];
                    break;
                case "OpenCycleMap":
                    url = MapTypeURLs["OpenCycleMapURL"];
                    break;
            }

            // Edited by Shustrik - added variables for configuration settings other than Google
            string quadcode = "";
            for (int i = (int)zoom - 1; i >= 0; i--)
                quadcode = quadcode + (((((y >> i) & 1) << 1) + ((x >> i) & 1)));
            url = url.Replace("{X}", x.ToString());
            url = url.Replace("{Y}", y.ToString());
            url = url.Replace("{Z}", ((int)zoom).ToString());
            url = url.Replace("{ZOOM}", ((int)zoom).ToString());
            url = url.Replace("{QUAD}", quadcode);
            url = url.Replace("{YAHOO_Y}", (((1 << ((int)zoom)) >> 1) - 1 - y).ToString());
            url = url.Replace("{YAHOO_ZOOM}", ((int)zoom + 1).ToString());
            url = url.Replace("{YAHOO_ZOOM_2}", (17 - (int)zoom + 1).ToString());
            url = url.Replace("{OAM_ZOOM}", (17 - (int)zoom).ToString());
            url = url.Replace("{GOOG_DIGIT}", ((x + y) & 3).ToString());
            url = url.Replace("{GOOG_QUAD}", GetQuadtreeFromXYZoom(x, y, zoom));
            url = url.Replace("{MS_DIGITBR}", ((((y & 1) << 1) + (x & 1)) + 1).ToString());
            url = url.Replace("{MS_DIGIT}", ((((y & 3) << 1) + (x & 1))).ToString());
            url = url.Replace("{Y_DIGIT}", ((x + y + zoom) % 3 +1).ToString());
            url = url.Replace("{GALILEO}", "Galileo".Substring(0, ((3 * x + y) & 7)));
            // support old style {} vars
            url = url.Replace("QQQQ", GetQuadtreeFromXYZoom(x,y,zoom));
            url = url.Replace("XXXX", x.ToString());
            url = url.Replace("YYYY", y.ToString());
            url = url.Replace("ZZZZ", (17 - (int)zoom).ToString());
            url = url.Replace("{OSM_ZOOM}", ((int)zoom).ToString());
            url = url.Replace("{MS_QUADCODE}", quadcode);
            url = url.Replace("*", ((x+y+zoom) %4).ToString());
            return url;
        }
        static void Main(string[] args)
        {
            string url = BuildUrl("GoogleMap", 1, 1, 1);
            //return <a href="http://mt2.google.com/vt/lyrs=m@121&hl=en&x=1&y=1&z=1&s=Gali">http://mt2.google.com/vt/lyrs=m@121&hl=en&x=1&y=1&z=1&s=Gali</a>
        }
    }
}
Share
Tagged as: , , , No Comments
25Sep/110

How to get the center coordinate of google map?

Sometimes, When we download offline map, we need a center coordinate of a map.

1. open google map;

2. adjust map and make sure the point  what you want at the center;

3. paste the following code at the url address input area of your explorer.

javascript:void(prompt('',gApplication.getMap().getCenter()))

 

Share
Filed under: HTML5, iPhone, Other, PHP No Comments
2Sep/110

Coordinate and Address

Get current location coordinate:

@implementation CurrentLocation

@synthesize locationManager;
@synthesize target,callBack;

#pragma mark --
#pragma mark Public
-(void) startUpdatingLocation{
	[[self locationManager] startUpdatingLocation];
}
#pragma mark --
#pragma mark Memory management
-(void) dealloc{
	[super dealloc];
	[locationManager release];
}
#pragma mark --
#pragma mark Location manager
/*
 Return a location manager -- create one if necessary.
 */
- (CLLocationManager *)locationManager {
    if (locationManager != nil) {return locationManager;}
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    [locationManager setDelegate:self];
    return locationManager;
}
#pragma mark --
#pragma mark CLLocationManagerDelegate methods
/*
 Conditionally enable the Add button:
 If the location manager is generating updates, then enable the button;
 If the location manager is failing, then disable the button.
 */
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
	NSLog(@"got the coordinate!");
}
- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
        NSLog(@"Failure!");
 }
@end

Get current location address:

@implementation AddressReverseGeoder

#pragma mark --
#pragma mark Public
//根据经纬度开始获取详细地址信息
- (void)startedReverseGeoderWithLatitude:(double)latitude longitude:(double)longitude{
	CLLocationCoordinate2D coordinate2D;
	coordinate2D.longitude = longitude;
	coordinate2D.latitude = latitude;
	//
	MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate2D];
	geoCoder.delegate = self;
	[geoCoder start];
}
#pragma mark --
#pragma mark MKReverseGeocoderDelegate methods
//获得地址信息
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
	NSString *address = [NSString stringWithFormat:@"%@ %@ %@ %@ %@%@", 
									  placemark.country,
									  placemark.administrativeArea,
									  placemark.locality,
									  placemark.subLocality,
									  placemark.thoroughfare,
									  placemark.subThoroughfare];
	NSLog(@"经纬度所对应的详细:%@", address);
	geocoder = nil;
}
//错误处理
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {
	NSLog(@"error %@" , error);
}
#pragma mark --
#pragma mark Memory management
- (void)dealloc {
    [super dealloc];
}
@end
Share
30Aug/114

中国地图偏移校正算法

该校正适用于 Google map China, Microsoft map china ,MapABC 等,因为这些地图构成方法是一样的。

需要offset.dat文件,该文件为0.01精度校正数据, 有需要者请联系作者, 报价: 5,000.00 RMB。

php算法

<?php
/*
   代码功能:利用0.01精度校正库文件修正中国地图经纬度偏移。
*/
header("Content-Type:text/html; charset=utf-8");
define('__dat_db__' , 'offset.dat' );// DAT数据文件
define('datmax' , 9813675 );// 数据条数-结束记录

// # offset.php?lat=39.914914&lon=116.460633
$lon=$_GET['lon'];
$lat=$_GET['lat'];
$tmplon=intval($lon * 100);
$tmplat=intval($lat * 100);
//经度到像素X值
function lngToPixel($lng,$zoom) {
return ($lng+180)*(256<<$zoom)/360;
}
//像素X到经度
function pixelToLng($pixelX,$zoom){
return $pixelX*360/(256<<$zoom)-180;
}
//纬度到像素Y
function latToPixel($lat, $zoom) {
$siny = sin($lat * pi() / 180);
$y=log((1+$siny)/(1-$siny));
return (128<<$zoom)*(1-$y/(2*pi()));
}
//像素Y到纬度
function pixelToLat($pixelY, $zoom) {
$y = 2*pi()*(1-$pixelY /(128 << $zoom));
$z = pow(M_E, $y);
$siny = ($z -1)/($z +1);
return asin($siny) * 180/pi();
}

function xy_fk( $number ){
        $fp = fopen(__dat_db__,"rb"); //■1■.将 r 改为 rb
        $myxy=$number;//#"112262582";
        $left = 0;//开始记录
        $right = datmax;//结束记录

        //采用用二分法来查找查数据
        while($left <= $right){
            $recordCount =(floor(($left+$right)/2))*8; //取半
            //echo "运算:left=".$left." right=".$right." midde=".$recordCount."<br />";
            @fseek ( $fp, $recordCount , SEEK_SET ); //设置游标
            $c = fread($fp,8); //读8字节
            $lon = unpack('s',substr($c,0,2));
            $lat = unpack('s',substr($c,2,2));
            $x = unpack('s',substr($c,4,2));
            $y = unpack('s',substr($c,6,2));
            $jwd=$lon[1].$lat[1];
            //echo "找到的经纬度:".$jwd;
            if ($jwd==$myxy){
               fclose($fp);
               return $x[1]."|".$y[1];
               break;
            }else if($jwd<$myxy){
               //echo " > ".$myxy."<br />";
               $left=($recordCount/8) +1;
            }else if($jwd>$myxy){
               //echo " < ".$myxy."<br />";
               $right=($recordCount/8) -1;
            }

        }
        fclose($fp);
}

$offset =xy_fk($tmplon.$tmplat);
$off=explode('|',$offset);
$lngPixel=lngToPixel($lon,18)+$off[0];
$latPixel=latToPixel($lat,18)+$off[1];

echo pixelToLat($latPixel,18).",".pixelToLng($lngPixel,18);

?>

c#算法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;   

namespace MapDigit.GIS
{
    public class GeoLatLng
    {   

        public GeoLatLng(double latitude, double longitude)
        {
            this.latitude = latitude;
            this.longitude = longitude;
        }
        public double latitude;
        public double longitude;
    }   

    public class GeoPoint
    {
        public GeoPoint(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        public int x;
        public int y;
    }   

    public class OffsetInChina
    {
        //用于从GPS坐标转换为偏移后坐标
        public static GeoLatLng fromEarthToMars(GeoLatLng earth)
        {
            GeoPoint ptOffset = getOffset(earth.latitude, earth.longitude);
            if (ptOffset.x != 0 || ptOffset.y != 0)
            {
                int pixelX, pixelY;
                TileSystem.LatLongToPixelXY(earth.latitude, earth.longitude, 18, out pixelX, out pixelY);
                GeoPoint pt = new GeoPoint(pixelX, pixelY);
                pt.x += ptOffset.x;
                pt.y += ptOffset.y;
                double latitude, longitude;
                TileSystem.PixelXYToLatLong(pt.x, pt.y, 18, out latitude, out longitude);
                return new GeoLatLng(latitude, longitude);   

            }
            else
            {
                return new GeoLatLng(earth.latitude, earth.longitude);
            }   

        }   

        //用于将偏移后坐标转成真实的坐标
        public static GeoLatLng fromMarToEarth(GeoLatLng mars)
        {
            GeoPoint ptOffset = getOffset(mars.latitude, mars.longitude);
            if (ptOffset.x != 0 || ptOffset.y != 0)
            {
                int pixelX, pixelY;
                TileSystem.LatLongToPixelXY(mars.latitude, mars.longitude, 18, out pixelX, out pixelY);
                GeoPoint pt = new GeoPoint(pixelX, pixelY);
                pt.x -= ptOffset.x;
                pt.y -= ptOffset.y;
                double latitude, longitude;
                TileSystem.PixelXYToLatLong(pt.x, pt.y, 18, out latitude, out longitude);
                return new GeoLatLng(latitude, longitude);   

            }
            else
            {
                return new GeoLatLng(mars.latitude, mars.longitude);
            }
        }   

        //这个函数用于将需要查询的经纬度转成最近的0.01分度值,无插值
        //也可以自行实现插值
        private static GeoPoint getQueryLocation(double latitude, double longitude)
        {
            int lat = (int)(latitude * 100);
            int lng = (int)(longitude * 100);
            double lat1 = ((int)(latitude * 1000 + 0.499999)) / 10.0;
            double lng1 = ((int)(longitude * 1000 + 0.499999)) / 10.0;
            for (double x = longitude; x < longitude + 1; x += 0.5)
            {
                for (double y = latitude; x < latitude + 1; y += 0.5)
                {
                    if (x <= lng1 && lng1 < (x + 0.5) && lat1 >= y && lat1 < (y + 0.5))
                    {
                        return new GeoPoint((int)(x + 0.5), (int)(y + 0.5));
                    }
                }
            }
            return new GeoPoint(lng, lat);
        }   

        private static GeoPoint getOffset(double longitude, double latitude)
        {
            //这个函数用于返回查询结果,就是从校正数据中返回18级时x,y方偏移
            //可以自行实现
            return null;
        }   

    }
}
Share
Tagged as: , 4 Comments