我的应用会显示地图,并自动将用户带到当前位置。有一个文本框,允许用户键入其位置,以在文本栏上键入的地址显示标记。
当我启动应用程序时,它会正确显示用户所在的位置。当我在地图上键入新地址时,它会在新键入的位置显示标记。但是,在此之后它会自动将用户带回到他们所在的位置,并从用户输入的位置删除标记。
我想onLocationChnaged会多次触发。有没有人知道应用程序启动时只找到一次用户位置的解决方案是什么?
这是我的代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (view != null)
{
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view); }
try
{
view = inflater.inflate(R.layout.fragment_map_view, container, false);
}
catch (InflateException e)
{ /* map is already there, just return view as it is */ }
LocationManager lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map_fragment)).getMap();
map.setMyLocationEnabled(true);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
DateTimeFragment datetime=new DateTimeFragment();
ft.add(R.id.datetime_container_map, datetime);
//SupportMapFragment mapFragment=null;
//if(mapFragment==null)
// mapFragment= (SupportMapFragment)fm.findFragmentById(R.id.map_fragment);
ft.commit();
AutoCompleteTextView location= (AutoCompleteTextView)view.findViewById(R.id.location_map);
location.setAdapter(new PlacesAutoCompleteAdapter(getActivity(),R.layout.list_layout));
location.setOnItemClickListener(this);
Spinner category=(Spinner)view.findViewById(R.id.category_query_map);
ArrayAdapter<CharSequence> adapterCategory = ArrayAdapter.createFromResource(this.getActivity().getBaseContext(),R.array.category_names, android.R.layout.simple_spinner_item);
adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
category.setAdapter(adapterCategory);
category.setOnItemSelectedListener(this);
return view;
}
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
String address_text = (String) adapterView.getItemAtPosition(position);
Geocoder geocoder = new Geocoder(getActivity().getBaseContext());
List<Address> addresses=null;
LatLng latlng;
MarkerOptions markerOptions;
try {
// Getting a maximum of 3 Address that matches the input text
addresses = geocoder.getFromLocationName(address_text, 3);
} catch (IOException e) {
e.printStackTrace();
}
if(addresses==null || addresses.size()==0){
Toast.makeText(getActivity().getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
}
// Clears all the existing markers on the map
map.clear();
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
latlng = new LatLng(address.getLatitude(), address.getLongitude());
String addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());
markerOptions = new MarkerOptions();
markerOptions.position(latlng);
markerOptions.title(addressText);
map.addMarker(markerOptions);
// Locate the first location
if(i==0)
map.animateCamera(CameraUpdateFactory.newLatLng(latlng));
}
}
@Override
public void onLocationChanged(Location location) {
map.clear();
MarkerOptions mp = new MarkerOptions();
mp.position(new LatLng(location.getLatitude(), location.getLongitude()));
mp.title("my position");
map.addMarker(mp);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 16));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
有人知道为什么在用户输入的地址中显示标记后,标记会自动更新到用户位置吗?
PS:我还想在附注中知道,如果使用Geocoder将字符串转换为地址是一个好习惯,如图所示..或者我应该将它放在AsyncTask中答案 0 :(得分:1)
您的onLocationChanged()
是否正在解雇并将标记更新到当前位置?
您可以防止多次更新:
private Location mLocation = null;
@Override
public void onLocationChanged(Location location) {
if (mLocation == null) {
mLocation = location;
map.clear();
MarkerOptions mp = new MarkerOptions();
mp.position(new LatLng(location.getLatitude(), location.getLongitude()));
mp.title("my position");
map.addMarker(mp);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 16));
}
}