2015-09-29 102 views
2

如何使用標記查找當前位置。這段代碼找到了位置,但是如何在當前位置顯示標記?在android studio中使用標記獲取當前位置

public void onSearch (View view) 
    { 
     ibtn = (ImageButton)findViewById(R.id.imageSearchButton); 
     EditText location_tf = (EditText) findViewById(R.id.editTextSearch); 
     String location = location_tf.getText().toString(); 
     List<Address> addressList = null; 

     if(location != null || !location.equals("")) 
     { 
      Geocoder geocoder = new Geocoder(this); 
      try{ 
       addressList = geocoder.getFromLocationName(location, 1); 
      }catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 

      Address address = addressList.get(0); 
      LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude()); 
      mMap.addMarker(new MarkerOptions().position(latLng).title("Accidents Here")); 
      mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 

     }  
    } 

回答

2

試試這個,

寫的onCreate()在您的主要活動類。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_gooogle_map); 

     TextView tvAddress = (TextView) findViewById(R.id.textView1); 
     GoogleMap googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

     googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
     googleMap.setMyLocationEnabled(true); 
     googleMap.getUiSettings().setZoomControlsEnabled(true); 
     googleMap.getUiSettings().setCompassEnabled(true); 
     googleMap.getUiSettings().setMyLocationButtonEnabled(true); 
     googleMap.getUiSettings().setZoomGesturesEnabled(true); 
     googleMap.getUiSettings().setRotateGesturesEnabled(true); 

     LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     List<String> providers = lm.getProviders(true); 
     Location l = null; 

     for (int i = 0; i < providers.size(); i++) { 
      l = lm.getLastKnownLocation(providers.get(i)); 
      if (l != null) { 
       latitude = l.getLatitude(); 
       longitude = l.getLongitude(); 
       strAdd = getCompleteAddressString(latitude, longitude); 
       tvAddress.setText("Complete Address : " + strAdd); 
       break; 
      } 
     } 

if (googleMap != null) { 

MarkerOptions marker = new MarkerOptions().position(
        new LatLng(latitude, longitude)).title("Hello Maps").snippet("Discription"); 

marker.icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); 

// Moving Camera to a Location with animation 
      CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(new LatLng(latitude, longitude)).zoom(12).build(); 

      googleMap.animateCamera(CameraUpdateFactory 
        .newCameraPosition(cameraPosition)); 

      googleMap.addMarker(marker); 

} 
} 

並且這是你的getCompleteAddressString()。

private String getCompleteAddressString(double LATITUDE, double LONGITUDE) { 
     String strAdd = ""; 
     Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
     try { 
      List<android.location.Address> addresses = geocoder 
        .getFromLocation(LATITUDE, LONGITUDE, 1); 
      if (addresses != null) { 
       android.location.Address returnedAddress = addresses.get(0); 
       StringBuilder strReturnedAddress = new StringBuilder(""); 

       for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) { 
        strReturnedAddress 
          .append(returnedAddress.getAddressLine(i)).append(
            "\n"); 
       } 
       strAdd = strReturnedAddress.toString(); 
       Log.w("My Current loction address", 
         "" + strReturnedAddress.toString()); 
      } else { 
       Log.w("My Current loction address", "No Address returned!"); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Log.w("My Current loction address", "Canont get Address!"); 
     } 
     return strAdd; 
    } 
+0

不是我在找什麼,你在給lat1 = 23.043714; lang1 = 72.6077147 ;? – Meli

+0

試試我編輯的答案 –