如何获取地图的坐标上自来水与MapFragment(不MapView类)? [英] How do I get the coordinates of a map on tap with MapFragment (not MapView)?

查看:158
本文介绍了如何获取地图的坐标上自来水与MapFragment(不MapView类)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有搜索周围时,地图上被窃听如何获取位置的坐标。然而,大多数,如果不是全部的实施例需要 MapView类作为参数。例如:

 公共布尔中的onTap(GeoPoint的P,图形页面地图){
    如果(isPinch){
        返回false;
    }其他{
        Log.i(TAG,TAP!);
        如果(P!= NULL){
            handleGeoPoint(P);
            返回true; //我们处理的自来水
        }其他{
            返回false; //空的GeoPoint
        }
    }
}@覆盖
公共布尔onTouchEvent(MotionEvent E,MapView类MapView类)
{
    诠释手指= e.getPointerCount();
    如果(e.getAction()== MotionEvent.ACTION_DOWN){
        isPinch = FALSE; //接触下来,不知道这是一个还捏
    }
    如果(e.getAction()== MotionEvent.ACTION_MOVE&放大器;&放大器;手指== 2){
        isPinch = TRUE; //两个手指,高清捏
    }
    返回super.onTouchEvent(即图形页面);
}

我如何因此与 MapFragment 获取地图上的抽头位置的位置,而不是图形页面 <? / p>

解决方案

有在的 $样品由谷歌Play服务SDK条件是c $ C 。这将使用 SupportMapFragment ,所以我不知道如何帮助,如果您使用的是新的,这将是 MapFragment

在EventsDemoActivity使用在此code地图样品的方法是将实施OnMapClickListener 的类。下面是一些code,你可能能够使用。结果

EventsDemoActivity:

 公共类EventsDemoActivity扩展FragmentActivity
    实现OnMapClickListener,OnMapLongClickListener {    私人GoogleMap的MMAP;
    私人TextView的mTapTextView;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.events_demo);        mTapTextView =(的TextView)findViewById(R.id.tap_text);        setUpMapIfNeeded();
    }    私人无效setUpMap()//如果setUpMapIfNeeded();需要再...
    {
        mMap.setOnMapClickListener(本);
        mMap.setOnMapLongClickListener(本);
    }    @覆盖
    公共无效onMapClick(经纬度点){
        mTapTextView.setText(窃听,点=+点);
    }    @覆盖
    公共无效onMapLongClick(经纬度点){
        mTapTextView.setText(长pressed,指向=+点);
    }
}

结果 events_demo.xml:

 &LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =match_parent
  机器人:layout_height =match_parent
  机器人:方向=垂直&GT;
  &LT;的TextView
    机器人:ID =@ + ID / tap_text
    机器人:文字=@字符串/ tap_instructions
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT/&GT;
  &LT;片段
    机器人:ID =@ + ID /图
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    类=com.google.android.gms.maps.SupportMapFragment/&GT;
&LT; / LinearLayout中&GT;

I have searched around on how to get the coordinates of a location when the map is tapped. However, most, if not all the examples require a MapView as a parameter. For example:

public boolean onTap(GeoPoint p, MapView map){
    if ( isPinch ){
        return false;
    }else{
        Log.i(TAG,"TAP!");
        if ( p!=null ){
            handleGeoPoint(p);
            return true;            // We handled the tap
        }else{
            return false;           // Null GeoPoint
        }
    }
}

@Override
public boolean onTouchEvent(MotionEvent e, MapView mapView)
{
    int fingers = e.getPointerCount();
    if( e.getAction()==MotionEvent.ACTION_DOWN ){
        isPinch=false;  // Touch DOWN, don't know if it's a pinch yet
    }
    if( e.getAction()==MotionEvent.ACTION_MOVE && fingers==2 ){
        isPinch=true;   // Two fingers, def a pinch
    }
    return super.onTouchEvent(e,mapView);
}

How do I therefore get the location of a tapped position on the map with MapFragment and not MapView?

解决方案

There is an example in the Sample Code provided by Google Play services SDK. This uses SupportMapFragment so I'm not sure how helpful this will be if you are using the new MapFragment.

The method the EventsDemoActivity uses in this map sample code is to implement OnMapClickListener to the class. Below is some of the code you might be able to use.

EventsDemoActivity:

public class EventsDemoActivity extends FragmentActivity
    implements OnMapClickListener, OnMapLongClickListener {

    private GoogleMap mMap;
    private TextView mTapTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.events_demo);

        mTapTextView = (TextView) findViewById(R.id.tap_text);

        setUpMapIfNeeded();
    }

    private void setUpMap() //If the setUpMapIfNeeded(); is needed then...
    {
        mMap.setOnMapClickListener(this);
        mMap.setOnMapLongClickListener(this);
    }

    @Override
    public void onMapClick(LatLng point) {
        mTapTextView.setText("tapped, point=" + point);
    }

    @Override
    public void onMapLongClick(LatLng point) {
        mTapTextView.setText("long pressed, point=" + point);
    }
}


events_demo.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <TextView
    android:id="@+id/tap_text"
    android:text="@string/tap_instructions"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
  <fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>

这篇关于如何获取地图的坐标上自来水与MapFragment(不MapView类)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆