[英]Google maps initialize location by wifi
I want to implement several things: 我要实现几件事:
when the activity creates the google maps will show the estimated location based on wifi... 当活动创建时,谷歌地图将显示基于wifi的估计位置...
how can I implement it? 我该如何实施?
I want to get the name of the location that user touch on google maps... 我想获取用户在Google地图上触摸过的位置的名称...
I read that I can implement it with the method, however with this method I can only get the latitude and longitude on the place that the user touched: 我读到可以用方法实现它,但是用这种方法,我只能在用户触摸的地方获得经度和纬度:
@Override public void onMapClick(LatLng point) { // TODO Auto-generated method stub }
I want to enable the user to search place in google maps, is there any method to implement it? 我想让用户能够在Google地图中搜索位置,有什么方法可以实现?
This is my activity till now: 到目前为止,这是我的活动:
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;
}
}
}
}
and this is my xml file till now: 到目前为止,这是我的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>
This will be managed automatically by the Maps component. 这将由地图组件自动管理。 You just have to call
GoogleMap.setMyLocationEnabled(true);
您只需要调用
GoogleMap.setMyLocationEnabled(true);
Important: Don't forget to call GoogleMap.setMyLocationEnabled(false)
in the onPause of your activity/fragment! 重要提示:不要忘记在活动/片段的onPause中调用
GoogleMap.setMyLocationEnabled(false)
!
You can use GoogleMap.setOnMyLocationChangeListener
to add an handler that would be called each time the user position change. 您可以使用
GoogleMap.setOnMyLocationChangeListener
添加一个处理程序,该处理程序将在每次用户位置更改时调用。
You can use Geocoder
to do reverse geocoding, but you will need a backend to be able to get results. 您可以使用
Geocoder
进行反向地理编码,但是需要后端才能获得结果。 Maybe the Google Play Service library provide said backend, but I am not sure as I never used this functionality 也许Google Play服务库提供了所说的后端,但是我不确定,因为我从未使用过此功能
To search the name of a touched location (not tested) : 要搜索触摸位置的名称 (未经测试) :
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.