2014-01-07 74 views
1

我的應用程序顯示地圖,並自動將用戶帶到當前位置。有一個文本框允許用戶鍵入他們的位置,以在文本欄上鍵入的地址顯示標記。在谷歌地圖中僅顯示當前用戶位置一次

當我啓動該應用程序時,它會正確顯示用戶所在的位置。當我在地圖上鍵入新地址時,它會在新輸入的位置顯示一個標記。但是,在此之後,它會自動將用戶帶回到他們所在的位置,並從用戶鍵入的位置刪除標記。

我想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:我也想知道,在一個側面說明,如果是將字符串轉換爲解決使用地理編碼爲shown..or我應該把這樣的一個的AsyncTask裏面一個很好的做法

回答

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)); 
    } 
    } 
+0

有什麼solution..how我可以顯示它只有一次 – gazubi

+0

首次onLocationChanged火災時,您可以保存關閉位置對象爲一個成員變量,和然後在onLocationChanged()中添加一個警戒,如果位置已經設​​置,則不更新標記。 – Eduard

+1

我使用代碼片段 – Eduard

相關問題