1. android中根據谷歌地圖的經緯度怎麼計算倆個點之間的直線距離
查詢小於 10 公里已內的地圖點 SQL 語句
$result = mysql_query ( "SELECT name,longitude,latitude,des FROM location
where type=".$type." and (((longitude - ".$lng.") * (longitude - ".$lng.") + (latitude
- ".$lat.") * (latitude - ".$lat.")) <= (10 / 110) * (10 / 110))");
其中核心部分是 (((longitude - ".$lng.") * (longitude - ".$lng.") + (latitude -
".$lat.") * (latitude - ".$lat.")) <= (10 / 110) * (10 / 110)),$lng和$lat 分別代碼
從Android 客戶端提交過來的經度和緯度。
2. android初始化坐標距離
兩點之間的距離是6378.137。
給定兩點的經緯度。計算兩點之間的距離。這里要注意經緯度一定要依照順序填寫
1. 利用android中的工具獲得,單位是米。
2. 通過計算的方式獲得,單位是公里。
當兩點的距離較近時使用兩種方式計算的結果基本一致,但是當兩點的距離比較遠時,計算的結果就會相差甚遠,比較發現兩點之間計算球面距離的方式更加精確一些,更貼近實際距離。本文重點講解兩點之間球面距離的計算方式。
3. 安卓怎麼用兩點的經緯度計算距離
計算公式:
1、Lat1 Lung1表示A點經緯度,Lat2 Lung2表示B點經緯度;
2、a=Lat1–Lat2為兩點緯度之差b=Lung1 -Lung2為兩點經度之差;
3、6378.137為地球半徑,單位為千米;
計算出來的結果單位為千米。
4. android百度地圖怎麼計算兩點距離
路線規劃提供了獲取路線距離的方法,見MKRoutePlan 類的 getDistance 方法。
如果是計算任意兩點的距離,自2.0.0版本開始,Android SDK為開發者提供了計算距離的介面(DistanceUtil),具體使用方法如下:
1.GeoPoint p1LL = new GeoPoint(39971802, 116347927);
2.GeoPoint p2LL = new GeoPoint(39892131, 116498555);
3.double distance = DistanceUtil.getDistance(p1LL, p2LL);
如果開發者使用的是1.3.5及以前的版本,在計算任意兩點之前的距離時,有如下兩種方法:一種利用勾股定理計算,適用於兩點距離很近的情況;一種按標準的球面大圓劣弧長度計算,適用於距離較遠的情況。
5. 兩點間距離怎麼算啊
在其中一條直線上找一點(有特定的點就直接用)如:2x+y=1,取x=0,則y=1(就可取點(0,1))再用點到直線的距離公式計算即可。
兩點間距離公式常用於函數圖形內求兩點之間距離、求點的坐標的基本公式,是距離公式之一。兩點間距離公式敘述了點和點之間距離的關系。
實例
現在有一隻工程隊要鋪設一條網路,連接A,B兩城。他們首先要知道兩城之間的距離,才能准備材料。他們用全球定位系統將兩城的位置在平面直角坐標系中表示出來。我們就來試試看能不能幫他們求出A、B兩城之間的距離。
6. android怎麼計算兩個坐標點之間的距離
千 鋒扣丁學堂android開發為您解答:
public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
Java code?
Parameters
startLatitude the starting latitude
startLongitude the starting longitude
endLatitude the ending latitude
endLongitude the ending longitude
results an array of floats to hold the results
詳細解釋見這里: distanceBetween(double, double, double, double, float[])
在大量坐標之間找最近距離的坐標
如果你用Mongodb,獲取最近的坐標很簡單,Mongodb自帶geoNearCommand,可以參照這里:GeospatialIndexing-geoNearCommand
Java code?
> db.runCommand( { geoNear : "places" , near : [50,50], num : 10 } );
> db.runCommand({geoNear:"asdf", near:[50,50]})
{
"ns" : "test.places",
"near" : "",
"results" : [
{
"dis" : 69.29646421910687,
"obj" : {
"_id" : ObjectId("4b8bd6b93b83c574d8760280"),
"y" : [
1,
1
],
"category" : "Coffee"
}
},
{
"dis" : 69.29646421910687,
"obj" : {
"_id" : ObjectId("4b8bd6b03b83c574d876027f"),
"y" : [
1,
1
]
}
}
],
"stats" : {
"time" : 0,
"btreelocs" : 1,
"btreelocs" : 1,
"nscanned" : 2,
"nscanned" : 2,
"objectsLoaded" : 2,
"objectsLoaded" : 2,
"avgDistance" : 69.29646421910687
},
"ok" : 1
}
7. Android游戲開發之Cocos2d-x
游戲的製作如播放電影,擁有四大核心,包括: 導演、場景、圖層、演員 。通過它們之間的不同配合,實現豐富多彩的效果。
public class CocosActivity extends AppCompatActivity {
private CCDirector mCcDirector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CCGLSurfaceView ccglSurfaceView =new CCGLSurfaceView(this);
setContentView(ccglSurfaceView);
導演,全局只有一個,單例模式創建
mCcDirector = CCDirector.sharedDirector();
開啟繪制(開始拍電影)
mCcDirector.attachInView(ccglSurfaceView);
幀率,每秒刷新的次數,與手機性能有關
//ccDirector.setDisplayFPS(true);
設置幀率為60(記得把幀率文件放進項目中)
//ccDirector.setAnimationInterval(1/60f);
設置橫屏
//ccDirector.setDeviceOrientation(CCDirector.);
屏幕適配,會基於不同屏幕大小進行適配
mCcDirector.setScreenSize(1920,1080);
場景
CCScene ccScene = CCScene.node();
圖層
CCLayer ccLayer =CCLayer.node();
給場景添加圖層
ccScene.addChild(ccLayer);
導演運行場景
mCcDirector.runWithScene(ccScene);
}
@Override
protected void onResume() {
super.onResume();
mCcDirector.onResume();
}
@Override
protected void onPause() {
super.onPause();
mCcDirector.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mCcDirector.end();
}
1、平移
參數1是移動時間,參數2是目標位置坐標,ccp方法是把坐標轉換為cocos2d的坐標
CCMoveTo ccMoveTo = CCMoveTo.action(3,ccp(200,0));
與上不同的是,第2個參數代表水平移動200,豎直不移動,即偏移量
CCMoveBy ccMoveBy =CCMoveBy.action(3,ccp(200,0));
2、旋轉
參數1時間,參數2角度,旋轉中心是左下角的點,順時針轉
CCRotateBy ccRotateBy =CCRotateBy.action(3,360);
與上不同的是,逆時針轉(捷徑,順時針轉270==逆時針轉90)
CCRotateTo ccRotateTo =CCRotateTo.action(3,270);
3、縮放
參數1時間,參數2X方向縮放,參數3Y方向縮放
CCScaleBy ccScaleBy =CCScaleBy.action(3,2,2);
參數1時間,參數2縮放
CCScaleTo ccScaleTo =CCScaleTo.action(3,2);
4、跳躍(參數1時間,參數2目標點,參數3跳躍高度,參數4跳躍次數)
CCJumpBy ccJumpBy =CCJumpBy.action(3,ccp(0,0),100,3);
5、淡入淡出(參數是時間)
CCFadeIn ccFadeIn =CCFadeIn.action(2);
CCFadeOut ccFadeOut =CCFadeOut.action(2);
6、貝塞爾曲線
點的含義,從point1出發,經過point2,到達endPosition
CCBezierConfig ccBezierConfig =new CCBezierConfig();
ccBezierConfig. controlPoint_1 =ccp(100,50);
ccBezierConfig. controlPoint_2 =ccp(200,200);
ccBezierConfig. endPosition =ccp(300,100);
CCBezierBy ccBezierBy =CCBezierBy.action(2,ccBezierConfig);
7、加速度
CCMoveBy ccMoveBy =CCMoveBy.action(2,ccp(100,100));
漸快漸慢,參數2是加速度
CCEaseIn ccEaseIn =CCEaseIn.action( ccMoveBy ,5);
CCEaseOut ccEaseOut =CCEaseOut.action( ccMoveBy ,5);
8、閃爍(時間,閃爍次數)
CCBlink ccBlink =CCBlink.action(2,10);
9、文字(參1是顯示的文字,參2是字體格式(可不填,如""),參數3是字體大小)
CCLabel ccLabel = CCLabel.labelWithString("顯示文字","STCAIYUN.TTF",20);
設置文字顏色
ccColor3B ccColor3B =ccc3(100,50,0);
ccLabel.setColor(ccColor3B );
設置文字位置
ccLabel.setPosition(ccp(width,height));
重置文字
ccLabel.setString("顯示文字");
顏色漸變(從本來的文字顏色漸變到設置的字體顏色)
CCTintBy tintBy =CCTintBy.action(3,ccc3(50,-50,100));
10、循環(參數是動畫效果)
CCRepeatForever ccRepeatForever =CCRepeatForever.action(action);
11、延遲(參數是時間)
CCDelayTime ccDelayTime =CCDelayTime.action(1);
12、反轉(將一個動畫倒序執行)
ccJumpBy.reverse();
13、同時(參數是不定長度數組)
CCSpawn ccSpawn =CCSpawn.actions(action1,action2);
14、連續動畫(參數是不定長度數組)
CCSequence ccSequence =CCSequence.actions(action1,action2, ccCallFunc ");
15、反射(執行一個動畫方法)
CCCallFunc .action(this," anim ");
被反射的執行方法
public void anim (){}
16、逐幀動畫
public void walk(){
存放幀動畫的集合
ArrayList ccSpriteFrames =new ArrayList<>();
String format="z_1_%d.png"; // %d和%02d代表整數,用i來取代,%d只取代i,%02d會補0
for(int i=1;i<10;i++){
CCSprite ccSprite =CCSprite.sprite(String.format(format,i));
CCSpriteFrame ccSpriteFrame =ccSprite.displayedFrame();
ccSpriteFrames.add(ccSpriteFrame );
}
參數1是動畫名稱,參數2是每一幀停留的時間,參數3是幀動畫集合
CCAnimation ccAnimation =CCAnimation.animation("walk",.2f,ccSpriteFrames);
參數2是否持續執行動畫,如果想執行無限循環,可以使用CCRepeatForever
CCAnimate ccAnimate =CCAnimate.action(ccAnimation,false);
mCcSprite.runAction(ccAnimate);
}
17、隱藏
CCHide ccHide =CCHide.action();
18、顯示
CCShow ccShow =CCShow.action();
19、跟隨(參數是跟隨的對象)
CCFollow ccFollow =CCFollow.action(mCCSprite);
20、執行動畫(上述皆是)
mCcSprite.runAction(action);
21、工具類使用
CGPointUtil .distance(point1,point2); //計算兩點距離
獲取聲音引擎
SoundEngine engine =SoundEngine.sharedEngine();
參數1是activity,SurfaceView創建時存入,參2是播放資源id(res\raw),參數3是否循環播放
engine.playSound(CCDirector.theApp,1,true);
手動停止音樂播放
engine.realesAllSounds();
生命周期跟隨Activity
SoundEngine.sharedEngine().resumeSound();
SoundEngine.sharedEngine().pauseSound();
SoundEngine.sharedEngine().realesAllSounds();
預載入播放音樂,避免播放音樂時沒聲音
SoundEngine.sharedEngine().preloadEffect();
SoundEngine.sharedEngine().preloadSound();
1、創建
CCSprite mCcSprite =new CCSprite(" pic.jpeg ");
2、錨點(圖片上的一點,類似圖釘,對應圖片顯示的位置)
CGPoint cgPoint =ccp(0,0);
mCcSprite.setAnchorPoint(cgPoint);
3、位置
mCcSprite.setPosition(cgPoint);
4、屬性(縮放、翻轉、透明度)
ccSprite.setFlipX(true); 水平翻轉
ccSprite.setOpacity(255); 透明度
ccSprite.setScale(2); 縮放
5、移除(在Layer裡面操作)
mCcSprite.removeSelf(); //精靈從圖層中移除
( Layer )this.removeSelf(); // 移除整個圖層
6、尺寸
CGSize cgSize =CCDirector.sharedDirector().winSize();
1、游戲暫停(圖層靜止,將不再響應任何事件)
MapLayer.this.onExit();
注意:因為圖層不再響應任何事件,所以暫停按鈕應該加在暫停圖層的父圖層上
this.getParent().addChild(new PauseLayer());
2、游戲繼續(圖層恢復動態,接收點擊事件)
MapLayer.this.onEnter();
通用的游戲繼續方法
CCDirector.sharedDirector().getRunningScene().onEnter();
3、定時器
CCScheler ccScheler =CCScheler.sharedScheler();
通過反射執行方法
ccScheler.schele(" onScheler ",this,2,false);
方法聲明為公開類型
public void onScheler (float f){ //實現具體邏輯
};
4、進度條
CCProgressTimer ccProgressTimer =CCProgressTimer.progressWithFile(" image/pic.jpeg "); //多層目錄
ccProgressTimer.setPosition(width,height);
this.getParent().addChild(ccProgressTimer); //具體加到什麼圖層,看情況
ccProgressTimer.setScale(0.6f);
ccProgressTimer.setPercentage(2);
設置顯示樣式(垂直,水平)->(最後2個字母,left,right,代表進度條從左往右)
ccProgressTimer.setType(CCProgressTimer. LR );
進度條外框 等元素,都作為精靈與進度條同級加入圖層
this.getParent().addChild(sprite);
Android坐標系的(0,0)在左上角,而Cocos2d-x坐標系的(0,0)在左下角。所以在處理Android的點擊事件MotionEvent時,需要進行坐標體系的轉換。
CGPoint cgPoint = convertPrevTouchToNodeSpace (event);
監聽圖片的點擊范圍,自定義封裝點擊事件
CGRect .containsPoint( mCcSprite.getBoundingBox() , cgPoint );
CCMenu ccMenu =CCMenu.menu();
CCSprite normalSprite =CCSprite.sprite("pic_1.jpeg"); //正常情況下顯示的圖片
CCSprite selectSprite =CCSprite.sprite("pic_2.jpeg"); //按下時顯示的圖片
注意:反射的方法需要使用pulbic修飾,參數指target,直接傳this,它不是上下文Context
CCMenuItemSprite itemSprite =CCMenuItemSprite.item( normalSprite , selectSprite , this ," onCLick ");
ccMenu.addChild( itemSprite );
this.addChild(ccMenu); //菜單添加到圖層
public void onCLick (Object obj){} //點擊事件響應方法,必須用public修飾以及帶參數
setIsTouchEnabled(true); //打開圖層點擊事件,默認關閉
模仿任何天氣現象,只需要改這一句,剩下的不變
CCParticleSystem ccParticleSystem= CCParticleSnow.node();
設置雪花大小
ccParticleSystem.setScale(2);
設置飄落的速度
ccParticleSystem.setSpeed(10);
設置雪花的圖片
ccParticleSystem.setTexture(CCTextureCache.sharedTextureCache().addImage("snow.png"));
this.addChild(ccParticleSystem,1);
停止粒子系統(下雪)
ccParticleSystem.stopSystem();
1、載入地圖
ArrayList mCGPoints = new ArrayList<>();
CCTMXTiledMap mCctmxTiledMap = CCTMXTiledMap.tiledMap(" map.tmx ");
mCctmxTiledMap.setAnchorPoint(ccp(0.5f,0.f));
mCctmxTiledMap.setPosition(width,height);
CCTMXObjectGroup cctmxObjectGroup= mCctmxTiledMap.objectGroupNamed("road");
ArrayList<HashMap<String,String>> objects = cctmxObjectGroup.objects;
for(HashMap<String,String> hashMap:objects){ //載入地圖的坐標(需要經過的坐標點)
Integer x =Integer.parseInt(hashMap.get("x"));
Integer y =Integer.parseInt(hashMap.get("y"));
CGPoint cgPoint =ccp(x,y);
mCGPoints .add(cgPoint); }
this.addChild(mCctmxTiledMap);
在地圖上添加精靈
mCCSprite.setPosition(mCGPoints.get(0));
mCctmxTiledMap.addChild(mCCSprite);
地圖跟隨精靈的移動而移動
CCFollow ccFollow =CCFollow.action(mCCSprite);
mCctmxTiledMap .runAction(ccFollow);
2、地圖隨手指觸摸事件移動(重寫觸摸方法)
@Override
public boolean ccTouchesMoved(MotionEvent event) {
手指拖拽時,地圖隨手指移動
mCctmxTiledMap.touchMove(event,mCctmxTiledMap);
地圖移動,地圖上所有精靈都隨之移動(地圖是父親,精靈是孩子)
mCctmxTiledMap.addChild(mCCSprite);
return super.ccTouchesMoved(event);
}
創建場景
CCScene ccScene =CCScene.node();
場景添加圖層
ccScene.addChild(ccLayer);
場景切換特效
CCJumpZoomTransition ccJumpZoomTransition =CCJumpZoomTransition.transition(2,ccScene);
導演切換場景
CCDirector.sharedDirector(). replaceScene (ccJumpZoomTransition);
CopyOnWrite容器即寫時復制的容器。通俗的理解是當我們往一個容器添加元素的時候,不直接往當前容器添加,而是先將當前容器進行Copy,復制出一個新的容器,然後新的容器里添加元素,添加完元素之後,再將原容器的引用指向新的容器。這樣做的好處是我們可以對CopyOnWrite容器進行並發的讀,而不需要加鎖,因為當前容器不會添加任何元素。所以CopyOnWrite容器也是一種讀寫分離的思想,讀和寫不同的容器。用法和ArrayList相近。
CopyOnWriteArrayList OnWriteArrayList = new CopyOnWriteArrayList();
setIsTouchEnabled(true); //打開點擊事件
@Override
public boolean ccTouchesBegan (MotionEvent event) {
return super.ccTouchesBegan(event);
}
@Override
public boolean ccTouchesCancelled (MotionEvent event) {
return super.ccTouchesCancelled(event);
}
@Override
public boolean ccTouchesMoved (MotionEvent event) {
return super.ccTouchesMoved(event);
}
@Override
public boolean ccTouchesEnded (MotionEvent event) {
return super.ccTouchesEnded(event);
}
描述:UI頻繁刷新,造成主線程的堵塞或掛起
private CCGLSurfaceView mCCGLSurfaceView;
mCCGLSurfaceView = (CCGLSurfaceView) CCDirector.sharedDirector().getOpenGLView();
mCCGLSurfaceView.queueEvent(new Runnable() {
@Override
public void run() { //切換到主線程
}
});
描述:CCSprite運行動畫時,沒有表現出任何視覺效果。
原因:一個動畫只能被一個CCSprite執行一次,執行完成後,原來的動畫會失效。
解決:每次執行的動畫,都需要重新生成,即使是相同的動畫效果。
緩存:CCDirector.sharedDirector().purgeCachedData();
8. 求一個Android使用LocationManager獲取兩個點的經緯度之後計算出兩個點的距離的Demo十萬火急。
兩點經緯度,計算距離
這種公式我必然是不知道的,谷歌翻了翻,有人(http://xxyyyboy.blog.163.com/blog/static/765832620110410457662/)說是
1.Lat1 Lung1 表示A點經緯度,Lat2 Lung2 表示B點經緯度;
2.a=Lat1 – Lat2 為兩點緯度之差 b=Lung1 -Lung2 為兩點經度之差;
3.6378.137為地球半徑,單位為千米;
計算出來的結果單位為千米。
也有人(http://panyee.cnblogs.com/archive/2006/07/04/442771.html )說直接從google maps的腳本里扒了段代碼。
我作為不明真相的群眾就圍觀轉一下maps的代碼:計算的結果是米為單位。
// 計算兩點距離
private final double EARTH_RADIUS = 6378137.0;
private double gps2m(double lat_a, double lng_a, double lat_b, double lng_b) {
double radLat1 = (lat_a * Math.PI / 180.0);
double radLat2 = (lat_b * Math.PI / 180.0);
double a = radLat1 - radLat2;
double b = (lng_a - lng_b) * Math.PI / 180.0;
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
+ Math.cos(radLat1) * Math.cos(radLat2)
* Math.pow(Math.sin(b / 2), 2)));
s = s * EARTH_RADIUS;
s = Math.Round(s * 10000) / 10000;
return s;
}