Google地图通过wifi初始化位置

时间:2013-03-29 13:08:36

标签: java android google-maps-android-api-2

我想实现几件事:

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

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

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

  4. 这是我迄今为止的活动:

    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>
    

1 个答案:

答案 0 :(得分:0)

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

  2. 您可以使用GoogleMap.setOnMyLocationChangeListener添加每次用户位置更改时调用的处理程序。

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


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

    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();
             }
        }
    
相关问题