2011-10-31 63 views
1

我有錯誤:「構造函數GeoPoint(double,double)未定義」。爲什麼這樣?如何做到這一點?據我所知,所有必要的庫鏈接和語法似乎是正確的。構造函數GeoPoint(double,double)未定義。那有什麼問題?

package com.fewpeople.geoplanner; 

import android.app.Activity; 
import android.os.Bundle; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 

public class GeoplannerActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     final MapView mMapView = (MapView) findViewById(R.id.mapview); 

    MapController mMapController = mMapView.getController(); 

    double x, y; 
    x= 60.113337; 
    y= 55.151317; 

    mMapController.animateTo(new GeoPoint(x, y)); 

    mMapController.setZoom(15); 

    } 

    protected boolean isRouteDisplayed() { 
     return false; 
    } 
} 

回答

5

GeoPoint需要兩個整數,這些整數是微小的座標。我使用這種簡單的方法:

/** 
* Converts a pair of coordinates to a GeoPoint 
* 
* @param coords double containing latitude and longitude 
* @return GeoPoint for the same coords 
*/ 
public static GeoPoint coordinatesToGeoPoint(double[] coords) { 
    if (coords.length > 2) { 
     return null; 
    } 
    if (coords[0] == Double.NaN || coords[1] == Double.NaN) { 
     return null; 
    } 
    final int latitude = (int) (coords[0] * 1E6); 
    final int longitude = (int) (coords[1] * 1E6); 
    return new GeoPoint(latitude, longitude); 
} 

此外,您的活動應擴展MapActivity。

+0

非常感謝你!有用! –

1

java不會自動將double轉換爲int(丟失數據等),並且GeoPoint的唯一構造方法接受2個整數。所以寫:

mMapController.animateTo(new GeoPoint((int)x, (int)y)); 

或者聲明你的點在整數首先。

+0

取代。有新的錯誤「類型MapController中的方法animateTo(GeoPoint,Message)不適用於參數(int, int)」。爲什麼? –

+1

我會從伊恩的評論去的。它更完整。 – drdwilcox

2

渡過了伊恩·G.克利夫頓的這似乎不必要的冗長的很好的工具方法:

/** 
* Converts a pair of coordinates to a GeoPoint 
* 
* @param lat double containing latitude 
* @param lng double containing longitude 
*    
* @return GeoPoint for the same coords 
*/ 
public static GeoPoint coordinatesToGeoPoint(double lat, double lgn) { 
    return new GeoPoint((int) (lat * 1E6), (int) (lgn * 1E6)); 
} 
相關問題