Java 谷歌地图通过wifi初始化位置

Java 谷歌地图通过wifi初始化位置,java,android,google-maps-android-api-2,Java,Android,Google Maps Android Api 2,我想实现几件事: 当活动创建时,谷歌地图将显示基于wifi的估计位置… 我如何实现它 我想获得用户在谷歌地图上触摸到的位置的名称… 我读到我可以用这个方法实现它,但是用这个方法我只能得到用户接触的地方的纬度和经度: @Override public void onMapClick(LatLng point) { // TODO Auto-generated method stub } 我想让用户在谷歌地图中搜索地点,有什么方法可以实现吗 这是我迄今为止的活动: import java

我想实现几件事:

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

  • 我想获得用户在谷歌地图上触摸到的位置的名称…
    我读到我可以用这个方法实现它,但是用这个方法我只能得到用户接触的地方的纬度和经度:

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

  • 这是我迄今为止的活动:

    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 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>
    
    import java.lang.ref.WeakReference;
    导入com.google.android.gms.maps.CameraUpdateFactory;
    导入com.google.android.gms.maps.GoogleMap;
    导入com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
    导入com.google.android.gms.maps.MapFragment;
    导入com.google.android.gms.maps.model.BitmapDescriptorFactory;
    导入com.google.android.gms.maps.model.LatLng;
    导入com.google.android.gms.maps.model.Marker;
    导入com.google.android.gms.maps.model.MarkerOptions;
    导入android.app.Activity;
    导入android.content.Context;
    导入android.graphics.Bitmap;
    导入android.graphics.BitmapFactory;
    导入android.os.Bundle;
    导入android.os.Handler;
    导入android.os.Message;
    导入android.support.v4.app.FragmentActivity;
    导入android.util.Log;
    导入android.view.view;
    导入android.view.ViewParent;
    导入android.widget.ImageView;
    导入android.widget.Toast;
    公共类itemSaleActivity扩展了FragmentActivity{
    私有静态上下文应用程序;
    私有静态谷歌地图;
    静态最终车床汉堡=新车床(53.558,9.927);
    私有静态最终整数负载_坐标=0;
    私人图像视图pic;
    私人场所;
    创建时的公共void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //将视图设置为register.xml
    setContentView(R.layout.itemsale);
    app=getApplicationContext();
    Bundle extras=getIntent().getExtras();
    byte[]byteArray=extras.getByteArray(“图片”);
    位图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(新MarkerOptions().position(hamburg.title)(“hamburg”);
    mHandler=新的LocationHandler(此);
    onmarkerdragstener-markerdragstener=new-onmarkerdragstener(){
    @凌驾
    MarkerDragStart上的公共空白(标记){
    //开始拖动标记时调用
    }
    @凌驾
    MarkerDragend上的公共空白(标记){
    //在放下标记时调用。
    double[]坐标=null;
    coords[0]=marker.getPosition().纬度;
    coords[1]=marker.getPosition().经度;
    恢复与保存位置(coords);
    Toast.makeText(getApplicationContext(),“Pin掉在:“+coords[0]+”,“+coords[1]+marker.getTitle(),Toast.LENGTH_LONG).show();
    }
    @凌驾
    MarkerDrag上的公共空白(标记){
    }
    };
    setOnMarkerDragListener(markerDragListener);
    查看titleView=getWindow().findViewById(android.R.id.title);
    如果(标题视图!=null){
    ViewParent=titleView.getParent();
    if(父级!=null&(视图的父级实例)){
    视图父视图=(视图)父视图;
    parentView.setVisibility(View.GONE);
    }
    }
    //您还可以通过传递
    //字符序列或资源id。
    }
    私有void RestoreUIwithSavedLocation(双[]坐标){
    获取(mHandler,LOAD_COORD,coordsArray).sendToTarget();
    }
    静态类LocationHandler扩展了处理程序{
    弱引用性;
    公共位置处理程序(活动){
    mActivity=新的WeakReference(活动);
    }
    公共无效handleMessage(消息消息消息){
    Activity contextActivity=mActivity.get();
    开关((int)msg.what){
    壳体载荷协调:
    map.clear();
    double[]coordArray=(double[])msg.obj;
    Marker Marker=map.addMarker(新的MarkerOptions().position(新的LatLng(coordArray[0],coordArray[1]));
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(新LatLng(coordArray[0],coordArray[1]),18));
    animateCamera(CameraUpdateFactory.zoomTo(18),2000年,空);
    marker.SetDragable(真);
    字符串s=Double.toString(coordArray[0])+“,”+Double.toString(coordArray[1]);
    Toast.makeText(应用程序,“在案例中”+s,Toast.LENGTH_LONG.show();
    打破
    }
    }
    }
    }
    
    这是我到目前为止的xml文件:

    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 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>
    
    
    
  • 这将由“地图”组件自动管理。您只需拨打
    重要提示:不要忘记在活动/片段暂停时调用
    GoogleMap.setMyLocationEnabled(false)

  • 您可以使用添加处理程序,该处理程序将在每次用户位置更改时调用

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


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

    MyActivity扩展了活动实现OnMapClickListener{
    地理编码器;
    @凌驾