gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 12:50:02 25 4
gpt4 key购买 nike

我在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com