谷歌地图 v2-设置我的位置和放大

我的问题是,有人知道如何设置谷歌地图,打开我的位置和在放大的视图?

目前,主要景观向非洲开放,一路放大。

所以我已经找了好几天了,我能找到的只有:

1)你不能在一个谷歌地图上动画两个东西(如放大和到我的位置) ?所以如果我能在设置动画之前设置缩放,那么这个问题就解决了。这就是问题所在,你可以改变一个,但不能同时改变两个。

2)我已经找到了其他可能有用的类,但是没有关于如何设置代码以便类可以操作谷歌地图的帮助。

这是我一直坚持到现在的代码,有些作品,有些没有。一些我觉得以后可能会有用的。

package com.MYWEBSITE.www;


import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;


public class MainActivity extends FragmentActivity {
private GoogleMap map;


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


map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);


//LocationSource a = (LocationSource) getSystemService(Context.LOCATION_SERVICE);
//LocationManager b = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//map.setLocationSource(a);


Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
double lat =  location.getLatitude();
double lng = location.getLongitude();
LatLng coordinate = new LatLng(lat, lng);


//CameraPosition.Builder x = CameraPosition.builder();
//x.target(coordinate);
//x.zoom(13);


//Projection proj = map.getProjection();
//Point focus = proj.toScreenLocation(coordinate);


//map.animateCamera(CameraUpdateFactory.newLatLng(coordinate));
map.animateCamera(CameraUpdateFactory.zoomBy(13));
//map.moveCamera(CameraUpdateFactory.newLatLng(coordinate));




////LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
}
}
163821 次浏览

你不能动画两件事情(如放大和去我的位置)在一个谷歌地图?

从编码的角度来看,您可以按顺序执行它们:

    CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
-73.98180484771729));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);


map.moveCamera(center);
map.animateCamera(zoom);

在这里,我先移动相机,然后动画相机,虽然两者都可以是 animateCamera()调用。GoogleMap是否将这些合并成一个事件,我不能说,因为它过得太快了。:-)

下面是我从中提取上述代码的示例项目


抱歉,这个答案有缺陷。请参阅 Rob's answer,了解一种在一次拍摄中真正做到这一点的方法,即创建一个 CameraPosition,然后从该 CameraPosition创建一个 CameraUpdate

您还可以设置这两个参数,如,

mMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(21.000000, -101.400000) ,4) );

这个可以定位你的地图在一个特定的位置和缩放。我用它来设置我的地图。

可以一次性改变位置、变焦、方位和倾斜。也可以设置 animateCamera()调用的持续时间。

CameraPosition cameraPosition = new CameraPosition.Builder()
.target(MOUNTAIN_VIEW)      // Sets the center of the map to Mountain View
.zoom(17)                   // Sets the zoom
.bearing(90)                // Sets the orientation of the camera to east
.tilt(30)                   // Sets the tilt of the camera to 30 degrees
.build();                   // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

看看这里的文件:

Https://developers.google.com/maps/documentation/android/views?hl=en-us#moving_the_camera

试试这样-

public class SummaryMapActivity extends FragmentActivity implements LocationListener{


private GoogleMap mMap;
private LocationManager locationManager;
private static final long MIN_TIME = 400;
private static final float MIN_DISTANCE = 1000;


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


if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
mMap.setMyLocationEnabled(true);


locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this);




}


@Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
mMap.animateCamera(cameraUpdate);
locationManager.removeUpdates(this);


}


@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


}

}

this is simple solution for your question

LatLng coordinate = new LatLng(lat, lng);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
map.animateCamera(yourLocation);
gmap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng(9.491327, 76.571404), 10, 30, 0)));

这是一个晚期的答案,但我认为它将有所帮助。使用这种方法:

protected void zoomMapInitial(LatLng finalPlace, LatLng currenLoc) {
try {
int padding = 200; // Space (in px) between bounding box edges and view edges (applied to all four sides of the bounding box)
LatLngBounds.Builder bc = new LatLngBounds.Builder();


bc.include(finalPlace);
bc.include(currenLoc);
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bc.build(), padding));
} catch (Exception e) {
e.printStackTrace();
}
}

加载地图后使用此方法。干杯!

最简单的方法是使用 CancelableCallback。您应该检查第一个操作是否完成,然后调用第二个:

mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, size.x, height, 0), new CancelableCallback() {


@Override
public void onFinish() {
CameraUpdate cu_scroll = CameraUpdateFactory.scrollBy(0, 500);
mMap.animateCamera(cu_scroll);
}


@Override
public void onCancel() {
}
});

@ CommonsWare 的回答实际上并不起作用,我发现这个工作正常:

map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-33.88,151.21), 15));

1.Add xml code in your layout for displaying maps.

启用谷歌地图 api 然后得到 api 的关键位置如下。

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:value="ADD-API-KEY"
android:layout_height="250dp"
tools:layout="@layout/newmaplayout" />
<ImageView
android:id="@+id/transparent_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@color/transparent" />

3. 在 oncreate 中添加此代码。

 MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(UpadateProfile.this);

4. 在 oncreate 后添加此代码。然后使用标记访问当前位置 放在那里

@Override
public void onMapReady(GoogleMap rmap) {
DO WHATEVER YOU WANT WITH GOOGLEMAP
map = rmap;
setUpMap();
}
public void setUpMap() {
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
map.setTrafficEnabled(true);
map.setIndoorEnabled(true);
map.getCameraPosition();
map.setBuildingsEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
markerOptions = new MarkerOptions();
markerOptions.title("Outlet Location");
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
map.clear();
markerOptions.position(point);
map.animateCamera(CameraUpdateFactory.newLatLng(point));
map.addMarker(markerOptions);
String all_vals = String.valueOf(point);
String[] separated = all_vals.split(":");
String latlng[] = separated[1].split(",");
MyLat = Double.parseDouble(latlng[0].trim().substring(1));
MyLong = Double.parseDouble(latlng[1].substring(0,latlng[1].length()-1));
markerOptions.title("Outlet Location");
getLocation(MyLat,MyLong);
}
});
}
public void getLocation(double lat, double lng) {
Geocoder geocoder = new Geocoder(UpadateProfile.this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
} catch (IOException e) {
TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this,e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}

You cannot animate two things (like zoom in and go to my location) in one google map.
所以使用移动和动画相机来缩放

googleMapVar.moveCamera(CameraUpdateFactory.newLatLng(LocLtdLgdVar));
googleMapVar.animateCamera(CameraUpdateFactory.zoomTo(10));