Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.stuff;
- 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 com.google.android.gms.maps.model.MarkerOptions;
- import android.location.Criteria;
- import android.location.Location;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.view.Menu;
- import android.support.v4.app.FragmentActivity;
- public class MainActivity extends FragmentActivity {
- private GoogleMap googleMap;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setUpMapIfNeeded();
- }
- private void setUpMapIfNeeded() {
- // TODO Auto-generated method stub
- if (googleMap == null) {
- googleMap = ((SupportMapFragment)
- getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
- if (googleMap != null) {
- setUpMap();
- }
- }
- }
- private void setUpMap() {
- // TODO Auto-generated method stub
- googleMap.setMyLocationEnabled(true);
- LocationManager locationManager = (LocationManager)
- getSystemService(LOCATION_SERVICE);
- Criteria criteria = new Criteria();
- String provider = locationManager.getBestProvider(criteria, true);
- Location myLocation = locationManager.getLastKnownLocation(provider);
- googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
- double latitude = myLocation.getLatitude();
- double longitude = myLocation.getLongitude();
- LatLng latLng = new LatLng(latitude, longitude);
- googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
- googleMap.animateCamera(CameraUpdateFactory.zoomTo(10));
- googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,
- longitude)).title("You are here!"));
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement