首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在移动地图上搜索固定标记位置

如何在移动地图上搜索固定标记位置
EN

Stack Overflow用户
提问于 2013-04-23 07:34:56
回答 1查看 3.1K关注 0票数 0

我正在做安卓地图应用程序,在这个应用程序中,我需要用固定的标记来拖动地图。

下面是我的code.It works well.Added,我需要在it.i.e.When上执行搜索功能--在EditBox顶部键入address,该位置应该位于固定位置标记下。

我怎么能这么做?

我的守则:

代码语言:javascript
复制
public class MapDragActivity extends MapActivity implements LocationListener{
    String pinadd="";
    private MapView map=null;
    private LocationManager locationManager;
    GeoPoint myLocation;

    /** Called when the activity is first created. */

    @SuppressWarnings("static-access")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maplayout);


        map=(MapView)findViewById(R.id.map);
        map.setBuiltInZoomControls(true);


        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (location != null)
            plotLocation(location);
        else
            locationManager.requestLocationUpdates(
                    locationManager.NETWORK_PROVIDER, 500L, 250.0f, this);

    }

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        if (location != null)
            plotLocation(location);
    }

    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @SuppressWarnings("static-access")
    @Override
    public void onResume() {
            super.onResume();
            locationManager.requestLocationUpdates(
                    locationManager.NETWORK_PROVIDER, 1000L, 500.0f, this);
   } 

    @Override
    public void onPause() {
            super.onPause();
            locationManager.removeUpdates(this);
    }

    @Override
    public void onDestroy(){
        locationManager.removeUpdates(this);
        super.onDestroy();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event){
        boolean result = super.dispatchTouchEvent(event);
        if (event.getAction() == MotionEvent.ACTION_UP){
                    Projection projection = map.getProjection();
                    int y = map.getHeight() / 2;
                    int x = map.getWidth() / 2;

                    GeoPoint geoPoint = projection.fromPixels(x, y);
                    Log.v("Latitude & Logitude", geoPoint.getLatitudeE6() +"//"+geoPoint.getLongitudeE6());
        }
        return result;
    }

    double roundTwoDecimals(double d){
            DecimalFormat twoDForm = new DecimalFormat("#.######");
            return Double.valueOf(twoDForm.format(d));
    }

    public void plotLocation(Location location) {
            GeoPoint point = new GeoPoint(
                    (int) (roundTwoDecimals(location.getLatitude()) * 1E6),
                    (int) (roundTwoDecimals(location.getLongitude()) * 1E6));
            myLocation = point;
            map.getController().animateTo(point);
            map.getController().setCenter(point);
            zoomToMyLocation();
    }

    private void zoomToMyLocation() {
        if (myLocation != null) {
            map.getController().setZoom(18);
            map.getController().animateTo(myLocation);
        }
    }

    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-04-22 15:28:20

我做了件有点棘手的事,在我的案子里起作用了。您可以在xml布局中放置一个标记作为视图,并使其居中。

代码语言:javascript
复制
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

然后,您应该从编辑文本中为纬度和经度对地址进行地理编码。最后,将摄像机动画到您的输入位置。

代码语言:javascript
复制
CameraPosition aCameraPosition = new CameraPosition.Builder().target(
            new LatLng(latitude, longitude)).zoom(15).build();      
    aGoogleMapObject.animateCamera(CameraUpdateFactory.newCameraPosition(aCameraPosition));

如果需要,对xml中的标记进行调整。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16163564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档