2013-02-12 125 views
18

我想用自己的圖像替換Android Maps V2用於「我的位置」(小藍點/箭頭)的默認圖標。我創建了自己的瓦片provider,它帶有一些主要爲藍色的地圖,因此默認的「我的位置」圖標很難看清。以前我會重寫MyLocationOverlay的繪製方法,但在新的API中似乎沒有。任何幫助將是偉大的!Android Maps API v2更改我的位置圖標

編輯:我還需要圖標才能夠旋轉,與箭頭所做的相同方式取決於您面對的方式。所以我不能只是使用普通的 :(基本上我只需要創建爲箭頭的自定義圖像..

enter image description here

更新大家好,更新​​到谷歌現在玩允許扁平標記和標記旋轉是正是我需要的

+0

你檢查你的SDK中的谷歌地圖V2已經以實例闡述與您的自定義圖像添加標記的功能和旋轉的演示它也相應地。 – GrIsHu 2013-02-16 04:45:00

+0

嘿Grishu,我確實看了一下SDK中的例子,演示程序的哪個部分專門看着旋轉我的位置圖標?我似乎無法找到它。 – Gyroscope 2013-02-23 03:13:39

+0

在sdk demo中,檢查已經顯示在地圖上添加標記的方式的標記類。 – GrIsHu 2013-02-23 04:40:43

回答

19

最新更新谷歌Play服務,現在允許你使用「平」的標記和旋轉它們是正是我需要的。這是我實現它的一個簡單版本。我可以做很多工作來優化和調整動畫,但它現在可以完成這項工作。歡迎任何反饋。

Marker mPositionMarker; 
GoogleMap mMap; 

@Override 
public void onLocationChanged(Location location) { 

    if (location == null) 
     return; 

    if (mPositionMarker == null) { 

     mPositionMarker = mMap.addMarker(new MarkerOptions() 
       .flat(true) 
       .icon(BitmapDescriptorFactory 
         .fromResource(R.drawable.positionIndicator)) 
       .anchor(0.5f, 0.5f) 
       .position(
         new LatLng(location.getLatitude(), location 
           .getLongitude()))); 
    } 

    animateMarker(mPositionMarker, location); // Helper method for smooth 
               // animation 

    mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location 
      .getLatitude(), location.getLongitude()))); 

} 

public void animateMarker(final Marker marker, final Location location) { 
    final Handler handler = new Handler(); 
    final long start = SystemClock.uptimeMillis(); 
    final LatLng startLatLng = marker.getPosition(); 
    final double startRotation = marker.getRotation(); 
    final long duration = 500; 

    final Interpolator interpolator = new LinearInterpolator(); 

    handler.post(new Runnable() { 
     @Override 
     public void run() { 
      long elapsed = SystemClock.uptimeMillis() - start; 
      float t = interpolator.getInterpolation((float) elapsed 
        /duration); 

      double lng = t * location.getLongitude() + (1 - t) 
        * startLatLng.longitude; 
      double lat = t * location.getLatitude() + (1 - t) 
        * startLatLng.latitude; 

      float rotation = (float) (t * location.getBearing() + (1 - t) 
        * startRotation); 

      marker.setPosition(new LatLng(lat, lng)); 
      marker.setRotation(rotation); 

      if (t < 1.0) { 
       // Post again 16ms later. 
       handler.postDelayed(this, 16); 
      } 
     } 
    }); 
} 
+0

不好:/ – Nima 2014-08-18 21:00:19

+0

這是否代表軸承? – 2014-08-29 16:58:14

+0

爲什麼這行給我不兼容的類型錯誤? final Interpolator interpolator = new LinearInterpolator(); – 2015-08-25 16:36:12

-2

您可以通過執行更改您的當前位置的圖標:

mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())) 
        .title("Current Location") 
        .snippet(address) 
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.your_image))); 
+0

@Gyroscope嘿做到了或沒有? – KunalK 2013-02-15 10:27:44

+0

你可以在圖像上標題.title(「當前位置」)=在圖像標題設置的圖像地址中設置可以使它.......或者如果你知道這一點... – Amitsharma 2014-02-18 07:18:49

24

我簡單的解決辦法就是禁用「我locatio谷歌地圖

的N」和創建地圖的ImageView與我的圖標,然後捕獲的ImageView與

的onClick和getMyLocation,animateCamera中的onClick

this.mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false); 
this.mGoogleMap.setMyLocationEnabled(true); 

@Override 
public void onClick(final View v) { 

    Location location = this.mGoogleMap.getMyLocation(); 

     if (location != null) { 

      LatLng target = new LatLng(location.getLatitude(), location.getLongitude()); 
      CameraPosition position = this.mGoogleMap.getCameraPosition(); 

      Builder builder = new CameraPosition.Builder(); 
      builder.zoom(15); 
      builder.target(target); 

      this.mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(builder.build())); 

      }  
} 
0

你不能處理點擊谷歌地圖MyLocation按鈕的情況下,是的,但有一個小竅門,通過它,你可以得到的行爲根據自己的需要:
先在你layout.xml文件中添加地圖和假MyLocation按鈕。
你可以用相對佈局的幫助下實現這一目標:

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

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

    <Button 
     android:id="@+id/myLocationButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:background="@drawable/mylocation" /> 
</RelativeLayout> 


你可以採取「mylocation.png」似乎像你在谷歌地圖上看到的。
LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

Criteria criteria = new Criteria(); 
    String s = locationManager.getBestProvider(criteria, false); 

    Location location = locationManager.getLastKnownLocation(s); 

您的虛擬MyLocation按鈕呼叫點擊事件,在其中您將添加標記
private GoogleMap googleMap; googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); googleMap.addMarker(new MarkerOptions().icon(YOUR_MODIFIED_ICON).position(latLng).title(TITLE).snippet(SNIPET));
這樣可以修改谷歌地圖的默認圖標。
是啊,你也需要設置縮放級別:
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

4

由於3.1.36您不能更改的圖標(我覺得它會改變未來,但看着API V2改進執行速度,將不會在聖誕節前)。

但是,您現在可以使用標記,因爲它具有setIcon函數。

基本上你必須忘記GoogleMap.setMyLocationEnabled,因爲它總是會出現在上面。您將改爲創建自己的LocationClient並使用Location的經緯度,長度和方位來更新Marker。此外,請添加Circle並使用準確性來獲得與默認myLocation可視化類似的效果。

0

試試這個;我用這個在地圖上繪製的路線,但我設置圖標標記和我的位置 聲明此爲查看您的位置:

LocationManager locationManager = (LocationManager) 
    getSystemService(LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    String provider = locationManager.getBestProvider(criteria, true); 
    Location myLocation= locationManager.getLastKnownLocation(provider); 

    longitude = myLocation.getLongitude(); 
    latitude = myLocation.getLatitude(); 
    fromPosition = new LatLng(latitude, longitude); 

,創造這樣的一個功能,我使用戰平地圖,我的標記,我的位置設置圖標:

mGoogleMap.clear(); 
if(response.equalsIgnoreCase("Success")) 
{ 
    ArrayList<LatLng> directionPoint = v2GetRouteDirection.getDirection(document); 
    PolylineOptions rectLine = new PolylineOptions().width(7).color(Color.BLUE); 

    for (int i = 0; i < directionPoint.size(); i++) { 
    rectLine.add(directionPoint.get(i)); 
    } 
    // Adding route on the map 
    mGoogleMap.setOnMarkerClickListener(MainActivity.this); 
    mGoogleMap.addPolyline(rectLine); 
    markerOptions.position(fromPosition); 
    markerOptions.draggable(true); 
    Marker1 = mGoogleMap.addMarker(new MarkerOptions() 
       .position(Estadio) 
       .title("Estadio Cuscatlan") 
       .snippet("Estadio Cuscatlan")              
       .icon(BitmapDescriptorFactory           
       .fromResource(R.drawable.mapmarker))); 
    Marker2 = mGoogleMap.addMarker(new MarkerOptions() 
       .position(Metrocentro) 
      .title("Metrocentro") 
      .snippet("Metrosuelo") 
      .icon(BitmapDescriptorFactory 
      .fromResource(R.drawable.mapmark 
     Marker3 = mGoogleMap.addMarker(new MarkerOptions() 
      .position(Mejicanos) 
      .title("Mejicanos") 
      .snippet("Mejicanos") 
      .icon(BitmapDescriptorFactory 
      .fromResource(R.drawable.mapmarker))); 
     MarkerMe = mGoogleMap.addMarker(new MarkerOptions()            
         .position(fromPosition) 
         .icon(BitmapDescriptorFactory 
         .fromResource(R.drawable.car64))); 
    } 
    Dialog.dismiss(); 
    } 
} 
+0

你可以使標題圖片.title(「Estadio Cuscatlan」)=在圖片標題的圖像地址中設置,你可以讓它........ – Amitsharma 2014-02-18 07:17:45

0

這裏訪問的位置按鈕的代碼:

View mv=findViewById(R.id.map); 
     View locationButton = ((View) mv.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

歡迎來到Stack Overflow!感謝您的代碼片段,它可能會提供一些即時的幫助。通過展示*爲什麼*這是一個很好的解決方案,對未來的讀者會有更好的解決方案,這將爲它的教育價值提供一個合適的解釋[//大大提高](// meta.stackexchange.com/q/114762)但不完全相同的問題。請編輯您的答案以添加解釋,並指出適用的限制和假設。 – 2017-06-05 11:29:47

相關問題