0

我想实现几件事:

  1. 当活动创建时,谷歌地图将显示基于 wifi 的估计位置......
    我该如何实现它?

  2. 我想获取用户在谷歌地图上触摸的位置的名称......
    我读到我可以使用该方法实现它,但是使用这种方法我只能获取用户触摸的地方的纬度和经度:

    @Override
    public void onMapClick(LatLng point) {
        // TODO Auto-generated method stub
    }
    
  3. 我想让用户在谷歌地图中搜索地点,有什么方法可以实现吗?

这是我到目前为止的活动:

import java.lang.ref.WeakReference;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.Toast;

public class itemSaleActivity extends FragmentActivity  {


    private static Context app;
    private static GoogleMap map;
    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    private static final int LOAD_COORD = 0;
    private ImageView pic;
    private LocationHandler mHandler;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // Set View to register.xml
        setContentView(R.layout.itemsale);

        app = getApplicationContext();
        Bundle extras = getIntent().getExtras();
        byte[] byteArray = extras.getByteArray("picture");
        Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

        pic = (ImageView) findViewById(R.id.itemImage);
        pic.setImageBitmap(bmp);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        //Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG).title("Hamburg"));
        mHandler = new LocationHandler(this); 

        OnMarkerDragListener markerDragListener = new OnMarkerDragListener() {

            @Override
            public void onMarkerDragStart(Marker marker) {
                // Called when the marker drag is started

            }

            @Override
            public void onMarkerDragEnd(Marker marker) {
                // Called when the marker is dropped down.
                double[] coords = null;
                coords[0] = marker.getPosition().latitude;
                coords[1] = marker.getPosition().longitude;
                RestoreUIwithSavedLocation(coords);
                Toast.makeText(getApplicationContext(),"Pin Dropped at: " + coords[0] + ", " + coords[1]+marker.getTitle() , Toast.LENGTH_LONG).show();

            }

            @Override
            public void onMarkerDrag(Marker marker) {

            }
        };

        map.setOnMarkerDragListener(markerDragListener);

        View titleView = getWindow().findViewById(android.R.id.title);
        if (titleView != null) {
            ViewParent parent = titleView.getParent();
            if (parent != null && (parent instanceof View)) {
             View parentView = (View)parent;
             parentView.setVisibility(View.GONE);
            }
        }

     // You can also assign the title programmatically by passing a
     // CharSequence or resource id.
    }

    private void RestoreUIwithSavedLocation(double[] coordsArray) {
        Message.obtain(mHandler, LOAD_COORD, coordsArray).sendToTarget();
    }

    static class LocationHandler extends Handler {
        WeakReference<Activity> mActivity;

        public LocationHandler(Activity activity) {
            mActivity = new WeakReference<Activity>(activity);
        }

        public void handleMessage(Message msg) {
            Activity contextActivity = mActivity.get();
            switch ((int)msg.what) {

            case LOAD_COORD:
                map.clear();
                double[] coordArray = (double[])msg.obj;
                Marker marker = map.addMarker(new MarkerOptions().position(new LatLng(coordArray[0], coordArray[1])));
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(coordArray[0], coordArray[1]), 18));
                map.animateCamera(CameraUpdateFactory.zoomTo(18), 2000, null);
                marker.setDraggable(true);
                String s = Double.toString(coordArray[0]) + ", " + Double.toString(coordArray[1]);              
                Toast.makeText(app,"in the case"+s , Toast.LENGTH_LONG).show();
                break;

            }
        }
    }
}

到目前为止,这是我的 xml 文件:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/itemImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/logo" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="400dp"
        android:layout_marginTop="10dp"
        android:layout_weight="66496.79"
        android:orientation="horizontal" >

        <fragment
            android:id="@+id/map"
            class="com.google.android.gms.maps.MapFragment"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="150dp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="170dp"
            android:orientation="vertical" >

            <Button
            android:id="@+id/messageBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:text="fffff" />

            <Button
                android:id="@+id/call"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:text="ffff" />

        <Button
            android:id="@+id/sms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/button1"
            android:layout_centerVertical="true"
            android:text="ffff" />
        <Button
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/button1"
            android:layout_centerVertical="true"
            android:text="fffff" />

        <Button
            android:id="@+id/navigate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_alignParentEnd="false"
            android:layout_alignParentStart="false"
            android:layout_below="@+id/button1"
            android:layout_marginTop="14dp"
            android:text="ffffffffff" />
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>
4

1 回答 1

0
  1. 这将由地图组件自动管理。您只需要调用重要提示:不要忘记调用您的活动/片段的 onPause!GoogleMap.setMyLocationEnabled(true);
    GoogleMap.setMyLocationEnabled(false)

  2. 您可以使用GoogleMap.setOnMyLocationChangeListener添加一个处理程序,每次用户位置更改时都会调用该处理程序。

  3. 您可以使用Geocoder反向地理编码,但您需要一个后端才能获得结果。也许Google Play 服务库提供了所说的后端,但我不确定,因为我从未使用过这个功能


要搜索触摸位置的名称 (未测试)

MyActivity extends Activity implements OnMapClickListener {

    Geocoder geocoder;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        geocoder = new Geocoder(this);
    }

    @Override
    public void onMapClick(LatLng point) {
         List<Address> addresses = geocoder.getFromLocation(point.latitude, point.longitude, 1);

         if (adresses.length > 0) {
             Address address = addresses.get(0);
             Toast.makeText(this, address.getFeatureName(), Toast.LENGTH_SHORT).show();
         }
         else {
             Toast.makeText(this, "Not found!", Toast.LENGTH_SHORT).show();
         }
    }
于 2013-03-29T13:29:34.830 回答