java - 在我的 android 应用程序中使用 mapview 时如何发送视口(viewport)坐标?

标签 java android android-mapview

我在我的 android 应用程序中使用 mapview 使用类 com.google.android.maps 当用户导航时,我不会使用后台进程加载标记 将视口(viewport)坐标发送到我的服务器 我可以像这里一样用 javascript 来做

 google.maps.event.addListener(map, 'idle', showMarkers);
  function showMarkers() {
var bounds = map.getBounds();

// Call you server with ajax passing it the bounds

// In the ajax callback delete the current markers and add new markers

但是我如何在 java 中执行此操作?请提出建议。

最佳答案

我发布了这个答案,我希望能节省一些时间 我发现对我来说最好的解决方案是使用自定义 map View SimpleMapView 首先在您的项目中创建 SimpleMapView 类,这是代码

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;




public class SimpleMapView extends MapView {

    private int currentZoomLevel = -1;
    private GeoPoint currentCenter;
    private List<ZoomChangeListener> zoomEvents = new ArrayList<ZoomChangeListener>();
    private List<PanChangeListener> panEvents = new ArrayList<PanChangeListener>();

    public SimpleMapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SimpleMapView(Context context, String apiKey) {
        super(context, apiKey);
    }

    public SimpleMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    /**
     * 
     * @return
     */
    public int[][] getBounds() {

        GeoPoint center = getMapCenter();
        int latitudeSpan = getLatitudeSpan();
        int longtitudeSpan = getLongitudeSpan();
        int[][] bounds = new int[2][2];

        bounds[0][0] = center.getLatitudeE6() - (latitudeSpan / 2);
        bounds[0][1] = center.getLongitudeE6() - (longtitudeSpan / 2);

        bounds[1][0] = center.getLatitudeE6() + (latitudeSpan / 2);
        bounds[1][1] = center.getLongitudeE6() + (longtitudeSpan / 2);
        return bounds;
    }

    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            GeoPoint centerGeoPoint = this.getMapCenter();
            if (currentCenter == null || 
                    (currentCenter.getLatitudeE6() != centerGeoPoint.getLatitudeE6()) ||
                    (currentCenter.getLongitudeE6() != centerGeoPoint.getLongitudeE6()) ) {
                firePanEvent(currentCenter, this.getMapCenter());
            }
            currentCenter = this.getMapCenter();
        }
        return super.onTouchEvent(ev);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if(getZoomLevel() != currentZoomLevel){
            fireZoomLevel(currentZoomLevel, getZoomLevel());
            currentZoomLevel = getZoomLevel();
        }
    }

    @Override
    public void setSatellite(boolean on){
        super.setSatellite(on);
    }

    @Override
    public MapController getController(){
        return super.getController();
    }

    private void fireZoomLevel(int old, int current){
        for(ZoomChangeListener event : zoomEvents){
            event.onZoom(old, current);
        }
    }

    private void firePanEvent(GeoPoint old, GeoPoint current){
        for(PanChangeListener event : panEvents){
            event.onPan(old, current);
        }
    }

    public void addZoomChangeListener(ZoomChangeListener listener){
        this.zoomEvents.add(listener);
    }

    public void addPanChangeListener(PanChangeListener listener){
        this.panEvents.add(listener);
    }
}

在你的mapactivity中做

 SimpleMapView mapView = (SimpleMapView) findViewById(R.id.mapView);

然后你有

    mapView.addPanChangeListener(new PanChangeListener() {

    @Override
    public void onPan(GeoPoint old, GeoPoint current) {
                  //TODO
              //do your work here 

                }
});

并在代码中添加 PanChangeListener 类

package yourPkageName;

import com.google.android.maps.GeoPoint;

public interface PanChangeListener {
    public void onPan(GeoPoint old, GeoPoint current);
}

并在代码中添加 ZoomChangeListener 类

package yourPkageName;

public interface ZoomChangeListener {
    public void onZoom(int old, int current);
}

在你的xml文件中添加

<?xml version="1.0" encoding="utf-8"?>

<YourPakageName.SimpleMapView   xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:apiKey="0mAbU5bZyFY2I46PFJ1ysXGcYlAmFM6fYBWSB7Q"
    android:clickable="true" />

关于java - 在我的 android 应用程序中使用 mapview 时如何发送视口(viewport)坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13154727/

相关文章:

android - 代号一 : I want to implement Automatic reading functionality

javascript - 防止 Phonegap notification.navigator 弹出窗口因对话框外的触摸而关闭

android - 无法获取 GoogleMap 对象

java - 输入类型日期以不同的格式传递给 Controller

java - 未找到数据 Java 异常

java - Java是否有计划用默认方法(java8)替代抽象类?

android - 安装 PhoneGap,执行命令时出错 'ant'

AlertDialog 中的 Android MapView

android - 在大多数设备上使用 Google map v2 时应用程序崩溃

java - 设置包装类是一个好的做法吗?