简体   繁体   English

如何在中心 - android上使用固定标记搜索拖动地图上的位置

[英]How to search location on dragging map with fixed marker at center-android

I'm doing android map application in which i need to do dragging map with fixed marker at center. 我正在做android地图应用程序,我需要在中心用固定标记拖动地图。

Below is my code.It works well.Added i need to do searching functionality on it.ie When i type address on top EditBox,that location should come under fixed positioned marker . 下面是我的代码。它工作得很好。我需要在it.ie上做搜索功能。 当我在顶部EditBox上键入地址时,该位置应该位于固定定位标记下

How could i do that? 我怎么能这样做?

My Code: 我的代码:

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;
    }

Thanks. 谢谢。

I did something little tricky, it worked in my case. 我做了一些有点棘手的事情,它适用于我的情况。 You could put a marker as a view through the xml layout and make it centered. 您可以将标记作为视图通过xml布局并使其居中。

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

Then, you should Geocode the address from edittext for latitude and longitude. 然后,您应该从edittext中为经度和经度对地址进行地理编码。 Finally, animate the Camera to your input location. 最后,将相机设置为输入位置的动画。

CameraPosition aCameraPosition = new CameraPosition.Builder().target(
            new LatLng(latitude, longitude)).zoom(15).build();      
    aGoogleMapObject.animateCamera(CameraUpdateFactory.newCameraPosition(aCameraPosition));

Make adjustments to the marker in the xml, if needed. 如果需要,调整xml中的标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM