如何计算当前视图中是否存在经度/纬度

发布于 2024-10-11 14:33:28 字数 744 浏览 3 评论 0原文

我真的很困惑:

我有经度/纬度点可以在 Android 应用程序中的谷歌地图上绘制。 因此,我创建了一个扩展 Overlay 的类...

我通过以下方式获取视图的当前经度/纬度部分:

GeoPoint topLeft = proj.fromPixels(0, 0);
GeoPoint bottomRight = proj.fromPixels(width-1, height-1);
int topLat = topLeft.getLatitudeE6();
int topLon = topLeft.getLongitudeE6();
int bottomLat = bottomRight.getLatitudeE6();
int bottomLon = bottomRight.getLongitudeE6();

以下有效(仅纬度):

if(latLon[0] >= bottomLat && latLon[0] <= topLat){ // do something; }

但这不起作用(经度):

if(latLon[1] >= topLon && latLon[1] <= bottomLon) { // do something; }

latLon[0] 是我想检查的纬度 latLon[1] 是我想检查的经度

有人有想法吗?

问候!

i am really stuck on this:

I have longitude / latitude points to draw on a google map within an android app.
Therefor I created a class extending Overlay...

I get the current longitude/latitude portion of view via:

GeoPoint topLeft = proj.fromPixels(0, 0);
GeoPoint bottomRight = proj.fromPixels(width-1, height-1);
int topLat = topLeft.getLatitudeE6();
int topLon = topLeft.getLongitudeE6();
int bottomLat = bottomRight.getLatitudeE6();
int bottomLon = bottomRight.getLongitudeE6();

The following works (only latitudes):

if(latLon[0] >= bottomLat && latLon[0] <= topLat){ // do something; }

but this does not work (longitudes):

if(latLon[1] >= topLon && latLon[1] <= bottomLon) { // do something; }

latLon[0] is the latitude I want to check
latLon[1] is the longitude I want to check

Anybody an idea?

Greetz!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你不解释 2024-10-18 14:33:29

从英格兰 0 号子午线向西开始,经度数字为负值。北美和南美的结果为阴性。

Longitude numbers are negative from the 0 meridian in England, westward. They're negative in North America and South America.

你的往事 2024-10-18 14:33:29

使用谷歌地图并在上面插入您的标记。
请参阅下文:

public class MapActivity extends GeolocationActivity implements GoogleMap.OnInfoWindowClickListener{

private final String TAG = "MapFragment";


private MapView _mapView;
private GoogleMap _gmap;

/**
 * the logo of the marker on the map
 */
private Bitmap _littlePin;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);

    _mapView = (MapView) findViewById(R.id.location_map);
    _mapView.onCreate(savedInstanceState);
    _gmap = _mapView.getMap(); // initalise the map
    if (_gmap != null)
    {
        setUpMap();
    }
}

/**
 * set up the map; Add the market on it with a blue dot for the location of the user in real time
 * @return false if something goes wrong, true otherwise
 * _latitude = latitude of the user, get from the GeolocationActivity
 * _longitude = longitude of the user, get from the GeolocationActivity
 */
private boolean setUpMap()
{
    // Gets to GoogleMap from the MapView and does initialization stuff
    _gmap.getUiSettings().setMyLocationButtonEnabled(false);
    _gmap.setMyLocationEnabled(true);
    if (_gmap == null) {
        Toast.makeText(this, "Google Maps not available",  Toast.LENGTH_LONG).show();
        return false;
    }
    // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
    MapsInitializer.initialize(this);


    BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.pin); // file stored in /res/drawable/
    Bitmap bitmap = bitmapDrawable.getBitmap();
    _littlePin = Bitmap.createScaledBitmap(bitmap, 96, 96, false); // 96/96px

    _gmap.setOnInfoWindowClickListener(this);// add listener on this class


    // here the _lat and _long should be the user of this cafe, at the moment I am using the one of the user
    this.addMarker("Cafe", "Best cafe in London", new LatLng(_latitude, _longitude));


    // camera, 13 if the zoon, you can change it manually, or dynamically in order to see all your markers
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(_latitude, _longitude), 13);
    _gmap.animateCamera(cameraUpdate); // zoom effect
    return true;
}

/**
 * Add markers on the map view
 * @param title title of place / marker
 * @param description short description
 * @param position geposition, LatLng object, filled with latitude and longitude.
 */
private void addMarker(String title, String description, LatLng position)
{
    _gmap.addMarker(new MarkerOptions()
            .position(position)
            .title(title)
            .snippet(description)
            .icon(BitmapDescriptorFactory.fromBitmap(_littlePin)));
}

/**
 * The user click on the marker on the map, this callback is called
 * @param marker clicked marker
 */
@Override
public void onInfoWindowClick(Marker marker) {
    Log.d(TAG, "onMarkerClick " + marker.getTitle());
}


@Override
public void onResume() {
    _mapView.onResume();
    super.onResume();
}

@Override
public void onDestroy() {
    super.onDestroy();
    _mapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    _mapView.onLowMemory();
}

}

这是一个很好的示例,可以帮助您。

以及 github 代码示例位于此处

希望这有帮助。

Use a google map and insert your marker on it.
See below :

public class MapActivity extends GeolocationActivity implements GoogleMap.OnInfoWindowClickListener{

private final String TAG = "MapFragment";


private MapView _mapView;
private GoogleMap _gmap;

/**
 * the logo of the marker on the map
 */
private Bitmap _littlePin;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);

    _mapView = (MapView) findViewById(R.id.location_map);
    _mapView.onCreate(savedInstanceState);
    _gmap = _mapView.getMap(); // initalise the map
    if (_gmap != null)
    {
        setUpMap();
    }
}

/**
 * set up the map; Add the market on it with a blue dot for the location of the user in real time
 * @return false if something goes wrong, true otherwise
 * _latitude = latitude of the user, get from the GeolocationActivity
 * _longitude = longitude of the user, get from the GeolocationActivity
 */
private boolean setUpMap()
{
    // Gets to GoogleMap from the MapView and does initialization stuff
    _gmap.getUiSettings().setMyLocationButtonEnabled(false);
    _gmap.setMyLocationEnabled(true);
    if (_gmap == null) {
        Toast.makeText(this, "Google Maps not available",  Toast.LENGTH_LONG).show();
        return false;
    }
    // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
    MapsInitializer.initialize(this);


    BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.pin); // file stored in /res/drawable/
    Bitmap bitmap = bitmapDrawable.getBitmap();
    _littlePin = Bitmap.createScaledBitmap(bitmap, 96, 96, false); // 96/96px

    _gmap.setOnInfoWindowClickListener(this);// add listener on this class


    // here the _lat and _long should be the user of this cafe, at the moment I am using the one of the user
    this.addMarker("Cafe", "Best cafe in London", new LatLng(_latitude, _longitude));


    // camera, 13 if the zoon, you can change it manually, or dynamically in order to see all your markers
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(_latitude, _longitude), 13);
    _gmap.animateCamera(cameraUpdate); // zoom effect
    return true;
}

/**
 * Add markers on the map view
 * @param title title of place / marker
 * @param description short description
 * @param position geposition, LatLng object, filled with latitude and longitude.
 */
private void addMarker(String title, String description, LatLng position)
{
    _gmap.addMarker(new MarkerOptions()
            .position(position)
            .title(title)
            .snippet(description)
            .icon(BitmapDescriptorFactory.fromBitmap(_littlePin)));
}

/**
 * The user click on the marker on the map, this callback is called
 * @param marker clicked marker
 */
@Override
public void onInfoWindowClick(Marker marker) {
    Log.d(TAG, "onMarkerClick " + marker.getTitle());
}


@Override
public void onResume() {
    _mapView.onResume();
    super.onResume();
}

@Override
public void onDestroy() {
    super.onDestroy();
    _mapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    _mapView.onLowMemory();
}

}

Here is a good example which gonna help you.

and the github with the code sample is here.

Hope this help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文