2011-10-31 132 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)); 
} 
相关问题