2010-02-11 28 views
10

Giả sử tôi có quyền kiểm soát chế độ xem bản đồ trong ứng dụng Android của mình. Nếu tôi biết một mốc tồn tại ở một vĩ độ và kinh độ nhất định, làm cách nào để xác định liệu mốc đó hiện có thể xem được trên màn hình của người dùng không? Có phương pháp nào để nhận tọa độ cho các góc trên bên trái và dưới cùng bên phải của khu vực hiển thị không?Làm cách nào để xác định liệu một địa chỉ được hiển thị trong khu vực hiện có thể xem?

+0

Mẹo, chỉ cần chú ý về vấn đề này: http://stackoverflow.com/questions/ 2667386/mapview-getlatitudespan-and-getlongitudespan-not-working –

Trả lời

6

Something như thế này sẽ làm các trick.

private boolean isCurrentLocationVisible() 
    { 
     Rect currentMapBoundsRect = new Rect(); 
     Point currentDevicePosition = new Point(); 
     GeoPoint deviceLocation = new GeoPoint((int) (bestCurrentLocation.getLatitude() * 1000000.0), (int) (bestCurrentLocation.getLongitude() * 1000000.0)); 

     mapView.getProjection().toPixels(deviceLocation, currentDevicePosition); 
     mapView.getDrawingRect(currentMapBoundsRect); 

     return currentMapBoundsRect.contains(currentDevicePosition.x, currentDevicePosition.y); 

    } 
-1

Ok vì nó đã cho tôi một thời gian bây giờ để tìm một giải pháp làm việc đơn giản, tôi sẽ đăng nó ở đây cho tất cả sau khi tôi;) Trong lớp con của riêng bạn MapView chỉ cần sử dụng như sau:

GeoPoint topLeft = this.getProjection().fromPixels(0, 0); 
GeoPoint bottomRight = this.getProjection().fromPixels(getWidth(),getHeight()); 

Thats tất cả, sau đó bạn có thể lấy tọa độ qua

topLeft.getLatitudeE6()/1E6 
topLeft.getLongitudeE6()/1E6 

bottomRight.getLatitudeE6()/1E6 
bottomRight.getLongitudeE6()/1E6 
+2

Vui lòng dừng đăng lại chính xác trả lời cho nhiều bài đăng. Nếu chúng là bản sao, hãy gắn cờ chúng như vậy. Nếu không, hãy điều chỉnh câu trả lời của bạn cho câu hỏi cụ thể được đặt ra. –

+0

Nó không hoạt động. –

1

Tham gia những lời khuyên here, herehere:

Đây là mã hoàn chỉnh của CustomMapView tôi:

package net.alouw.alouwCheckin.ui.map; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.util.Pair; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapView; 

/** 
* @author Felipe Micaroni Lalli ([email protected]) 
*/ 
public class CustomMapView extends MapView { 
    public CustomMapView(Context context, String s) { 
     super(context, s); 
    } 

    public CustomMapView(Context context, AttributeSet attributeSet) { 
     super(context, attributeSet); 
    } 

    public CustomMapView(Context context, AttributeSet attributeSet, int i) { 
     super(context, attributeSet, i); 
    } 

    /** 
    * 
    * @return a Pair of two pairs with: top right corner (lat, lon), bottom left corner (lat, lon) 
    */ 
    public Pair<Pair<Double, Double>, Pair<Double, Double>> getMapCorners() { 
     GeoPoint center = getMapCenter(); 
     int latitudeSpan = getLatitudeSpan(); 
     int longtitudeSpan = getLongitudeSpan(); 

     double topRightLat = (center.getLatitudeE6() + (latitudeSpan/2.0d))/1.0E6; 
     double topRightLon = (center.getLongitudeE6() + (longtitudeSpan/2.0d))/1.0E6; 

     double bottomLeftLat = (center.getLatitudeE6() - (latitudeSpan/2.0d))/1.0E6; 
     double bottomLeftLon = (center.getLongitudeE6() - (longtitudeSpan/2.0d))/1.0E6; 

     return new Pair<Pair<Double, Double>, Pair<Double, Double>>(
       new Pair<Double, Double>(topRightLat, topRightLon), 
       new Pair<Double, Double>(bottomLeftLat, bottomLeftLon)); 
    } 
} 
Các vấn đề liên quan