2016-04-22 47 views
8

Tôi đang làm việc trên bản đồ google và tôi cần thay đổi vị trí của Vị trí của tôi (nút vị trí hiện tại). Nút vị trí hiện tại ở trên cùng bên phải. Vì vậy, hãy giúp tôi làm thế nào để tái định vị nút vị trí hiện tại trên màn hình bản đồ google. Cảm ơn trước.Cách thay đổi vị trí của Nút Vị trí của tôi trong Google Maps bằng cách sử dụng android studio

mẫu mã của tôi:

final GoogleMap googleMap = mMapView.getMap(); 
googleMap.setMyLocationEnabled(true); 
+2

Tôi không nghĩ là có thể. –

+0

định dạng mã tốt hơn và sửa một số định dạng ngữ pháp – Robert

Trả lời

8

bạn có thể sử dụng

View locationButton = ((View) mMapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); 
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams(); 
// position on right bottom 
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); 
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
rlp.setMargins(0, 180, 180, 0); 
+0

để biết thêm chi tiết kiểm tra tại đây http://stackoverflow.com/questions/31591524/android-align-left-aligns-to-the-center –

+0

ở đây thực sự: http: // stackoverflow. com/questions/28688470/android-google-maps-api-change-vị trí-of-maps-toolbar/31337222 # 31337222 và Yashwanth có lỗi trong đôi ALIGN-PARENT_TOP – djdance

2

Bằng cách đặt đệm để googlemap Fragment bạn có thể thay đổi vị trí của myLocationButton nhưng chỉ vấn đề là nó cũng sẽ thay đổi vị trí của các nút khác như zoom.

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
mapFragment.getMapAsync(this); 

@Override 
public void onMapReady(GoogleMap map) { 
    map.setPadding(left, top, right, bottom); 
} 
0

Bạn có thể làm một việc. Bạn vô hiệu hóa nút "Vị trí của tôi" mặc định và thực hiện nút tùy chỉnh ở vị trí mong muốn của bạn và khi nhấp vào nút đó, bạn di chuyển camara sang latlng hiện tại.

0

Thật không may, bạn chỉ có thể đặt đệm, như vậy:

@Override 
public void onMapReady(GoogleMap googleMap) { 
    googleMap.setPadding(left, top, right, bottom); 
    ... 
} 

tôi đã kết thúc tạo của riêng tôi và nhúng nó trong một bố cục khung sử dụng layout_gravity để xác định nơi tôi muốn nó, trong trường hợp này dưới/kết thúc :

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="300dp"> 

    <fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     .../> 


    <ImageView 
     android:id="@+id/myLocationCustomButton" 
     android:layout_width="@dimen/custom_my_location_button_size" 
     android:layout_height="@dimen/custom_my_location_button_size" 
     android:layout_gravity="bottom|end" 
     android:background="@drawable/disc" 
     android:backgroundTint="@android:color/white" 
     android:padding="@dimen/margin_smallest" 
     android:src="@drawable/ic_my_location" 
     ../> 
</FrameLayout> 

sau đó, trong hoạt động này, tôi khởi tạo và bắt đầu một api khách hàng google mà tôi có thể sử dụng để lấy vị trí hiện tại khi cần thiết bằng các nút trong một nhấp chuột, xem người nghe nút bấm bên dưới:

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    ... 
    if (googleApiClient == null) { 
     googleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 

    myLocationButton = rootView.findViewById(R.id.myLocationCustomButton); 
} 

@Override 
protected void onStart() { 
    googleApiClient.connect(); 
    super.onStart(); 
} 

@Override 
protected void onStop() { 
    googleApiClient.disconnect(); 
    super.onStop(); 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    myLocationButton.performClick(); 
} 

@Override 
public void onConnectionSuspended(int i) { 
} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
} 

@Override 
public void onMapReady(final GoogleMap googleMap) { 
    this.googleMap = googleMap; 

    myLocationButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
      CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16); 
      MainActivity.this.googleMap.animateCamera(cameraUpdate, 250, null); 
     } 
    }); 

Ứng dụng của tôi sub-module của build.gradle cũng có:

ext { 
    playServicesVersion = "8.4.0" 
} 

dependencies { 
    ...  
    compile "com.google.android.gms:play-services-location:${playServicesVersion}" 
    ... 
} 

Hy vọng rằng sẽ giúp.

12

tôi giải quyết vấn đề này trong mảnh bản đồ của tôi bằng cách tái định vị nút vị trí của tôi vào góc thấp bên phải của điểm sử dụng mã dưới đây, đây là MapsActivity.java tôi: -

import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.RelativeLayout; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 
    View mapView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_map); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapView = mapFragment.getView(); 
     mapFragment.getMapAsync(this); 
      } 

    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     mMap.setMyLocationEnabled(true); 

     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(-34, 151); 
     mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 

     if (mapView != null && 
       mapView.findViewById(Integer.parseInt("1")) != null) { 
      // Get the button view 
      View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); 
      // and next place it, on bottom right (as Google Maps app) 
      RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) 
        locationButton.getLayoutParams(); 
      // position on right bottom 
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); 
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
      layoutParams.setMargins(0, 0, 30, 30); 
     } 

    } 
} 

Và đây là cách bố trí của đoạn: -

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.infogird.www.location_button_reposition.MapFragment"> 

    <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:map="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 
</FrameLayout> 

Tôi hy vọng, điều này sẽ giải quyết vấn đề của bạn. Cảm ơn.

+0

Vị trí này sắp xếp lại điều gì? layoutParams.addRule (RelativeLayout.ALIGN_PARENT_TOP, 0); layoutParams.addRule (RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); –

3

Bạn có thể sử dụng dưới mã cho tất cả các điều kiện như: 1.If bạn muốn hiển thị nút vị trí trong góc trên bên phải 2.If bạn muốn hiển thị nút vị trí ở phía dưới bên phải 3. Nếu bạn muốn hiển thị nút vị trí ở phía dưới bên trái

1.If bạn muốn hiển thị nút vị trí trong góc trên bên phải

View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); 
      RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams(); 
      // position on right bottom 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 

2.If bạn muốn hiển thị nút vị trí ở phía dưới bên phải

View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); 
      RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams(); 
      // position on right bottom 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);rlp.setMargins(0,0,30,30); 

3.Nếu bạn muốn hiển thị nút vị trí ở phía dưới bên trái

View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); 
      RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams(); 
      // position on right bottom 


      rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); 

      rlp.addRule(RelativeLayout.ALIGN_PARENT_END, 0); 
      rlp.addRule(RelativeLayout.ALIGN_END, 0); 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
      rlp.setMargins(30, 0, 0, 40); 

Best of Luck

0

công việc của nó hiển thị My Location Nút Dưới cùng bên phải góc trên MapView

XML

<com.google.android.gms.maps.MapView 
    android:id="@+id/mapView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

JAVA

private MapView mMapView; 

mMapView = (MapView) findViewById(R.id.mapView); 

mMapView.getMapAsync(new OnMapReadyCallback() { 
    @Override 
    public void onMapReady(final GoogleMap mMap) { 
     if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      mMap.setMyLocationEnabled(true); 
     } 

     if (mMapView != null && mMapView.findViewById(Integer.parseInt("1")) != null) { 
      View locationButton = ((View) mMapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); 
      RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) locationButton.getLayoutParams(); 
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); 
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
      layoutParams.setMargins(0, 0, 20, 20); 
     } 
    } 
}); 
0

Tôi biết nó đã được trả lời và chấp nhận nhưng những câu trả lời này không hiệu quả với tôi. Có lẽ đã có những thay đổi trong RelativeLayout.LayoutParams hoặc thậm chí trong GoogleMaps kể từ khi nó được trả lời.

Trong nỗ lực của tôi với các câu trả lời hiện có, tôi nhận ra rằng có thể có nhiều quy tắc LayoutParam cho nút la bàn ghi đè nỗ lực di chuyển nút la bàn của tôi. Tôi đã thử nghiệm bằng cách xóa một số thông số như sau:

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) 
       locationButton.getLayoutParams(); 

layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP); 
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_START); 
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END); 

Và sau đó thêm quy tắc mới như đã thực hiện trong một số câu trả lời. Nhưng nó vẫn không hoạt động như mong đợi. Thường xuyên hơn không, nút ở đâu đó ở phía bên trái.

Tôi quyết định chỉ tạo LayoutParams mới và thay thế các bảng hiện có và ... nó hoạt động hoàn hảo!

Tôi ban đầu đã sử dụng thẻ xem cho GoogleMapCompass, nhưng câu hỏi liên quan đến GoogleMapMyLocationButton. Vì vậy, tôi đã nhận xét ra dòng GoogleMapCompass và đặt trong dòng GoogleMapMyLocationButton.

Đây là mã cho MapFragment.java:

import android.app.Fragment; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.RelativeLayout; 

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 


public class MapFragment extends Fragment implements OnMapReadyCallback { 
    private static final String TAG = MapFragment.class.getSimpleName(); 

    private SupportMapFragment mapFragment = null; 

    public MapFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment_map, container, false); 

     Log.d(TAG, "onCreateView()"); 

     mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); 
     mapFragment.setRetainInstance(true); 

     mapFragment.getMapAsync(this); 

     return view.findViewById(R.id.map); 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     Log.d(TAG, "onMapReady()"); 

     View mapView = mapFragment.getView(); 

     moveCompassButton(mapView); 
    } 

    /** 
    * Move the compass button to the right side, centered vertically. 
    */ 
    public void moveCompassButton(View mapView) { 
     try { 
      assert mapView != null; // skip this if the mapView has not been set yet 

      Log.d(TAG, "moveCompassButton()"); 

      // View view = mapView.findViewWithTag("GoogleMapCompass"); 
      View view = mapView.findViewWithTag("GoogleMapMyLocationButton"); 

      // move the compass button to the right side, centered 
      RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE); 
      layoutParams.addRule(RelativeLayout.CENTER_VERTICAL); 
      layoutParams.setMarginEnd(18); 

      view.setLayoutParams(layoutParams); 
     } catch (Exception ex) { 
      Log.e(TAG, "moveCompassButton() - failed: " + ex.getLocalizedMessage()); 
      ex.printStackTrace(); 
     } 
    } 
} 
Các vấn đề liên quan