构造函数GeoPoint(double,double)未定义。这有什么问题?

时间:2011-10-31 22:29:58

标签: android

我遇到错误:“构造函数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;
    }
}

3 个答案:

答案 0 :(得分: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。

答案 1 :(得分:2)

提出了Ian G. Clifton的不错的实用方法,似乎不必要的冗长:

/**
 * 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));
}

答案 2 :(得分:1)

java不会自动将double转换为int(丢失数据等),并且GeoPoint的唯一构造函数接受2个int。所以写:

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

或者首先将你的积分宣布为整数。