2013-02-12 99 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

相关问题