gpt4 book ai didi

android - 仅在谷歌地图中显示当前用户位置一次

转载 作者:行者123 更新时间:2023-11-30 03:11:07 25 4
gpt4 key购买 nike

我的应用程序显示 map ,并自动将用户带到他们当前的位置。有一个文本框,允许用户键入他们的位置以在文本栏中键入的地址处显示标记。

当我启动该应用程序时,它会正确地向用户显示他们所在的位置。当我在 map 上输入新地址时,它会在新输入的位置显示一个标记。但是,此后它会自动将用户带回他们所在的位置,并从用户键入的位置中删除标记。

我想 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 中

最佳答案

会不会是您的 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));
}
}

关于android - 仅在谷歌地图中显示当前用户位置一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20963788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com